forked from five-in-row-AI/AI_Five-in-row
-
Notifications
You must be signed in to change notification settings - Fork 0
/
game.py
377 lines (322 loc) · 15 KB
/
game.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
#Game Class
#Gomoku Type Games - Lingliang Zhang / Comp Sci Assignment 4
#import relevant classes
from player import Player
from ai import AI
import copy
import time
class Game(object):
def __init__(self):
self.mode, self.length, self.win_con, self.player1, self.player2 = self.startup()
self.active = 0
self.speed = 0
self.learning = 0
#function to determine the game settings
def startup(self):
#intro message
print "/" * 50 + "\nWelcome to Gomoku!\n By Chaofan Yu, Fei Zhao, Qian Wang, Yuntuo Wang\n" + "/"*50 + "\n"
correct = 0
#determine mode
while correct == 0:
#try and except added to prevent breaking on non-numerical inputs
try:
mode = input('What mode would you like to play? \n(1 for Single \
player / 2 for Multiplayer):\n')
if mode != 1 and mode != 2: #check to see if mode is valid
print "Invalid input, please try again"
else: correct = 1
except:
print "Invalid input, please try again."
#choose size of game board
correct = 0
while correct == 0:
try:
length = input('\nHow long do you want the edge of the game board to be? \n(3-10):\n')
if length <= 2 or length >= 11: #check to see if mode is valid
print "Invalid input, please enter a size between 3 and 10."
else: correct = 1
except:
print "Invalid input, please enter a valid size between 3 and 10."
#choose win condition
correct = 0
while correct == 0:
try:
win_con = input('\nHow many in a row should you need to make in a row to win? \
\n(greater than 2, less than or equal to the game board length): \n')
if win_con < 3 or win_con > length: #check to see if mode is valid
print "Invalid input, please enter a length greater than 2, \n\
and less than or equal to the game board length"
else: correct = 1
except:
print "Invalid input, please enter a valid length greater than 2, \n\
and less than or equal to the game board length"
#Give the players appropriate names
if mode == 1:
player1, player2 = Player('you', 'X'), AI('the Computer', 'O')
else:
player1, player2 = Player('Player 1', 'X'), Player('Player 2', 'O')
#return variables
return mode, length, win_con, player1, player2
#function which executes turns
def play_game(self, player1, player2):
self.board = [[x*self.length+(y+1) for y in range(self.length)] for x in range(self.length)]
self.remaining = [i for i in range(1,self.length**2+1)]
self.position_dict = {}
#build the position dictionary
for i in range(1,self.length**2+1):
self.position_dict[i] = ((i-1)/self.length,(i-1)%self.length)
self.draw_board()
#loop to execute turns
for i in range(self.length**2):
#determine whose turn it is
if self.active%2 == 0:
if self.mode == 1:
print "It is your turn." + " (X)"
else:
print "It is Player 1's turn. (X)"
player1.turn(self)
else:
print "It is " + player2.name + "'s turn. " + "(O)"
player2.turn(self)
self.active += 1
self.draw_board()
#check for winners
win_symbol = self.check_winners()
if win_symbol != None:
if win_symbol == 'X':
winner = player1.name + '(X)'
else:
winner = player2.name + '(O)'
if self.mode == 1 and winner == player2.name + '(O)':
print '\n' + '/'*40 + '\nSorry! You lost!\n' + '/'*40 + '\n'
break
else:
print '\n' + '/'*40+ '\n Congratulations! ' + winner.capitalize() +\
' Won!\n' + '/'*40 + '\n'
break
#condition if the game is a tie
if win_symbol == None:
print '\n' + '/'*40 + '\n' + 'The game is a draw.\n' + '/'*40 + '\n'
#function to check if someone has won the game
def check_winners(self):
#build the list through which to check
checklist = []
for i in range(1, self.length+1):
for element in self.pos_in_row(i):
checklist.append(self.count_row(element))
for element in self.pos_in_col(i):
checklist.append(self.count_row(element))
for element in self.pos_in_dia(0):
checklist.append(self.count_row(element))
for element in self.pos_in_dia(1):
checklist.append(self.count_row(element))
for check in checklist:
if check['O'] == self.win_con:
return 'O'
if check['X'] == self.win_con:
return 'X'
#tallies the number of each piece in a row
def count_row(self, row):
row_tally = {'O': 0, 'X': 0, 0: 0}
for position in row:
val = self.board[self.position_dict[position][0]][self.position_dict[position][1]]
if type(val) == int:
val = 0
row_tally[val] += 1
return row_tally
#function which returns list of positions in a row
def pos_in_row(self, row_number):
result = []
for i in range(self.length-self.win_con+1):
result.append(range((row_number-1)*self.length+1+i,(row_number-1)*self.length+1+i+self.win_con))
return result
#function which returns list of positions in a column
def pos_in_col(self, col_number):
result = []
for i in range(self.length-self.win_con+1):
result.append(range(col_number+i*(self.length), self.length*(self.win_con+i)+1, self.length))
return result
#funciton which returns list of positions in a diagonal (0 for left to right or 1 for right to left)
def pos_in_dia(self, dia_number):
x_pos = set([])
y_pos = set([])
result = []
if dia_number == 0:
for row_number in range(1, self.length-self.win_con+2):
x_pos = x_pos.union(set(range((row_number-1)*self.length+1,(row_number-1)*self.length+self.length+1)))
y_pos = y_pos.union(set(range(row_number, self.length**2+1, self.length)))
for start_pos in x_pos.intersection(y_pos):
result.append(range(start_pos,start_pos+(self.win_con-1)*(self.length+1)+1,self.length+1))
if dia_number == 1:
for row_number in range(1, self.length-self.win_con+2):
x_pos = x_pos.union(set(range((row_number-1)*self.length+1,(row_number-1)*self.length+self.length+1)))
for row_number in range(self.length, self.win_con-1, -1):
y_pos = y_pos.union(set(range(row_number, self.length**2+1, self.length)))
for start_pos in x_pos.intersection(y_pos):
result.append(range(start_pos, start_pos + (self.win_con-1)*(self.length-1) + 1,self.length-1))
return result
#function to draw the game board
def draw_board(self):
for row in range(1+self.length*2):
if row%2 == 0:
print '-',
for i in range(self.length):
print '--',
print '-',
print
else:
for column in range(1+self.length*2):
if column%2 ==0:
print '|',
else:
if self.board[row/2][column/2] == 100:
print "100|",
break
elif len(str(self.board[row/2][column/2])) == 1:
print str(self.board[row/2][column/2]) + " ",
else:
print self.board[row/2][column/2],
print
print
#begins the learning routine
def learn(self, player, speed):
old = 0
new = 0
old_ai = AI('Old AI', 'X')
new_ai = AI('New AI', 'O')
self.speed = speed
counter = 0
self.learning = 1
start_time = time.clock()
for i in range(10,0,-1):
counter += 1
new_ai.co1 += i
self.mock_round(old_ai, new_ai, counter)
if self.mock_round(old_ai, new_ai, counter): old = old + 1
else: new += 1
if new_ai.co1 - i > 0:
counter += 1
new_ai.co1 -= i
self.mock_round(old_ai, new_ai, counter)
if self.mock_round(old_ai, new_ai, counter): old = old + 1
else: new += 1
counter += 1
new_ai.co2 += i
self.mock_round(old_ai, new_ai, counter)
if self.mock_round(old_ai, new_ai, counter): old = old + 1
else: new += 1
if new_ai.co2 - i > 0:
counter += 1
new_ai.co2 -= i
self.mock_round(old_ai, new_ai, counter)
if self.mock_round(old_ai, new_ai, counter): old = old + 1
else: new += 1
counter += 1
new_ai.co3 += i
self.mock_round(old_ai, new_ai, counter)
if self.mock_round(old_ai, new_ai, counter): old = old + 1
else: new += 1
if new_ai.co3 - i > 0:
counter += 1
new_ai.co3 -= i
self.mock_round(old_ai, new_ai, counter)
if self.mock_round(old_ai, new_ai, counter): old = old + 1
else: new += 1
counter += 1
new_ai.co3 += i
self.mock_round(old_ai, new_ai, counter)
if self.mock_round(old_ai, new_ai, counter): old = old + 1
else: new += 1
if new_ai.co3 - i > 0:
counter += 1
new_ai.co3 -= i
self.mock_round(old_ai, new_ai, counter)
if self.mock_round(old_ai, new_ai, counter): old = old + 1
else: new += 1
counter += 1
new_ai.co4 += i
self.mock_round(old_ai, new_ai, counter)
if self.mock_round(old_ai, new_ai, counter): old = old + 1
else: new += 1
if new_ai.co4 - i > 0:
counter += 1
new_ai.co4 -= i
self.mock_round(old_ai, new_ai, counter)
if self.mock_round(old_ai, new_ai, counter): old = old + 1
else: new += 1
counter += 1
new_ai.co5 += i
self.mock_round(old_ai, new_ai, counter)
if self.mock_round(old_ai, new_ai, counter): old = old + 1
else: new += 1
if new_ai.co5 - i > 0:
counter += 1
new_ai.co5 -= i
self.mock_round(old_ai, new_ai, counter)
if self.mock_round(old_ai, new_ai, counter): old = old + 1
else: new += 1
player.switch(old_ai)
self.learning = 0
print "new: " + str(new)
print "old: " + str(old)
print """\n%s\n%s\n%s\nFinished learning! %d games in %d seconds.
Final intelligence scores:\n (co1: %d co2: %d, co3: %d, co4: %d, co5: %d)\n%s\n%s\n%s\n""" % ('/'*40,'/'*40,'/'*40, counter,\
time.clock() - start_time,\
player.co1, player.co2, player.co3, player.co4, player.co5,\
'/'*40,'/'*40,'/'*40)
#controller of a mock game
def mock_round(self, old_ai, new_ai, counter):
if self.speed != 3:
print new_ai.co1, new_ai.co2, new_ai.co3, new_ai.co4, new_ai.co5, 'NEW AI Intelligence'
print old_ai.co1, old_ai.co2, old_ai.co3, old_ai.co4, old_ai.co5, 'OLD AI Intelligence'
if self.play_mock_game(old_ai, new_ai, 1, counter) == 'O':
old_ai.switch(new_ai)
# new = new + 1
#else: old = old + 1
#print "new win: " + str(new)
#print "old win: " + str(old)
if self.speed != 3:
print "\n%s\nEnd of game: %d. Current intelligence scores: \n (co1: %d co2: %d, co3: %d, co4: %d, co5: %d).\n%s\n"\
% ('/'*40, counter, old_ai.co1, old_ai.co2, old_ai.co3, old_ai.co4, old_ai.co5, '/'*40)
time.sleep(0.3)
new_ai.switch(old_ai)
return (self.play_mock_game(old_ai, new_ai, 1, counter) == 'O')
#the mock counterpart of play_game(), plays a mock game
def play_mock_game(self, player1, player2, active, counter):
self.board = [[x*self.length+(y+1) for y in range(self.length)] for x in range(self.length)]
self.remaining = [i for i in range(1,self.length**2+1)]
self.position_dict = {}
#build the position dictionary
for i in range(1,self.length**2+1):
self.position_dict[i] = ((i-1)/self.length,(i-1)%self.length)
for i in range(self.length**2):
#determine whose turn it is
if active%2 == 0:
if self.speed == 1:
print "It old AI's turn " + " (X)"
self.draw_board()
time.sleep(0.3)
player1.turn(self)
else:
if self.speed == 1:
print "It is new AI's turn. " + "(O)"
self.draw_board()
time.sleep(0.3)
player2.turn(self)
active += 1
#if self.speed == 1: self.draw_board()
#check for winners
win_symbol = self.check_winners()
if win_symbol != None:
if win_symbol == 'X':
winner = player1.name + '(X)'
else:
winner = player2.name + '(O)'
if self.speed != 3: print "\n%s\n%s Won!\n%s\n" % ('/'*40, winner, '/'*40)
if self.speed == 2: self.draw_board()
return win_symbol
#condition if the game is a tie
if win_symbol == None:
if self.speed != 3: print '\n' + '/'*40 + '\n' + 'The game is a draw.\n' + '/'*40 + '\n'
if self.speed == 2: self.draw_board()
return win_symbol