-
Notifications
You must be signed in to change notification settings - Fork 0
/
tty.py
227 lines (182 loc) · 7.28 KB
/
tty.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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
"""Author : Amit Jagannath Magar
Assignment No 2: Designing unbeatable Tic Tac Toe game using MinMax Algorithm and MinMax algorithm using alpha beta pruning
Version : 1.0
Reference := Game search from Geek for Geeks
"""
"""
Player's move will always represented as 'x' and Computer's move will be represented as 'o'
"""
no_states = 0 # this variable will count no of states generated by each min max algo
no_statesab = 0 # this variable will count no of states generated by min max algorithm with pruning
def getVal(matrix):
"""This function checks if Player or Computer has own the game"""
# Checking for rows
for iter in range(0, 3):
if matrix[iter][0] == matrix[iter][1] and matrix[iter][1] == matrix[iter][2]:
if matrix[iter][0] == 'x':
return 1
if matrix[iter][0] == 'o':
return -1
# Checking for columns
for iter in range(0, 3):
if matrix[0][iter] == matrix[1][iter] and matrix[1][iter] == matrix[2][iter]:
if matrix[0][iter] == 'x':
return 1
if matrix[0][iter] == 'o':
return -1
# Checking for diagonals
if matrix[0][0] == matrix[1][1] and matrix[1][1] == matrix[2][2]:
if matrix[0][0] == 'x':
return 1
if matrix[0][0] == 'o':
return -1
if matrix[0][2] == matrix[1][1] and matrix[1][1] == matrix[2][0]:
if matrix[0][2] == 'x':
return 1;
if matrix[0][2] == 'o':
return -1
return 0
"""
This function checks if there are possible moves on board which player can take
"""
def checkMoves(matrix):
for iter1 in range(0, 3):
for iter2 in range(0, 3):
if matrix[iter1][iter2] == '_':
return True
return False
"""
This function is implementation of minimax algorithms which generate minimax tree recursively
"""
def miniMax(matrix, player):
global no_states
no_states = no_states + 1 # Increment count of global states generated by minimax algorithm for each step
INFINITY = float("inf")
# Evaluate board to check if anyone has won the game or not
val = getVal(matrix)
if val == 1:
return 1
elif val == -1:
return -1
else:
if checkMoves(matrix) == False: # if there is no winner in game and no possible moves to take return 0
return 0
if player == 'x': # check if this is maximizer player
max1 = -INFINITY
for it1 in range(0, 3):
for it2 in range(0, 3):
if matrix[it1][it2] == '_': # check for empty position on board and generate tree
matrix[it1][it2] = 'x'
max1 = max(max1, miniMax(matrix, 'o')) # Call minimizer ply
matrix[it1][it2] = '_'
return max1 # return maximum gain which can be achieved
else:
min1 = INFINITY
for it1 in range(0, 3):
for it2 in range(0, 3):
if matrix[it1][it2] == '_': # check for empty position on board and generate tree
matrix[it1][it2] = 'o'
min1 = min(min1, miniMax(matrix, 'x')) # Call maximizer ply
matrix[it1][it2] = '_'
return min1
"""
This function implements MiniMax algorithm with alph beta pruning
"""
def miniMaxAB(matrix, player, alpha, beta):
global no_statesab # Increment count of global states generated by minimax algorithm using pruning for each step
no_statesab = no_statesab + 1
INFINITY = float("inf")
# Evaluate currenet matrix board
val = getVal(matrix)
if val == 1:
return 1
elif val == -1:
return -1
else:
if checkMoves(matrix) == False:
return 0
if player == 'x': # check if this is maximizer player
# print ('in max function')
for it1 in range(0, 3):
for it2 in range(0, 3):
if matrix[it1][it2] == '_': # check for empty position on board and generate tree
matrix[it1][it2] = 'x'
val = miniMaxAB(matrix, 'o', alpha, beta)
matrix[it1][it2] = '_'
alpha = max(val, alpha)
if beta <= alpha: # pruning
return beta
return alpha # return maximum gain which can be achieved
else:
for it1 in range(0, 3):
for it2 in range(0, 3):
if matrix[it1][it2] == '_': # check for empty position on board and generate tree
matrix[it1][it2] = 'o'
val = miniMaxAB(matrix, 'x', alpha, beta)
matrix[it1][it2] = '_'
beta = min(beta, val)
if beta <= alpha: # pruning
return alpha
return beta
"""this functions calls both min max functions and min max with alpha beta pruning functions to get optimal move to play """
def findBest(matrix):
global no_states
global no_statesab
bestVal = float("inf")
bestVal2 = float("inf")
x = -1
y = -1
x2 = -1
y2 = -1
no_states = 0
no_statesab = 0
for it1 in range(0, 3):
for it2 in range(0, 3):
if matrix[it1][it2] == '_':
matrix[it1][it2] = 'o'
moveVal = miniMax(matrix, 'x')
moveVal2 = miniMaxAB(matrix, 'x', -9999, 9999)
matrix[it1][it2] = '_'
if bestVal2 > moveVal2:
x2 = it1
y2 = it2
bestVal2 = moveVal2
if (bestVal > moveVal):
x = it1
y = it2
bestVal = moveVal
print ("No of States Generated by Min Max", no_states)
print ("X and Y values selected by Min Max", x, y)
print ("No of States Generated by Min Max with pruning", no_statesab)
print ("X and Y values selected by Min Max with pruning", x2, y2)
return x, y
"""Printing the Game Board to User properly"""
def printMat(matrix):
print ("-----------------------Matrix-------------------------------")
for it1 in range(0, 3):
print(matrix[it1][0], matrix[it1][1], matrix[it1][2])
print ("------------------------------------------------------------")
"""This is main functions which drives the program"""
if __name__ == '__main__':
matrix = [['_', '_', '_'], ['_', '_', '_'], ['_', '_', '_']]
print ("Human moves represented by x and Computer moves is represented o in matrix")
print ("You are starting player")
val = getVal(matrix)
while val == 0 and checkMoves(matrix):
x = int(input("Enter X Coordniate"))
y = int(input("Enter Y Coordniate"))
if matrix[x][y] == 'x' or matrix[x][y] == 'o':
print ()
print ("Wrong X and Y cordinates enter again")
continue
matrix[x][y] = 'x'
x1, y1 = findBest(matrix)
matrix[x1][y1] = 'o'
printMat(matrix)
val = getVal(matrix)
if (val == 0):
print ("Game is draw")
elif (val == -1):
print ("Computer won the game (dont mind it!) he is unbeatable")
else:
print ("Strange you won the game")