forked from tylerxprice/hive-framework
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrandy.py
174 lines (126 loc) · 3.96 KB
/
randy.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
import argparse
import random
import sys
from game import *
class OpeningBook():
"""
Very, very simple opening book to give the bot some direction.
"""
def __init__(self, game):
self.game = game
def findMove(self):
"""
. . .
/ \
. |wG1| .
\ /
. . .
1. wG1
"""
if self.game.turnNumber == 1:
return Move(self.game.currentPlayer.pieces['G1'], Point.NONE, Point(0,0,0))
"""
. . . .
/ \ / \
. |bG1|w__| .
\ / \ /
. . . .
1. w__, 2. bG1 -w__
"""
if self.game.turnNumber == 2:
return Move(self.game.currentPlayer.pieces['G1'], Point.NONE, Point(-1,-1,0))
"""
. . . .
/ \
. . |wQ | .
/ \ / \ /
. |b__|wG1| .
\ / \ /
. . . .
1. wG1, 2. b__ -wG1, 3. wQ wG1/
"""
if self.game.turnNumber == 3:
return Move(self.game.currentPlayer.pieces['Q'], Point.NONE, Point(1,0,0))
class MoveSearch():
WIN_SCORE = 2**15 - 1
def __init__(self, game):
self.game = game
self.horizonDepth = 1
self.bestMove = None
self.heuristic = Heuristic(game)
def findMove(self):
self.minimax(self.horizonDepth, float('-infinity'), float('infinity'))
return self.bestMove
def minimax(self, depth, alpha, beta):
"""
Basic Alpha-Beta min-maxing
"""
val = self.checkWinScore()
if not val == None:
return val
if depth <= 0:
val = self.evaluate()
return val
validMoves = self.game.getValidMoves()
for move in validMoves:
self.game.makeMove(move)
val = -self.minimax(depth - 1, -beta, -alpha)
self.game.unmakeMove(move)
if val >= beta: # our opponent won't let us get to this move, it's too good
return beta
if val > alpha: # yea! a better move that we can get to
alpha = val
if depth == self.horizonDepth:
self.bestMove = move
return alpha
def checkWinScore(self):
winner = self.game.getWinner()
if winner == Game.WINNER_NONE:
return None
signFlip = 1
if self.game.currentPlayer.color == Player.BLACK:
signFlip = -1
if winner == Game.WINNER_DRAW:
return signFlip * MoveSearch.CONTEMPT_FACTOR
return signFlip * (MoveSearch.WIN_SCORE + depth) * winner
def evaluate(self):
signFlip = 1
if self.game.currentPlayer.color == Player.BLACK:
signFlip = -1
return signFlip * self.heuristic.evaluate()
class Heuristic():
"""
Positive => WHITE is winning!
"""
def __init__(self, game):
self.game = game
def evaluate(self):
return random.randrange(-10, 10)
class Randy():
""" A bot that will play random moves in Hive """
def __init__(self, args):
self.args = self._parseArgs(args)
self.game = Game('' , '', self.args['times'], self.args['moves'], self.args['expansions'])
self.player = self.game.currentPlayer
self.bestMove = None
def _parseArgs(self, args):
parser = argparse.ArgumentParser(prog="randy", argument_default='')
parser.add_argument(args[0], default='')
parser.add_argument('--times', default='30000,0,0') # game time, white used, black used (ms)
parser.add_argument('--moves', default='') # 1. wS1, 2. bG1 -wS1, 3. wQ wS1/, ...
parser.add_argument('--expansions', default='') # LMD
args = parser.parse_args(args)
return vars(args)
def run(self):
#self.game.printBoard()
self.bestMove = OpeningBook(self.game).findMove()
if not self.bestMove:
self.bestMove = MoveSearch(self.game).findMove()
self.printMove()
def printMove(self):
if not self.bestMove:
sys.stdout.write('pass')
else:
sys.stdout.write(self.game.getMoveNotation(self.bestMove))
if __name__ == "__main__":
Randy(sys.argv).run()