-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1.py
95 lines (83 loc) · 2.78 KB
/
1.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
def print_board(board):
for row in board:
print(" ".join(row))
def is_winner(board, player):
# Check rows, columns, and diagonals
for i in range(3):
if all(board[i][j] == player for j in range(3)) or \
all(board[j][i] == player for j in range(3)):
return True
if all(board[i][i] == player for i in range(3)) or \
all(board[i][2 - i] == player for i in range(3)):
return True
return False
def is_full(board):
return all(board[i][j] != ' ' for i in range(3) for j in range(3))
def is_game_over(board):
return is_winner(board, 'X') or is_winner(board, 'O') or is_full(board)
def minimax(board, depth, maximizing_player):
if is_winner(board, 'X'):
return -1
elif is_winner(board, 'O'):
return 1
elif is_full(board):
return 0
if maximizing_player:
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)
board[i][j] = ' '
max_eval = max(max_eval, eval)
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)
board[i][j] = ' '
min_eval = min(min_eval, eval)
return min_eval
def get_best_move(board):
best_move = None
best_eval = float('-inf')
for i in range(3):
for j in range(3):
if board[i][j] == ' ':
board[i][j] = 'O'
eval = minimax(board, 0, False)
board[i][j] = ' '
if eval > best_eval:
best_eval = eval
best_move = (i, j)
return best_move
def play_game():
board = [[' ' for _ in range(3)] for _ in range(3)]
current_player = 'X'
while not is_game_over(board):
print_board(board)
if current_player == 'X':
row, col = map(int, input("Enter your move (row and column separated by space): ").split())
if board[row][col] == ' ':
board[row][col] = 'X'
current_player = 'O'
else:
print("Invalid move. Try again.")
else:
print("AI is thinking...")
move = get_best_move(board)
board[move[0]][move[1]] = 'O'
current_player = 'X'
print_board(board)
if is_winner(board, 'X'):
print("You win!")
elif is_winner(board, 'O'):
print("AI wins!")
else:
print("It's a draw!")
if __name__ == "__main__":
play_game()