-
Notifications
You must be signed in to change notification settings - Fork 0
/
tictactoe.py
166 lines (152 loc) · 6.15 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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
class TicTacToe:
def __init__(self):
self.board = [[' ' for _ in range(3)] for _ in range(3)]
self.current_player = 'X'
def print_board(self):
for row in self.board:
print(' | '.join(row))
print('---------')
def is_valid_move(self, row, col):
return self.board[row][col] == ' '
def make_move(self, row, col):
if self.is_valid_move(row, col):
self.board[row][col] = self.current_player
self.current_player = 'O' if self.current_player == 'X' else 'X'
return True
return False
def is_game_over(self):
# Check rows and columns for a win
for i in range(3):
if self.board[i][0] == self.board[i][1] == self.board[i][2] != ' ':
return True
if self.board[0][i] == self.board[1][i] == self.board[2][i] != ' ':
return True
# Check diagonals for a win
if self.board[0][0] == self.board[1][1] == self.board[2][2] != ' ':
return True
if self.board[0][2] == self.board[1][1] == self.board[2][0] != ' ':
return True
# Check for a draw
if all(cell != ' ' for row in self.board for cell in row):
return True
return False
def minimax(self, depth, is_maximizing):
if self.is_game_over():
if self.current_player == 'X':
return -10 + depth
elif self.current_player == 'O':
return 10 - depth
else:
return 0
if is_maximizing:
best_score = -1000
for i in range(3):
for j in range(3):
if self.is_valid_move(i, j):
self.make_move(i, j)
score = self.minimax(depth + 1, False)
self.board[i][j] = ' '
self.current_player = 'X' if self.current_player == 'O' else 'X'
best_score = max(score, best_score)
return best_score
else:
best_score = 1000
for i in range(3):
for j in range(3):
if self.is_valid_move(i, j):
self.make_move(i, j)
score = self.minimax(depth + 1, True)
self.board[i][j] = ' '
self.current_player = 'X' if self.current_player == 'O' else 'X'
best_score = min(score, best_score)
return best_score
def ai_move(self):
best_score = -1000
best_move = None
for i in range(3):
for j in range(3):
if self.is_valid_move(i, j):
self.make_move(i, j)
score = self.minimax(0, False)
self.board[i][j] = ' '
self.current_player = 'X' if self.current_player == 'O' else 'X'
if score > best_score:
best_score = score
best_move = (i, j)
self.make_move(best_move[0], best_move[1])
class TicTacToe:
def __init__(self):
self.board = [[' ' for _ in range(3)] for _ in range(3)]
self.current_player = 'X'
def print_board(self):
for row in self.board:
print(' | '.join(row))
print('---------')
def is_valid_move(self, row, col):
return self.board[row][col] == ' '
def make_move(self, row, col):
if self.is_valid_move(row, col):
self.board[row][col] = self.current_player
self.current_player = 'O' if self.current_player == 'X' else 'X'
return True
return False
def is_game_over(self):
# Check rows and columns for a win
for i in range(3):
if self.board[i][0] == self.board[i][1] == self.board[i][2] != ' ':
return True
if self.board[0][i] == self.board[1][i] == self.board[2][i] != ' ':
return True
# Check diagonals for a win
if self.board[0][0] == self.board[1][1] == self.board[2][2] != ' ':
return True
if self.board[0][2] == self.board[1][1] == self.board[2][0] != ' ':
return True
# Check for a draw
if all(cell != ' ' for row in self.board for cell in row):
return True
return False
def minimax(self, depth, is_maximizing):
if self.is_game_over():
if self.current_player == 'X':
return -10 + depth
elif self.current_player == 'O':
return 10 - depth
else:
return 0
if is_maximizing:
best_score = -1000
for i in range(3):
for j in range(3):
if self.is_valid_move(i, j):
self.make_move(i, j)
score = self.minimax(depth + 1, False)
self.board[i][j] = ' '
self.current_player = 'X' if self.current_player == 'O' else 'X'
best_score = max(score, best_score)
return best_score
else:
best_score = 1000
for i in range(3):
for j in range(3):
if self.is_valid_move(i, j):
self.make_move(i, j)
score = self.minimax(depth + 1, True)
self.board[i][j] = ' '
self.current_player = 'X' if self.current_player == 'O' else 'X'
best_score = min(score, best_score)
return best_score
def ai_move(self):
best_score = -1000
best_move = None
for i in range(3):
for j in range(3):
if self.is_valid_move(i, j):
self.make_move(i, j)
score = self.minimax(0, False)
self.board[i][j] = ' '
self.current_player = 'X' if self.current_player == 'O' else 'X'
if score > best_score:
best_score = score
best_move = (i, j)
self.make_move(best_move[0], best_move[1])