Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 3666555

Browse files
Merge pull request #1 from Suraj1127/master
Fixes bugs in Alpha Beta pruning
2 parents 2d09e9d + 4bbf337 commit 3666555

File tree

3 files changed

+50
-43
lines changed

3 files changed

+50
-43
lines changed

‎Board.py‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -395,7 +395,6 @@ def movable_tigers(self):
395395
"""
396396
Returns the number of movable tigers on the board
397397
"""
398-
399398
return sum(int(self._movable(t)) for t in self.tigerPos)
400399

401400
def generate_move_list(self, rdm=True):

‎Engine.py‎

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ def evaluate(self, depth=0):
2121
"""
2222
winner = self.board.winner
2323
if not winner:
24-
return 3 * self.board.movable_tigers() + 7 * self.board.deadGoats - depth
24+
return 30 * self.board.movable_tigers() + 70 * self.board.deadGoats - depth
2525

2626
if winner == Board.Player.G:
2727
return -Engine.INF
@@ -37,49 +37,56 @@ def minmax(self, is_max=True, depth=0, alpha=-INF, beta=INF):
3737

3838
# find the minimum attainable value for the minimizer
3939
if not is_max:
40+
value = self.INF
4041
for move in self.board.generate_move_list():
4142
# first make the move
4243
self.board.make_move(move)
4344

4445
# go deeper in the search tree recursively
45-
value = self.minmax(True, depth + 1, alpha, beta)
46+
value_t = self.minmax(True, depth + 1, alpha, beta)
4647

47-
if value <= beta:
48-
beta = value
48+
beta = min(beta, value_t)
49+
50+
51+
if value_t <= value:
52+
value = value_t
53+
beta = min(beta, value)
4954
if depth == 0:
5055
self.best_move = move
5156

5257
# then revert the move
5358
self.board.revert_move(move)
5459

55-
# ab pruning
5660
if alpha >= beta:
57-
returnbeta
61+
break
5862

59-
return beta
63+
return value
6064

6165
# find the maximum attainable value for the maximizer
6266
else:
67+
value = -self.INF
6368
for move in self.board.generate_move_list():
6469
# first make the move
6570
self.board.make_move(move)
6671

6772
# go deeper in the search tree recursively
68-
value = self.minmax(False, depth + 1, alpha, beta)
73+
value_t = self.minmax(False, depth + 1, alpha, beta)
6974

70-
if value >= alpha:
71-
alpha = value
75+
if value_t >= value:
76+
value = value_t
77+
alpha = max(alpha, value)
7278
if depth == 0:
7379
self.best_move = move
7480

81+
82+
7583
# then revert the move
7684
self.board.revert_move(move)
7785

78-
# ab pruning
79-
if alpha >= beta:
80-
return alpha
86+
if alpha > beta:
87+
break
8188

82-
return alpha
89+
return value
8390

8491
def best_tiger_move(self):
8592
self.minmax()

‎ui.py‎

100644100755
Lines changed: 29 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
#!/usr/bin/env python3
2+
13
import os
24
import configparser
35
import itertools
@@ -17,7 +19,6 @@ class UIGame(object):
1719
_idx_to_col = {0: 'A', 1: 'B', 2: 'C', 3: 'D', 4: 'E'}
1820

1921
def __init__(self, canvas, statustext):
20-
super(UIGame, self).__init__()
2122
# self.board = Board('1GG1G/1GGGT/1GGGG/GGTGG/GTGTG t g0 c3 mA3')
2223
self.board = Board()
2324

@@ -28,22 +29,22 @@ def __init__(self, canvas, statustext):
2829
self.from_idx = None
2930

3031
self.tiger_radius = 7
31-
self.sheep_radius = 5
32+
self.goat_radius = 5
3233

3334
self.board_grid_x = [10, 60, 110, 160, 210]
3435
self.board_grid_y = [10, 60, 110, 160, 210]
3536
self.board_rect = [1, 1, 221, 221]
3637

3738
self.game = None
3839
self.win = ''
39-
self.ai_turn = False
40+
self.ai_turn = True
4041

41-
self.config = configparser.SafeConfigParser(defaults={
42-
'sheepcolor': 'gray',
42+
self.config = configparser.ConfigParser(defaults={
43+
'goatcolor': 'gray',
4344
'tigercolor': 'yellow'
4445
})
4546
self.config.add_section('ui')
46-
self.sheep_color = self.config.get('ui', 'sheepcolor')
47+
self.goat_color = self.config.get('ui', 'goatcolor')
4748
self.tiger_color = self.config.get('ui', 'tigercolor')
4849

4950
self.draw_board()
@@ -192,7 +193,7 @@ def draw(self):
192193
self.make_ai_move()
193194

194195
tr = self.tiger_radius
195-
sr = self.sheep_radius
196+
sr = self.goat_radius
196197

197198
# display the tigers and goats on the ui board
198199
for entry, (y, x) in zip(Board._get_full_position(self.board.position.split()[0]),
@@ -206,7 +207,7 @@ def draw(self):
206207
elif entry == "G":
207208
self.cids.append(self.canvas.create_oval(x - sr, y - sr,
208209
x + sr, y + sr,
209-
fill=self.sheep_color))
210+
fill=self.goat_color))
210211

211212
def check_win(self):
212213
# read the current board position
@@ -217,48 +218,48 @@ def check_win(self):
217218
self.win = 'tigers'
218219
return
219220

220-
elif self.board.winner == Board.Player.T:
221+
elif self.board.winner == Board.Player.G:
221222
# self.game.wait()
222223
# self.game = None
223224
self.statustext.set('Goats win!')
224225
self.win = 'goats'
225226
return
226227

227228
def new(self):
228-
# self.config = configparser.SafeConfigParser()
229-
# self.config.read(os.path.expanduser('uiconf'))
230-
231-
# if self.config.has_option('game', 'ai'):
232-
# if self.config.get('game', 'ai').lower() == 'sheep':
233-
# cmdline.append('-s')
234-
# elif self.config.get('game', 'ai').lower() == 'tiger':
235-
# cmdline.append('-t')
236-
# else:
237-
# cmdline.append('-s')
238-
239-
# if self.config.has_option('game', 'aistrength'):
240-
# cmdline.append(self.config.get('game', 'aistrength'))
241-
# else:
242-
# cmdline.append('3')
229+
self.config = configparser.ConfigParser()
230+
self.config.read(os.path.expanduser('uiconf'))
231+
232+
if self.config.has_option('game', 'ai'):
233+
if self.config.get('game', 'ai').lower() == 'goat':
234+
self.ai_turn=True
235+
elif self.config.get('game', 'ai').lower() == 'tiger':
236+
self.ai_turn=False
237+
else:
238+
pass
239+
240+
if self.config.has_option('game', 'aistrength'):
241+
aistrength=self.config.get('game', 'aistrength')
242+
else:
243+
aistrength=6
243244

244245
# self.win = ''
245-
self.init_ai()
246+
self.init_ai(int(aistrength))
246247
# self.engine.make_best_move()
247248
# self.check_win()
248249
self.draw()
249250

250-
def init_ai(self):
251+
def init_ai(self, aistrength):
251252
self.ai_vs_ai = False
252253
self.board = Board()
253-
self.engine = Engine(self.board, depth=5)
254+
self.engine = Engine(self.board, depth=aistrength)
254255

255256
def make_ai_move(self, ev=None):
256257
move = self.engine.get_best_move()
257258
self.apply_move(move)
258259

259260

260261
def configure():
261-
config = configparser.SafeConfigParser()
262+
config = configparser.ConfigParser()
262263
config.add_section('game')
263264
config.read(os.path.expanduser('uiconf'))
264265

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /