-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPART-2.py
407 lines (358 loc) · 13.4 KB
/
PART-2.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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
# PART 2:
# This part builds upon Part 1. AI0 makes random moves. AI1 checks if there are winning moves and makes them. AI2 does what AI1 does and also prevents the opponent
# from winning on the next move if possible. We will play these strategies against each other.
# Choose the versus then just sit back and enjoy the show of Tic Tac Toe AI vs AI.
import random;
import numpy as np;
# class to display board and operatte the first player for game
class display:
# display game board bu iterating though each row and column
def display_board(self):
print(" ", end = " ");
for i in range (n):
print(i, end = " ");
print();
for i in range (m):
print(i, end = " ");
for j in range (n):
print(board[i][j], end = " ");
print();
print();
# passing the random int of first player(0 or 1)
def first_player_message(self, first_player):
self.display_board(); # To give the idea of the board of game
# display message for first move according to the random int
if first_player == 0:
print("First player is AI Level 0.");
elif first_player == 1:
print("First player is AI Level 1.");
elif first_player == 2:
print("First player is AI Level 2.");
# same as part 1: cheacking for all possible winning sequences; if not :match tie
class checking:
def check_winner(self, i, j, sign):
#count list
count = [0, 0, 0, 0, 0, 0, 0, 0];
# passing move position with sign and count list
# calcualte is a stand alone function to be used by other classes easily
count = calculate(i, j, sign, count);
# after recieving the count list; summing the vertical, horizontal and diagonal total marks and comparing it with k
# count[0 + 1] = vertical sequence: count[2 + 3] = horizontal sequence: count[4 + 5] = diagonal left: count[6 + 7] = diagonal right
if count[0] + count[1] >= k-1 or count[2] + count[3] >= k-1 or count[4] + count[5] >= k-1 or count[6] + count[7] >= k-1:
return sign;
else:
return self.check_tie();
def check_tie(self):
for i in range(m):
for j in range(n):
if board[i][j] == '_':
return "";
return "tie";
# the strategy for each AI level is build in different methods within the moves class. The class also inherits checking class
class moves(checking):
# AI level 0 makes random moves at available spot
def comp_move_level0(self, sign):
while True:
i = random.randint(0, m - 1);
j = random.randint(0, n - 1);
if board[i][j] == '_':
board[i][j] = sign;
return self.check_winner(i,j,sign);
# AI level 1 tries and makes winning moves
def comp_move_level_1(self, sign):
for i in range(m):
for j in range(n):
if board[i][j] == '_':
count = [0, 0, 0, 0, 0, 0, 0, 0];
count = calculate(i, j, sign, count);
if count[0]+count[1] == k-1 or count[2]+count[3] == k-1 or count[4]+count[5] == k-1 or count[6]+count[7] == k-1:
board[i][j] = sign;
return sign;
return "";
# AI level 2 tries to block the winning move of the opponent and makes the winning moves as well
def comp_move_level_2(self, sign, opp_sign):
for i in range(m):
for j in range(n):
if board[i][j] == '_':
count = [0, 0, 0, 0, 0, 0, 0, 0];
count = calculate(i, j, sign, count);
if count[0]+count[1] == k-1 or count[2]+count[3] == k-1 or count[4]+count[5] == k-1 or count[6]+count[7] == k-1:
board[i][j] = opp_sign;
return sign;
return "";
'''Stand alone function that calcualtes all the winning possibilities'''
# same as PART-1
def calculate(i, j, sign, count):
# vertically up count[0]
if i - k + 1 >= 0:
for x in range(i-1, i-k, -1):
if board[x][j] == sign:
count[0] += 1;
else:
break;
else:
for x in range(i-1, -1, -1):
if board[x][j] == sign:
count[0] += 1;
else:
break;
# vertically down count[1]
if i + k -1 < m:
for x in range(i+1, i+k):
if board[x][j] == sign:
count[1] += 1
else:
break;
else:
for x in range(i + 1, m):
if board[x][j] == sign:
count[1] += 1;
else:
break;
# horizontally left count[2]
if j - k + 1 >= 0:
for x in range(j-1, j-k, -1):
if board[i][x] == sign:
count[2] += 1;
else:
break;
else:
for x in range(j-1, -1, -1):
if board[i][x] == sign:
count[2] += 1;
else:
break;
# horizontally right count[3]
if j + k - 1 < n:
for x in range(j + 1, j + k):
if board[i][x] == sign:
count[3] += 1;
else:
break;
else:
for x in range(j+1,n):
if board[i][x] == sign:
count[3] += 1;
else:
break;
# diagonally left up count[4]
if i - k + 1 >= 0 and j - k + 1 >= 0:
for x,y in zip(range(i-1, i-k, -1),range(j-1, j-k, -1)):
if board[x][y] == sign:
count[4] += 1;
else:
break;
else:
for x,y in zip(range(i-1, -1, -1),range(j-1, -1, -1)):
if board[x][y] == sign:
count[4] += 1;
else:
break;
# diagonally right down count[5]
if i+k-1 < m and j+k-1 < n:
for x,y in zip(range(i+1, i+k),range(j+1, j+k)):
if board[x][y] == sign:
count[5] += 1;
else:
break;
else:
for x,y in zip(range(i+1, m),range(j+1, n)):
if board[x][y] == sign:
count[5] += 1;
else:
break;
# diagonally left down count[6]
if i + k - 1 < m and j - k + 1 >= 0:
for x,y in zip(range(i+1, i+k),range(j-1, j-k, -1)):
if board[x][y] == sign:
count[6] += 1;
else:
break;
else:
for x,y in zip(range(i+1, m),range(j-1, -1, -1)):
if board[x][y] == sign:
count[6] += 1;
else:
break;
# diagonally right up count[7]
if i-k+1 >= 0 and j+k-1 < n:
for x,y in zip(range(i-1,i-k,-1),range(j+1,j+k)):
if board[x][y] == sign:
count[7] += 1;
else:
break;
else:
for x,y in zip(range(i-1,-1,-1),range(j+1,n)):
if board[x][y] == sign:
count[7] += 1;
else:
break;
return count;
'''Each of the 3 VS set cominations are created in 3 different functions (AI1 VS AI0), (AI2 VS AI0) and (AI2 VS AI1)'''
# the way of calling is most important: different for different combinations
# at the entry in the functions, instances of move and checking are created
# at the end: the result stores which Ai won or if tie.
# ai1 vs ai0
def ai1_vs_ai0():
t = moves();
first_player = random.choice([0,1]);
d.first_player_message(first_player);
while True:
if first_player == 0:
result = t.comp_move_level0('0');
d.display_board();
if result == "":
if t.comp_move_level_1('1') == '1':
result = '1';
else:
result = t.comp_move_level0('1');
d.display_board();
elif first_player == 1:
if t.comp_move_level_1('1') == '1':
result = '1';
else:
result = t.comp_move_level0('1');
d.display_board();
if result == "":
result = t.comp_move_level0('0');
d.display_board();
#check which Ai won 0/1/tie
if result == '0':
return result;
elif result == '1':
d.display_board();
return result;
elif result == "tie":
return result;
# ai2 vs ai0
def ai2_vs_ai0():
t = moves();
c = checking();
first_player = random.choice([0,2]);
d.first_player_message(first_player);
while True:
if first_player == 0:
result = t.comp_move_level0('0');
d.display_board();
if result == "":
if t.comp_move_level_1('2') == '2':
result = '2';
elif t.comp_move_level_2('0','2') == '0':
result = c.check_tie();
d.display_board();
else:
result = t.comp_move_level0('2');
d.display_board();
elif first_player == 2:
if t.comp_move_level_1('2') == '2':
result = '2';
elif t.comp_move_level_2('0','2') == '0':
result = c.check_tie();
else:
result = t.comp_move_level0('2');
d.display_board();
if result == "":
result = t.comp_move_level0('0');
d.display_board();
#check which Ai won 0/2/tie
if result == '0':
return result;
elif result == '2':
d.display_board();
return result;
elif result == "tie":
return result;
# ai2 vs ai1
def ai2_vs_ai1():
t = moves();
c = checking();
first_player = random.randint(1,2);
d.first_player_message(first_player);
while True:
if first_player == 1:
if t.comp_move_level_1('1') == '1':
result = '1';
else:
result = t.comp_move_level0('1');
d.display_board();
if result == "":
if t.comp_move_level_1('2') == '2':
result = '2';
elif t.comp_move_level_2('1','2') == '1':
result = c.check_tie();
else:
result = t.comp_move_level0('2');
d.display_board();
elif first_player == 2:
if t.comp_move_level_1('2') == '2':
result = '2';
elif t.comp_move_level_2('1','2') == '1':
result = c.check_tie();
else:
result = t.comp_move_level0('2');
d.display_board();
if result == "":
if t.comp_move_level_1('1') == '1':
result = '1';
else:
result = t.comp_move_level0('1');
d.display_board();
#check which Ai won 2/1/tie
if result == '1':
return result;
elif result == '2':
#d.display_board();
return result;
elif result == "tie":
#d.display_board();
return result;
# infinite loop
while True:
print("New Tic-Tac-Toe match begins."); # First message for the player to see
m = int(input("Enter the number of rows: ")); # the board with m rows
n = int(input("Enter the number of columns: ")); # board with n columns
k = int(input("Enter the length of the sequence of marks arranged horizontally, vertically, or diagonally that constitutes a win: "));
choice = int(input("Enter the type of AI that they will play against each other: 1 for AI1 vs AI0, 2 for AI2 vs AI9 and 3 for AI2 vs AI1: ")); #levels of AI user wants to compete each other.
print();
# instance for display class created
d = display();
# instance for moves class created
t = moves();
# to store the end result for iether of the player's win or tie
result = "";
# board created of m*n dimension
board = np.full((m,n), '_'); # creating standard board of m*n as per case
# if AI level 0
if choice == 1:
result = ai1_vs_ai0();
if result == '1':
print("Level 1 won this time.");
elif result == '0':
print("Level 0 won this time.");
elif result == "tie":
print("Match Tied.");
elif choice == 2:
result = ai2_vs_ai0();
if result == '2':
print("Level 2 won this time.");
elif result == '0':
print("Level 0 won this time.");
elif result == "tie":
print("Match Tied.");
elif choice == 3:
result = ai2_vs_ai1()
if result == '2':
print("Level 2 won this time.");
elif result == '1':
print("Level 1 won this time.");
elif result == "tie":
print("Match Tied.");
# if any other level except 0, 1 and 2: invalid level
else:
print("Invalid Level.");
# replay
c = input("Wanna play again? Enter 'Y' or 'y' to play again, or anything else to quit: ");
if c != 'Y' and c != 'y':
print("Bye!");
break;
print();