diff --git a/tictactoe/__pycache__/tictactoe.cpython-313.pyc b/tictactoe/__pycache__/tictactoe.cpython-313.pyc new file mode 100644 index 0000000..c1d8204 Binary files /dev/null and b/tictactoe/__pycache__/tictactoe.cpython-313.pyc differ diff --git a/tictactoe/game_play.py b/tictactoe/game_play.py new file mode 100644 index 0000000..3528ad1 --- /dev/null +++ b/tictactoe/game_play.py @@ -0,0 +1,66 @@ +import tictactoe + +def print_board(board): + """ + Prints the board in a human-readable format. + """ + for row in board: + print("|", end=" ") + for cell in row: + print(cell if cell is not None else " ", end=" | ") + print("\n" + "-" * 13) + + +def get_human_move(board): + """ + Prompts user to input a valid move. + """ + while True: + try: + print("Enter row and column num") + row = int(input("Enter row (0, 1, 2): ")) + col = int(input("Enter column (0, 1, 2): ")) + if (row, col) in tictactoe.actions(board): + return (row, col) + else: + print("Invalid move. Try again.\n") + except ValueError: + print("Please enter valid integers.\n") + + +def main(): + board = tictactoe.initial_state() + human_player = input("Do you want to play as X or O? ").strip().upper() + + if human_player not in ["X", "O"]: + print("Invalid choice. Defaulting to X.") + human_player = "X" + + ai_player = tictactoe.O if human_player == tictactoe.X else tictactoe.X + + while not tictactoe.terminal(board): + print_board(board) + current = tictactoe.player(board) + + if current == human_player: + print(f"Your turn ({human_player})") + move = get_human_move(board) + else: + print(f"AI is thinking... ({ai_player})") + move = tictactoe.minimax(board) + + board = tictactoe.result(board, move) + + # Final board and result + print_board(board) + win = tictactoe.winner(board) + if win is None: + print("It's a draw!") + elif win == human_player: + print("You win!") + else: + print("AI wins!") + + +if __name__ == "__main__": + main() diff --git a/tictactoe/tictactoe.py b/tictactoe/tictactoe.py index 3d8d187..777fd42 100644 --- a/tictactoe/tictactoe.py +++ b/tictactoe/tictactoe.py @@ -65,56 +65,24 @@ def winner(board): """ Returns the winner of the game, if there is one. """ - for i in range(3): - if ( - board[i][0] == board[i][1] - and board[i][0] == board[i][2] - and board[i][0] != None - ): - if board[i][0] == "X": - return X - elif board[i][0] == "O": - return O - else: - return None - - for i in range(3): - if ( - board[0][i] == board[1][i] - and board[1][i] == board[2][i] - and board[0][i] != None - ): - if board[0][i] == "X": - return X - elif board[0][i] == "O": - return O - else: - return None - - if ( - board[0][0] == board[1][1] - and board[1][1] == board[2][2] - and board[0][0] != None - ): - if board[0][0] == "X": - return X - elif board[0][0] == "O": - return O - else: - return None - - if ( - board[2][0] == board[1][1] - and board[1][1] == board[0][2] - and board[2][0] != None - ): - if board[2][0] == "X": - return X - elif board[2][0] == "O": - return O - else: - return None - + win_combos = [ + # Rows + [(0,0), (0,1), (0,2)], + [(1,0), (1,1), (1,2)], + [(2,0), (2,1), (2,2)], + # Columns + [(0,0), (1,0), (2,0)], + [(0,1), (1,1), (2,1)], + [(0,2), (1,2), (2,2)], + # Diagonals + [(0,0), (1,1), (2,2)], + [(0,2), (1,1), (2,0)], + ] + + for combo in win_combos: + a, b, c = combo + if board[a[0]][a[1]] == board[b[0]][b[1]] == board[c[0]][c[1]] != None: + return board[a[0]][a[1]] return None