-
Notifications
You must be signed in to change notification settings - Fork 3
/
gameengine.py
635 lines (572 loc) · 23 KB
/
gameengine.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
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
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
import easyAI
from easyAI.AI.DictTT import DictTT
from easyAI import TT
from copy import copy
import random
from operator import itemgetter
from characters import AI_LIST
from tactics import Tactics
USER = 1
AI = 2
PLAYER_LIST = [USER, AI]
#
# BOARD LAYOUT
#
# 13 12 11 10 09 08 AI
# 14 07
# 01 02 03 04 05 06 USER
#
# HAND = 00
HOUSE_LIST = {
USER: [1, 2, 3, 4, 5, 6],
AI: [8, 9, 10, 11, 12, 13]
}
STORE_IDX = {
USER: 7,
AI: 14
}
HAND = 0
OWNER = 0
NEXT = 1
ROLE = 2
OPP = 3
DISTPIT = 4
HOUSE = 88
STORE = 99
# 13 12 11 10 09 08 AI
# 14 07
# 01 02 03 04 05 06 USER
P = {
1: {OWNER: USER, NEXT: {USER: 2, AI: 2}, ROLE: HOUSE, OPP: 13, DISTPIT: {USER: 6, AI: 12}},
2: {OWNER: USER, NEXT: {USER: 3, AI: 3}, ROLE: HOUSE, OPP: 12, DISTPIT: {USER: 5, AI: 11}},
3: {OWNER: USER, NEXT: {USER: 4, AI: 4}, ROLE: HOUSE, OPP: 11, DISTPIT: {USER: 4, AI: 10}},
4: {OWNER: USER, NEXT: {USER: 5, AI: 5}, ROLE: HOUSE, OPP: 10, DISTPIT: {USER: 3, AI: 9}},
5: {OWNER: USER, NEXT: {USER: 6, AI: 6}, ROLE: HOUSE, OPP: 9, DISTPIT: {USER: 2, AI: 8}},
6: {OWNER: USER, NEXT: {USER: 7, AI: 8}, ROLE: HOUSE, OPP: 8, DISTPIT: {USER: 1, AI: 7}},
7: {OWNER: USER, NEXT: {USER: 8, AI: 8}, ROLE: STORE, OPP: 0, DISTPIT: None},
8: {OWNER: AI , NEXT: {USER: 9, AI: 9}, ROLE: HOUSE, OPP: 6, DISTPIT: {USER: 12, AI: 6}},
9: {OWNER: AI , NEXT: {USER: 10, AI: 10}, ROLE: HOUSE, OPP: 5, DISTPIT: {USER: 11, AI: 5}},
10: {OWNER: AI , NEXT: {USER: 11, AI: 11}, ROLE: HOUSE, OPP: 4, DISTPIT: {USER: 10, AI: 4}},
11: {OWNER: AI , NEXT: {USER: 12, AI: 12}, ROLE: HOUSE, OPP: 3, DISTPIT: {USER: 9, AI: 3}},
12: {OWNER: AI , NEXT: {USER: 13, AI: 13}, ROLE: HOUSE, OPP: 2, DISTPIT: {USER: 8, AI: 2}},
13: {OWNER: AI , NEXT: {USER: 1, AI: 14}, ROLE: HOUSE, OPP: 1, DISTPIT: {USER: 7, AI: 1}},
14: {OWNER: AI , NEXT: {USER: 1, AI: 1}, ROLE: STORE, OPP: 0, DISTPIT: None},
}
ALL_PITS = range(1, 15)
OWN_PITS_FROM_STORE = [
None,
[6, 5, 4, 3, 2, 1],
[13, 12, 11, 10, 9, 8],
]
ACTION = "action"
COUNT = "count"
LOC = "loc"
BALANCE = 0
GREED = 1
CAUTION = 2
EMPTY = 0
FULL = 1
INF = float("inf")
class MancalaAI(easyAI.NonRecursiveNegamax, object):
# class MancalaAI(easyAI.Negamax, object):
def __init__(self, settings, testing=False, tt=None, defender_role=False):
self.settings = settings
self.testing = testing
self.defender_role = defender_role
self.tactics = Tactics()
self.set_character()
super(MancalaAI, self).__init__(self.character['lookahead'], tt=self.tt)
def set_character(self):
if self.testing:
if self.defender_role:
self.character = self.testing[1]
else:
self.character = self.testing[0]
else:
self.character = AI_LIST[self.settings["ai_chosen"]]
self.depth = self.character['lookahead']
self.tactics.remap(self.character, self.settings)
self.tt = None
#
def __call__(self, game):
if self.character['strategy'] == "random":
# this is only used by Maisy
possible_moves = game.possible_moves()
shortest_length = min([len(m) for m in possible_moves])
shortest_moves = [m for m in possible_moves if len(m)==shortest_length]
return random.choice(shortest_moves)
if self.character['error_rate'] > 0.0:
chance = random.random()
if chance < self.character['error_rate']:
possible_moves = game.possible_moves()
return random.choice(possible_moves)
return super(MancalaAI, self).__call__(game)
class KalahHumanPlayer(easyAI.Human_Player):
def get_tactics(self):
return None
class KalahAIPlayer(easyAI.AI_Player):
def set_character(self):
self.AI_algo.set_character()
def get_tactics(self):
return self.AI_algo.tactics
class KalahGame(easyAI.TwoPlayersGame):
def __init__(self, settings, testing=False, verbose=True):
self.trace = []
self.testing = testing
self.settings = settings
self.verbose = verbose
if self.testing:
self.players = [
KalahAIPlayer(MancalaAI(
self.settings,
testing=testing
)),
KalahAIPlayer(MancalaAI(
self.settings,
testing=testing,
defender_role=True
))
]
else:
self.players = [
KalahHumanPlayer(),
KalahAIPlayer(MancalaAI(self.settings))
]
self.set_character()
self.nplayer = self.settings['first_player']
self.animate = []
self.want_animation = False
self.board = [0] * 15
self.reset_board(empty=True)
def set_character(self):
if self.testing:
self.players[0].set_character()
self.players[1].set_character()
self.character = self.testing[0]
else:
self.character = AI_LIST[self.settings["ai_chosen"]]
self.players[1].set_character()
def is_stopping_in_own_store(self, pit):
count = self.board[pit] % 13 # if seeds > 12 then they wrap around board; so modulo 13
return count == P[pit][DISTPIT][self.nplayer]
def ttentry(self):
return tuple(self.board)
def ttrestore(self, entry_tuple):
for index in range(len(self.board)):
self.board[index] = entry_tuple[index]
def possible_moves(self, sorting=False):
move_list = [[pit] for pit in self.possible_moves_choices()]
completed_list = []
if sorting:
self.recurse_moves_sorting(move_list, completed_list)
# completed_list is now a list of tuples; sort and remove scoring
sorted_list = sorted(completed_list, key=itemgetter(0), reverse=True)
completed_list = [move for (score, move) in sorted_list]
else:
self.recurse_moves(move_list, completed_list)
return completed_list
def recurse_moves(self, move_list, completed_list):
for move in move_list:
last_pit = move[-1]
if self.is_stopping_in_own_store(last_pit):
board_copy = copy(self.board)
self.make_move_choice(last_pit)
more_choices = self.possible_moves_choices()
if more_choices:
next_todo = []
for pit in more_choices:
next_todo.append(move + [pit])
self.recurse_moves(next_todo, completed_list)
else:
completed_list.append(move)
self.board = board_copy
else:
completed_list.append(move)
def recurse_moves_sorting(self, move_list, completed_list):
for move in move_list:
board_copy = copy(self.board)
last_pit = move[-1]
last_pit_flag = self.is_stopping_in_own_store(last_pit)
self.make_move_choice(last_pit)
if last_pit_flag:
more_choices = self.possible_moves_choices()
if more_choices:
next_todo = []
for pit in more_choices:
next_todo.append(move + [pit])
self.recurse_moves_sorting(next_todo, completed_list)
else:
score = self.tactical_scoring(self.nplayer, self.nopponent)
completed_list.append((score, move))
else:
score = self.tactical_scoring(self.nplayer, self.nopponent)
completed_list.append((score, move))
self.board = board_copy
def possible_moves_choices(self):
possible = []
for house in HOUSE_LIST[self.nplayer]:
if self.board[house]:
possible.append(house)
return possible
def animated_play_move(self, move):
self.want_animation = True
self.play_move(move)
self.want_animation = False
def make_move(self, move):
self.animate = []
for pit in move:
self.make_move_choice(pit)
def make_move_choice(self, house):
if self.want_animation:
self.animate.append({ACTION: "normal_move", LOC: house})
self.animate.append({ACTION: "scoop", LOC: house})
#
# scoop up the house chosen
#
# self._scoop(house)
self.board[HAND] = self.board[house]
self.board[house] = 0
current_house = house
#
# drop the seeds into the pits
#
for ctr in range(self.board[HAND]):
next_house = P[current_house][NEXT][self.nplayer]
# self._drop(next_house)
self.board[HAND] -= 1
self.board[next_house] += 1
if self.want_animation:
self.animate.append({ACTION: "drop", LOC: next_house, COUNT: 1})
current_house = next_house
#
# capture if possible
#
if self.settings['capture_rule'] == 0: # capture if opposite is full
if self.board[current_house] == 1:
# note: A 'store' OPP is always the empty hand, so it always fails
if self.board[P[current_house][OPP]]:
if P[current_house][OWNER] == self.nplayer:
# see note above; if P[current_house][ROLE] == HOUSE:
if self.want_animation:
self.animate.append({ACTION: "steal"})
self._scoop(P[current_house][OPP])
self._scoop(current_house)
self._drop_all(STORE_IDX[self.nplayer])
elif self.settings['capture_rule'] == 1: # capture even if opposite is empty
if self.board[current_house] == 1:
if P[current_house][OWNER] == self.nplayer:
if P[current_house][ROLE] == HOUSE:
if self.want_animation:
self.animate.append({ACTION: "steal"})
if self.board[P[current_house][OPP]]:
self._scoop(P[current_house][OPP])
self._scoop(current_house)
self._drop_all(STORE_IDX[self.nplayer])
# elif settings['capture_rule'] == 2: # no capture
# pass
#
# end of game scooping
#
if self.is_over():
if self.want_animation:
self.animate.append({ACTION: "game_over"})
if self.settings['eog_rule'] == 0:
# traditional end-of-game handling: both players scoop own houses into store
for player in PLAYER_LIST:
for house in HOUSE_LIST[player]:
if self.board[house]:
self._scoop(house)
if self.board[HAND]:
self._drop_all(STORE_IDX[player])
elif self.settings['eog_rule'] == 1:
# put seed in store of player who does not have seeds
if any([self.board[house] for house in HOUSE_LIST[USER]]):
empty_player = AI
else:
empty_player = USER
for player in PLAYER_LIST:
for house in HOUSE_LIST[player]:
if self.board[house]:
self._scoop(house)
if self.board[HAND]:
self._drop_all(STORE_IDX[empty_player])
elif self.settings['eog_rule'] == 2:
# put seeds in store of player who ended game (current player)
for player in PLAYER_LIST:
for house in HOUSE_LIST[player]:
if self.board[house]:
self._scoop(house)
if self.board[HAND]:
self._drop_all(STORE_IDX[self.nplayer])
elif self.settings['eog_rule'] == 3:
pass
# leave seeds alone
# # just place in hand for proper scoring; don't animate this ever
# temp = self.want_animation
# self.want_animation = False
# for player in PLAYER_LIST:
# for house in HOUSE_LIST[player]:
# if self.board[house]:
# self._scoop(house)
# self.want_animation = temp # restore
# def is_over(self):
# for player in PLAYER_LIST:
# has_seed = False
# for house in HOUSE_LIST[player]:
# if self.board[house]:
# has_seed = True
# if has_seed is False:
# return True
# return False
def is_over(self):
if not any(self.board[1:7]): # check for seeds in player side
return True
if not any(self.board[8:14]): # check for seeds on AI side
return True
return False
def show(self, full=False):
print "player: {} with score {}".format(self.nplayer, self.scoring())
if full:
print " tactical: "
for line in self.trace:
print " {}".format(line)
print "hand: {}".format(self.board[HAND])
print "board:\n"
print " 13 12 11 10 09 08 AI"
print " " + " ".join(
["[{:02d}]".format(self.board[pit]) for pit in reversed(HOUSE_LIST[AI])]
)
print " [{:02d}] [{:02d}]".format(
self.board[STORE_IDX[AI]], self.board[STORE_IDX[USER]]
)
print " " + " ".join(
["[{:02d}]".format(self.board[pit]) for pit in HOUSE_LIST[USER]]
)
print " 01 02 03 04 05 06 USER"
def scoring(self, player=None):
if self.is_over():
s = self.strategic_scoring(self.nplayer, self.nopponent)
if s > 0:
t = 9000
else:
t = -9000
else:
if self.character['fitness'] == "caution":
s = self.caution_scoring(player)
elif self.character['fitness'] == "greed":
s = self.greed_scoring(player)
else:
s = self.strategic_scoring(self.nplayer, self.nopponent)
t = self.tactical_scoring(self.nplayer, self.nopponent)
return s + t
def strategic_scoring(self, player, opponent):
raw_score = self.board[STORE_IDX[player]] - self.board[STORE_IDX[opponent]]
return raw_score * 1000
def caution_scoring(self, player):
raw_score = self.board[STORE_IDX[USER]]
if self.player==AI:
raw_score *= -1
return raw_score * 1000
def greed_scoring(self, player):
raw_score = self.board[STORE_IDX[AI]]
if self.player==USER:
raw_score *= -1
return raw_score * 1000
def tactical_scoring(self, player, opponent):
'''
Measure the relative value of a board layout in terms
of short-term tactics.
Because we are using MiniMax (Negamax), it is critical that
the measure be zero sum. A advantage for one player must
exactly equal the disadvantage fo the other player.
'''
# leaving empty pits on own side
# if True: self.trace = []
if self.character['tactics'] == "blind":
# if True: self.trace.append("+0 BLIND TACTICS")
return 0
tactics = self.players[1].get_tactics()
if not tactics:
return 0
score = 0
for dist, pit in enumerate(OWN_PITS_FROM_STORE[player]):
if self.board[pit]==0:
opp_count = self.board[P[pit][OPP]]
if opp_count:
score += tactics.empty_pit_value[dist][FULL] * opp_count
# if True: self.trace.append("+{} OWN EMPTY/FULL PIT {} cnt={}".format(tactics.empty_pit_value[dist][FULL] * opp_count, pit, opp_count))
else:
score += tactics.empty_pit_value[dist][EMPTY]
# if True: self.trace.append("+{} OWN EMPTY/EMPTY PIT {}".format(tactics.empty_pit_value[dist][EMPTY], pit))
for dist, pit in enumerate(OWN_PITS_FROM_STORE[opponent]):
if self.board[pit]==0:
opp_count = self.board[P[pit][OPP]]
if opp_count:
score -= tactics.empty_pit_value[dist][FULL] * opp_count
# if True: self.trace.append("-{} OPP EMPTY/FULL PIT {} cnt={}".format(tactics.empty_pit_value[dist][FULL] * opp_count, pit, opp_count))
else:
score -= tactics.empty_pit_value[dist][EMPTY]
# if True: self.trace.append("-{} OPP EMPTY/EMPTY PIT {}".format(tactics.empty_pit_value[dist][EMPTY], pit))
# easy repeat patterns seen
for dist, pit in enumerate(OWN_PITS_FROM_STORE[player]):
if self.board[pit] == (dist + 1):
score += tactics.easy_repeat_value[dist]
# if True: self.trace.append("+{} OWN EASY REPEAT AT {}".format(tactics.easy_repeat_value[dist], pit))
for dist, pit in enumerate(OWN_PITS_FROM_STORE[opponent]):
if self.board[P[pit][OPP]] == (dist + 1):
score -= tactics.easy_repeat_value[dist]
# if True: self.trace.append("-{} OPP EASY REPEAT AT {}".format(tactics.easy_repeat_value[dist], pit))
return score
# def unmake_move(self, move):
# pass
def reset_board(self, restoration=False, empty=False):
self.want_animation = True
#
# manipulate basic settings
#
self.seeds_per_house = self.settings['seeds_per_house']
#
# manipulate board
#
if self.want_animation:
self.animate = [{ACTION: "setting_up"}]
# determine new board
if restoration:
new_board = copy(self.board)
self.board = [12 * self.seeds_per_house] + [0] * 14
else:
if empty:
self.board = [12 * self.seeds_per_house] + [0] * 14
new_board = copy(self.board)
else:
new_board = [0] + \
[self.seeds_per_house] * 6 + [0] + \
[self.seeds_per_house] * 6 + [0]
for pit in ALL_PITS:
if self.board[pit]:
self._scoop(pit)
for pit in ALL_PITS:
if new_board[pit]:
self._drop(pit, count=new_board[pit])
self.board[HAND] = 0 # this is for error recovery. In theory, you
# should never needs this as the hand will be
# empty already.
#
# randomly move seeds if that is called for
#
if not restoration:
if self.settings['randomness_rule'] == 2:
from_pit = random.choice(HOUSE_LIST[AI])
to_pit = random.choice([h for h in HOUSE_LIST[AI] if h!=from_pit])
leave = self.board[from_pit] - 1
#
self.animate.append({ACTION: "random_seed", LOC: from_pit})
self._scoop(from_pit)
self._drop(from_pit, count=leave)
self._drop(to_pit)
#
from_pit = random.choice(HOUSE_LIST[USER])
to_pit = random.choice([h for h in HOUSE_LIST[USER] if h!=from_pit])
leave = self.board[from_pit] - 1
#
self.animate.append({ACTION: "random_seed", LOC: from_pit})
self._scoop(from_pit)
self._drop(from_pit, count=leave)
self._drop(to_pit)
#
# manipulate AI character
#
self.set_character()
#
# set nplayer
#
if not restoration:
self.nplayer = self.settings['first_player']
self.want_animation = False
def _scoop(self, pit):
self.board[HAND] += self.board[pit]
self.board[pit] = 0
if self.want_animation:
self.animate.append({ACTION: "scoop", LOC: pit})
def _drop(self, pit, count=1):
self.board[HAND] -= count
self.board[pit] += count
if self.want_animation:
self.animate.append({ACTION: "drop", LOC: pit, COUNT: count})
def _drop_all(self, pit):
count = self.board[HAND]
self.board[pit] += self.board[HAND]
self.board[HAND] = 0
if self.want_animation:
self.animate.append({ACTION: "drop_all", LOC: pit, COUNT: count})
def get_winner(self):
user_score = self.strategic_scoring(USER, AI)
if user_score > 0:
return USER
if user_score < 0:
return AI
return 0
def get_animation(self):
return self.animate
def usermove_start_simulation(self):
self.want_animation = True
self.animate = []
self.original_board = copy(self.board)
def usermove_simulate_choice(self, choices_so_far):
self.animate = []
current_choice = choices_so_far[-1]
self.make_move_choice(current_choice)
def usermove_finish_simulation(self):
self.animate = []
self.board = self.original_board
self.want_animation = False
if __name__=="__main__":
settings = {
"ai_chosen": 11,
"who_plays_first": 1,
"first_player": AI,
"seeds_per_house_selection": 1,
"seeds_per_house": 4,
"capture_rule": 0,
"eog_rule": 0,
"randomness_rule": 0,
"seed_drop_rate": 0.4,
}
character = AI_LIST[settings['ai_chosen']]
game = KalahGame(settings)
game.reset_board()
one_play_test = True
if one_play_test:
# reference:
#
# time python gameengine.py
# One move test
# POSS MOVES [[8], [9], [10, 8], [10, 9], [10, 11], [10, 12], [10, 13], [11], [12], [13]]
# AI plays [10, 13]
# real 0m1.990s
# user 0m1.980s
# sys 0m0.008s
print "One move test"
print "POSS MOVES", game.possible_moves()
move = game.get_move()
print "AI plays", move
else:
while not game.is_over():
# print game.animate
game.show(full=True)
if game.nplayer==USER:
poss = game.possible_moves()
for index, move in enumerate(poss):
print index, move
index = int(input("enter move:"))
move = poss[index]
else:
move = game.get_move()
print "AI plays", move
game.play_move(move)
print
print "GAME OVER!"
print
print game.show()
print
print "RESULT:", ["TIE", "USER WON", "AI WON"][game.get_winner()]