-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTicTacToe.py
166 lines (136 loc) · 4.87 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
import random
class Player:
def __init__(self, name):
self.name = name
def get_input(self, board):
x = int(input("Enter x:"))
y = int(input("Enter y:"))
return x, y
class AI(Player):
def __init__(self, name):
super().__init__(name)
def get_input(self, board):
res = self.findBestMove(board)
print(res)
return res
def findBestMove(self, board):
bestScore = -1000
bestMove = (-1,-1)
for i in range(0,3):
for j in range(0,3):
if board.matrix[i][j] == 0:
board.matrix[i][j] = 2
current_score = self.minimax(board,1, False)
if current_score > bestScore:
bestScore = current_score
bestMove = (i,j)
board.matrix[i][j] = 0
return bestMove
def minimax(self, board, depth, isMaximizingPlayer):
go_check = board.is_game_over()
if go_check != 0:
if go_check == 1:
return -10 + depth
elif go_check ==2:
return 10 - depth
else:
return 0
if isMaximizingPlayer:
bestVal = -1000
for i in range(0,3):
for j in range(0,3):
if board.matrix[i][j] == 0:
board.matrix[i][j] = 2
value = self.minimax(board, depth + 1, False)
bestVal = max(bestVal, value)
board.matrix[i][j] = 0
return bestVal
else:
bestVal = 1000
for i in range(0,3):
for j in range(0,3):
if board.matrix[i][j]==0:
board.matrix[i][j] = 1
value = self.minimax(board, depth+1, True)
bestVal = min(bestVal, value)
board.matrix[i][j] = 0
return bestVal
class Board:
def __init__(self):
self.matrix = [[0, 0, 0], [0, 0, 0], [0, 0, 0]]
def set(self, player_key, row, column):
if row < 0 or row > 2 or column < 0 or column > 2:
return False
if self.matrix[row][column] != 0:
return False
self.matrix[row][column] = player_key
return True
def is_full(self):
for i in range(0,3):
for j in range(0,3):
if self.matrix[i][j] == 0:
return False
return True
def is_game_over(self):
for i in range(0,3):
ok = True
for j in range(0,3):
if self.matrix[i][j] != self.matrix[i][0]:
ok = False
if ok == True and self.matrix[i][0] != 0:
return self.matrix[i][0]
for j in range(0,3):
ok = True
for i in range(0,3):
if self.matrix[i][j] != self.matrix[0][j]:
ok = False
if ok == True and self.matrix[0][j] != 0:
return self.matrix[0][j]
if self.matrix[0][0] == self.matrix[1][1] and self.matrix[1][1] == self.matrix[2][2] and self.matrix[0][0] != 0:
return self.matrix[0][0]
if self.matrix[0][2] == self.matrix[1][1] and self.matrix[2][0] == self.matrix[1][1] and self.matrix[1][1] != 0:
return self.matrix[1][1]
if self.is_full() == True:
return 3
else:
return 0
def draw(self):
print("{} {} {}\n{} {} {}\n{} {} {}".format(*[self.matrix[i][j] for i in range(0,3) for j in range(0,3)]))
class Game:
def __init__(self):
self.player1 = Player("PlayerOne")
self.player2 = Player("PlayerTwo")
self.board = Board()
self.game_loop()
def game_loop(self):
result = 0
pindex = 1
while not result:
self.board.draw()
print("Player %d turn:" % pindex)
coord = (-1, -1)
if pindex == 1:
coord = self.player1.get_input(self.board)
else:
coord = self.player2.get_input(self.board)
if coord[0] > 2 or coord [0] < 0 or coord[1] > 2 or coord [1] < 0:
print("Incorrect coordinates")
continue
if not self.board.set(pindex, *coord):
print("Invalid choice")
continue
state = self.board.is_game_over()
if state == 1 or state == 2:
print("Player %d wins:" % pindex)
break
elif state == 3:
print("The game ended in draw")
break
pindex = pindex % 2 + 1
class AIGame(Game):
def __init__(self):
self.player1 = Player("PlayerOne")
self.player2 = AI("PlayerTwo")
self.board = Board()
self.game_loop()
game = AIGame()