-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtictactoe.py
124 lines (112 loc) · 3.94 KB
/
tictactoe.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
import copy
# Define the initial state of the board
initial_board = [
['_', '_', '_'],
['_', '_', '_'],
['_', '_', '_']
]
# Function to print the current state of the board
def print_board(board):
for row in board:
print(' '.join(row))
print()
# Function to check if a player has won
def check_winner(board, player):
# Check rows, columns, and diagonals
return any(
all(cell == player for cell in row) for row in board
) or any(
all(row[i] == player for row in board) for i in range(3)
) or all(
board[i][i] == player for i in range(3)
) or all(
board[i][2 - i] == player for i in range(3)
)
# Function to check if the board is full (draw)
def check_draw(board):
return all(all(cell != '_' for cell in row) for row in board)
# Minimax function to find the best move
def minimax(board, depth, is_maximizing, alpha, beta):
if check_winner(board, 'X'):
return -1
if check_winner(board, 'O'):
return 1
if check_draw(board):
return 0
if is_maximizing:
max_eval = float('-inf')
for i in range(3):
for j in range(3):
if board[i][j] == '_':
board[i][j] = 'O'
eval = minimax(board, depth + 1, False, alpha, beta)
board[i][j] = '_'
max_eval = max(max_eval, eval)
alpha = max(alpha, eval)
if beta <= alpha:
break
return max_eval
else:
min_eval = float('inf')
for i in range(3):
for j in range(3):
if board[i][j] == '_':
board[i][j] = 'X'
eval = minimax(board, depth + 1, True, alpha, beta)
board[i][j] = '_'
min_eval = min(min_eval, eval)
beta = min(beta, eval)
if beta <= alpha:
break
return min_eval
# Function to find the best move for the computer
def find_best_move(board):
best_move = None
best_value = float('-inf')
for i in range(3):
for j in range(3):
if board[i][j] == '_':
board[i][j] = 'O'
move_value = minimax(board, 0, False, float('-inf'), float('inf'))
board[i][j] = '_'
if move_value > best_value:
best_move = (i, j)
best_value = move_value
return best_move
# Main function to play the game
def play_game():
print("Welcome to Tic-Tac-Toe!")
print("You are 'X' and the computer is 'O'.")
print("Player 'X' goes first. Let's begin!\n")
while True:
board = copy.deepcopy(initial_board)
while True:
print_board(board)
if check_draw(board):
print("It's a draw!")
break
if check_winner(board, 'O'):
print("Player O wins!")
break
if check_winner(board, 'X'):
print("Player X wins!")
break
# Player X's turn (human)
row = int(input("Enter the row (0, 1, or 2): "))
col = int(input("Enter the column (0, 1, or 2): "))
if board[row][col] == '_':
board[row][col] = 'X'
else:
print("Invalid move. Try again.")
continue
# Player O's turn (computer)
if not check_winner(board, 'X') and not check_draw(board):
move = find_best_move(board)
if move:
board[move[0]][move[1]] = 'O'
# Ask the user if they want a rematch
rematch = input("Do you want a rematch? (yes/no): ").lower()
if rematch != 'yes' and rematch != 'y':
break
# Start the game
play_game()