forked from mrda/junkcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathollehto.py
More file actions
executable file
·566 lines (469 loc) · 18.9 KB
/
Copy pathollehto.py
File metadata and controls
executable file
·566 lines (469 loc) · 18.9 KB
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
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
#!/usr/bin/env python
#
# ollehto.py - play the game of ollehto
#
# Copyright (C) 2015 Michael Davies <michael@the-davies.net>
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License as
# published by the Free Software Foundation; either version 2 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
# 02111-1307, USA.
DOT = '.'
FREE = ' '
DARK = 'X'
LIGHT = 'O'
HUMAN = 'human'
ROBOT = 'robot'
import inspect
import random
import textmenu
class Cell(object):
def __init__(self, row, col):
self.row = row
self.col = col
self.owner = FREE
def isfree(self):
return self.owner == FREE
def __str__(self):
if self.isfree():
s = " is free"
else:
s = " is owned by %s" % self.owner
return "Cell at (%d, %d) %s" % (self.row, self.col, s)
class Board(object):
def __init__(self, rows=8, cols=8):
self.row_size = rows
self.col_size = cols
self.clear_grid()
def set(self, row, col, owner):
self.grid[row][col].owner = owner
def initialise_board(self):
"""Initialise the board to the starting positions"""
self.set(3, 3, LIGHT)
self.set(4, 4, LIGHT)
self.set(3, 4, DARK)
self.set(4, 3, DARK)
def clear_grid(self, grid=None):
if grid:
grid = [[Cell(row, col) for col in range(grid.col_size)]
for row in range(grid.row_size)]
else:
self.grid = [[Cell(row, col) for col in range(self.col_size)]
for row in range(self.row_size)]
def dump(self):
b = '+' + '-------' * self.col_size + '+' "\n"
for row in range(self.row_size):
b += '|'
for col in range(self.col_size):
b += self.grid[row][col].coords_str() + " "
b += '|' + "\n"
b += '+' + '-------' * self.col_size + '+' "\n"
return b
def _centre(self, st, width=4):
return st.center(width, ' ')
def format_coords(self, row, col):
return('%s%s' % (chr(65+row), col))
def __str__(self, curr_row=None, curr_col=None, include_score=True):
output = ' '
for r in range(self.row_size):
output += self._centre(str(r))
output += '\n'
output += ' '
output += '+' + '---+' * self.col_size + '\n'
for row in range(self.row_size):
output += self._centre(chr(65+row))
output += '|'
for col in range(self.col_size):
cell = self.grid[row][col]
output += ' ' + cell.owner + ' '
output += '|'
output += '\n'
output += ' '
output += '+'
for col in range(self.col_size):
cell = self.grid[row][col]
output += '---+'
output += '\n'
if include_score:
s = 'Score: White=%d. Black=%d' % (self.score(LIGHT),
self.score(DARK))
output += self._centre(s, 38)
output += '\n'
return output
def is_on_board(self, row, col):
"""Check to see that the row, col is on this board"""
# Ignore row where off the board
if row < 0 or row >= self.row_size:
return False
# Ignore col where off the board
if col < 0 or col >= self.col_size:
return False
return True
def get_opponent(self, colour):
if colour == DARK:
return LIGHT
return DARK
def copy_board(self):
b = Board(self.row_size, self.col_size)
for row in range(self.row_size):
for col in range(self.col_size):
b.set(row, col, self.grid[row][col].owner)
return b
def build_possibilities_board(self, candidates):
cand_board = self.copy_board()
for cand in candidates:
cand_board.set(cand['row'], cand['col'], DOT)
return cand_board
def find_all_valid_moves(self, colour):
slots = []
for row in range(self.row_size):
for col in range(self.col_size):
cell = self.grid[row][col]
if cell.owner == colour:
cell_cands = (
self.find_valid_moves_for_cell(row, col, colour))
slots.extend(cell_cands)
return slots
def find_valid_moves_for_cell(self, row, col, colour):
candidate_list = []
for dr in [-1, 0, 1]:
for dc in [-1, 0, 1]:
# Ignore the zero transform
if dr == 0 and dc == 0:
continue
# Keep searching in this direction until we find
# that this spot is invalid, or is valid
dir_result = self.valid_moves_for_cell_in_direction(row,
col,
dr,
dc,
colour)
if dir_result is not None:
candidate_list.append(dir_result)
return candidate_list
def valid_moves_for_cell_in_direction(self, row, col, dr, dc, colour):
opponent = self.get_opponent(colour)
cand_row = row
cand_col = col
flips = []
while True:
cand_row += dr
cand_col += dc
if not self.is_on_board(cand_row, cand_col):
return None
elif self.grid[cand_row][cand_col].owner == FREE:
if len(flips) > 0:
result = {'row': cand_row,
'col': cand_col,
'count': len(flips),
'flips': flips}
return result
else:
return None
elif self.grid[cand_row][cand_col].owner == opponent:
flips.append((cand_row, cand_col))
elif self.grid[cand_row][cand_col].owner == colour:
return None
def _print_candidates(self, candidates):
for c in candidates:
print("Possible candidates are: %s with count %d" %
(self.format_coords(c['row'], c['col']), c['count']))
print(" and they are:")
st = ""
for f in c['flips']:
st += self.format_coords(f[0], f[1]) + " "
print(" " + st)
def is_valid_move(self, row, col, colour):
candidates = self.find_all_valid_moves(colour)
for candidate in candidates:
if candidate['row'] == row and candidate['col'] == col:
return True
return False
def score(self, colour):
count = 0
for row in range(self.row_size):
for col in range(self.col_size):
if self.grid[row][col].owner == colour:
count += 1
return count
def find_flips_in_direction(self, row, col, dr, dc, colour):
""" Search in dr, dc direction from row, col and find all the flips
required """
opponent = self.get_opponent(colour)
flips = []
cand_row = row
cand_col = col
while True:
cand_row += dr
cand_col += dc
if not self.is_on_board(cand_row, cand_col):
return None
elif self.grid[cand_row][cand_col].owner == FREE:
return None
elif self.grid[cand_row][cand_col].owner == opponent:
flips.append((cand_row, cand_col))
elif self.grid[cand_row][cand_col].owner == colour:
return flips
def find_flips(self, row, col, colour):
""" Find the tokens that need to flip as a result of player
placing a token at row, col"""
all_flips = []
for dr in [-1, 0, 1]:
for dc in [-1, 0, 1]:
# Ignore the zero transform
if dr == 0 and dc == 0:
continue
flips = self.find_flips_in_direction(row, col, dr, dc, colour)
if flips is not None:
all_flips.extend(flips)
return all_flips
def make_move(self, row, col, colour):
self.grid[row][col].owner = colour
flips = self.find_flips(row, col, colour)
for f in flips:
self.grid[f[0]][f[1]].owner = colour
class Player(object):
def __init__(self, name, colour):
self.name = name
self.colour = colour
def make_move(self, board):
# http://norvig.com/python-iaq.html
caller = inspect.getouterframes(inspect.currentframe())[1][3]
raise NotImplementedError(caller + ' must be implemented in subclass')
class Human(Player):
def __init__(self, name, colour):
self.kind = HUMAN
super(Human, self).__init__(name, colour)
def make_move(self, board):
valid_moves = board.find_all_valid_moves(self.colour)
if len(valid_moves) == 0:
print("Sorry %s, there are no valid moves for you. "
"You have to skip" % self.name)
return True
row, col = self.get_user_input(board, valid_moves)
board.make_move(row, col, self.colour)
return False
def get_user_input(self, board, valid_moves):
input_str = (
"%s, you're playing %s and it's your turn. Enter your move: "
% (self.name, self.colour))
possibility_board = board.build_possibilities_board(valid_moves)
valid_move = False
while not valid_move:
print possibility_board
raw_move = raw_input(input_str).upper()
if raw_move is None or raw_move == '':
continue
if len(raw_move) != 2:
print("*** That's not a valid move, moves look like 'A3'")
continue
move = raw_move.strip()
try:
row = ord(move[0]) - 65 # 'A' == 65
col = int(move[1])
except ValueError:
# Don't care what the problem is, the entered
# characters just aren't valid
row = -1
col = -1
if board.is_valid_move(row, col, self.colour):
valid_move = True
else:
print "Sorry, '%s' is an invalid move" % raw_move
return row, col
class Robot(Player):
def __init__(self, name, colour, display):
self.kind = ROBOT
self.display = display
super(Robot, self).__init__(name, colour)
class SimpleRobot(Robot):
def __init__(self, colour, name=None, display=True, randomise=False):
self.randomise = randomise
if name is None:
name = 'Stewart'
super(SimpleRobot, self).__init__(name, colour, display)
def make_move(self, board):
poss_moves = []
for move in board.find_all_valid_moves(self.colour):
if len(poss_moves) == 0:
poss_moves = [move]
else:
if move['count'] > poss_moves[0]['count']:
poss_moves = [move]
elif move['count'] == poss_moves[0]['count']:
poss_moves.append(move)
if len(poss_moves) == 0:
if self.display:
print("%s must skip a move :-(" % self.name)
return True # Must skip a move
else:
if self.randomise:
chosen_move = random.choice(poss_moves)
else:
chosen_move = poss_moves[0]
if self.display:
print("%s has decided to move to %s" %
(self.name,
board.format_coords(chosen_move['row'],
chosen_move['col'])))
board.make_move(chosen_move['row'], chosen_move['col'],
self.colour)
return False
class SimpleRandomRobot(SimpleRobot):
def __init__(self, colour, name=None, display=True):
if name is None:
name = 'Penny'
super(SimpleRandomRobot, self).__init__(colour, name, display,
randomise=True)
class Game(object):
def __init__(self, player1, player2, output=True):
self.b = Board()
self.b.initialise_board()
self.player1 = player1
self.player2 = player2
self.output = output
def print_valid_moves(self, colour, valid_moves):
for move in valid_moves:
coords = self.b.format_coords(move['row'], move['col'])
print("Player %s can choose %s which has a count of %d" %
(colour, coords, move['count']))
def _debug_current_moves(self, board):
for colour in (DARK, LIGHT):
valid_moves = self.b.find_all_valid_moves(colour)
self.print_valid_moves(colour, valid_moves)
def end_of_game_message(self, winner, player1_score, player2_score,
number_of_moves):
print "\n***** GAME OVER *****"
s1 = max(player1_score, player2_score)
s2 = min(player1_score, player2_score)
if winner is not None:
report = ("Congratulations %s, you won %d to %d\n"
% (winner.name, s1, s2))
else:
report = "Wow, a draw! Congratulations to both players!\n"
report += "(The game took %d moves)\n" % number_of_moves
return report
def determine_winner(self, player1_score, player2_score):
if player1_score > player2_score:
winning_player = self.player1
elif player2_score > player1_score:
winning_player = self.player2
else:
winning_player = None
return winning_player
def play_game(self):
still_playing = True
player1_skip = False
player2_skip = False
number_of_moves = 0
while still_playing:
player1_skip = self.player1.make_move(self.b)
if not player1_skip:
number_of_moves += 1
if player1_skip and player2_skip:
break
player2_skip = self.player2.make_move(self.b)
if not player2_skip:
number_of_moves += 1
if player1_skip and player2_skip:
break
player1_score = self.b.score(self.player1.colour)
player2_score = self.b.score(self.player2.colour)
winner = self.determine_winner(player1_score, player2_score)
if self.output:
print self.end_of_game_message(winner, player1_score,
player2_score, number_of_moves)
print self.b.__str__(include_score=False)
return winner, player1_score, player2_score, number_of_moves
def play_single_player_game():
print("\nStarting a one player game\n")
while True:
name = raw_input("Please enter your name: ")
if name is not None or name != "":
break
player1 = Human(name, DARK)
player2 = SimpleRandomRobot(LIGHT)
g = Game(player1, player2)
winner, player1_score, player2_score, number_of_moves = g.play_game()
def play_two_player_game():
print("\nStarting a two player game\n")
while True:
name_1 = raw_input("Please enter the name of player 1: ")
if name_1 is not None or name_1 != "":
break
while True:
name_2 = raw_input("Please enter the name of player 2: ")
if name_2 is not None or name_2 != "":
break
player1 = Human(name_1, DARK)
player2 = Human(name_2, LIGHT)
g = Game(player1, player2)
winner, player1_score, player2_score, number_of_moves = g.play_game()
def play_computer_vs_computer(output=True):
if output:
print("\nPlaying computer vs computer\n")
player1 = SimpleRandomRobot(DARK, display=False)
player2 = SimpleRobot(LIGHT, display=False)
g = Game(player1, player2, output)
winner, player1_score, player2_score, number_of_moves = g.play_game()
return winner, player1_score, player2_score, number_of_moves
def play_many_comp_vs_comp():
number_games = 100
result = {}
for i in range(number_games):
winner, player1_score, player2_score, number_of_moves = (
play_computer_vs_computer(output=False))
if winner is None:
# Draw
if 'Draw' not in result:
result['Draw'] = (1, player1_score, player2_score,
number_of_moves)
else:
wins = result['Draw'][0] + 1
score1 = result['Draw'][1] + player1_score
score2 = result['Draw'][2] + player2_score
moves = result['Draw'][3] + number_of_moves
result['Draw'] = (wins, player1_score, player2_score,
number_of_moves)
continue
if winner.name not in result:
result[winner.name] = (1, player1_score, player2_score,
number_of_moves)
else:
wins = result[winner.name][0] + 1
score1 = result[winner.name][1] + player1_score
score2 = result[winner.name][2] + player2_score
moves = result[winner.name][3] + number_of_moves
result[winner.name] = (wins, score1, score2, moves)
print("%-20s\t\t%s\t%s\t%s\t%s\t%s\t%s" % ('Player', 'Wins', 'For',
'Agst', '% For', '% Agst', 'Moves/Win'))
print("%-20s\t\t%s\t%s\t%s\t%s\t%s\t%s" % ('~~~~~~', '~~~~', '~~~',
'~~~~', '~~~~~', '~~~~~~', '~~~~~~~~~'))
for player in result:
wins, points_for, points_against, moves = result[player]
tot_points = 1.0 * points_for + points_against
print("%-20s\t\t%d\t%d\t%d\t%.2f\t%.2f\t%.2f" %
(player, wins, points_for, points_against,
points_for/tot_points, points_against/tot_points,
1.0*moves/wins))
if __name__ == '__main__':
menu = textmenu.TextMenu('ollehto - Main Menu')
menu.add_exit('q', "Press 'q' to exit")
menu.add_option('1', 'Play a one person game', play_single_player_game)
menu.add_option('2', 'Play a two person game', play_two_player_game)
menu.add_option('c', 'Play a random computer vs computer game',
play_computer_vs_computer)
menu.add_option('r', 'Play many computer vs computer games',
play_many_comp_vs_comp)
menu.start_menu()
print("\nThanks for playing!")