import os from machine import Pin import time gpio_pins = range(28) pins = [Pin(pin_num, Pin.IN, Pin.PULL_UP) for pin_num in gpio_pins] up = pins[2] down = pins[3] left = pins[4] right = pins[5] a = pins[6] b = pins[7] playing = True def clear(): print("\x1B\x5B2J", end="") print("\x1B\x5BH", end="") def printBoard(board, selectCol = 4): clear() # Print the top border print(" "*((selectCol*2)-1) + "V" + " "*(15-(selectCol*2))) print("---------------") # Print each of the rows for row in range(6): print("|", end="") for col in range(7): print(board[col][row] + "|", end="") print() # Print the bottom border print("---------------") def chooseCol(selectCol = 4): while True: if a.value() != 0 and b.value() != 0: if left.value() == 0: if selectCol > 1: selectCol -= 1 printBoard(board, selectCol) time.sleep(0.3) elif right.value() == 0: if selectCol < 7: selectCol += 1 printBoard(board, selectCol) time.sleep(0.3) else: #print("You entered", selectCol) time.sleep(0.3) return selectCol # Initialize the board board = [] for col in range(7): board.append([" "] * 6) def isValidMove(board, col): # Check if the column is full if board[col - 1][0] != " ": return False # Otherwise, the move is valid return True def isWinner(board, player): # Check horizontal spaces for col in range(4): for row in range(6): if board[col][row] == player and board[col + 1][row] == player and board[col + 2][row] == player and board[col + 3][row] == player: return True # Check vertical spaces for col in range(7): for row in range(3): if board[col][row] == player and board[col][row + 1] == player and board[col][row + 2] == player and board[col][row + 3] == player: return True # Check / diagonal spaces for col in range(4): for row in range(3): if board[col][row] == player and board[col + 1][row + 1] == player and board[col + 2][row + 2] == player and board[col + 3][row + 3] == player: return True # Check \ diagonal spaces for col in range(4): for row in range(3, 6): if board[col][row] == player and board[col + 1][row - 1] == player and board[col + 2][row - 2] == player and board[col + 3][row - 3] == player: return True return False def makeMove(board, col, player): # Find the first empty space in the column for row in range(5, 0, -1): #print(row) if board[col - 1][row] == " ": board[col - 1][row] = player break # Main game loop while playing: # Print the board printBoard(board) # Ask for player 1 input #player1Input = input("Player 1, enter a column number (or 0 to quit): ") player1Input = chooseCol() # Check if player 1 wants to quit if player1Input == 0: playing = False break # Check if player 1's input is valid if player1Input < 1 or player1Input > 7: print("Invalid input. Try again.") continue # Check if player 1's input is a valid move if not isValidMove(board, player1Input): print("Invalid move. Try again.") continue # Make player 1's move makeMove(board, player1Input, "X") time.sleep(0.1) # Check if player 1 won if isWinner(board, "X"): printBoard(board) print("!!!Player 1 wins!!!") playing = False break # Print the board printBoard(board) # Ask for player 2 input #player2Input = input("Player 2, enter a column number (or 0 to quit): ") player2Input = chooseCol() # Check if player 2 wants to quit if player2Input == 0: playing = False break # Check if player 2's input is valid if player2Input < 1 or player2Input > 7: print("Invalid input. Try again.") continue # Check if player 2's input is a valid move if not isValidMove(board, player2Input): print("Invalid move. Try again.") continue # Make player 2's move makeMove(board, player2Input, "O") # Check if player 2 won if isWinner(board, "O"): #printBoard(board) print("!!!Player 2 wins!!!") playing = False break