@@ -53,7 +53,7 @@ def max_value(state, dice_roll):
53
53
game .dice_roll = dice_roll
54
54
return v
55
55
56
- def min_value (state , dice_roll ):
56
+ def min_value (state , dice_roll ):
57
57
v = infinity
58
58
for a in game .actions (state ):
59
59
v = min (v , chance_node (state , a ))
@@ -395,10 +395,21 @@ class Backgammon(Game):
395
395
rolling a pair of dice."""
396
396
397
397
def __init__ (self ):
398
+ """Initial state of the game"""
398
399
self .dice_roll = (- random .randint (1 , 6 ), - random .randint (1 , 6 ))
399
- board = BackgammonBoard ()
400
+ # TODO : Add bar to Board class where a blot is placed when it is hit.
401
+ point = {'W' :0 , 'B' :0 }
402
+ self .board = [point .copy () for index in range (24 )]
403
+ self .board [0 ]['B' ] = self .board [23 ]['W' ] = 2
404
+ self .board [5 ]['W' ] = self .board [18 ]['B' ] = 5
405
+ self .board [7 ]['W' ] = self .board [16 ]['B' ] = 3
406
+ self .board [11 ]['B' ] = self .board [12 ]['W' ] = 5
407
+ self .allow_bear_off = {'W' : False , 'B' : False }
408
+
400
409
self .initial = GameState (to_move = 'W' ,
401
- utility = 0 , board = board , moves = self .get_all_moves (board , 'W' ))
410
+ utility = 0 ,
411
+ board = self .board ,
412
+ moves = self .get_all_moves (self .board , 'W' ))
402
413
403
414
def actions (self , state ):
404
415
"""Returns a list of legal moves for a state."""
@@ -409,16 +420,16 @@ def actions(self, state):
409
420
legal_moves = []
410
421
for move in moves :
411
422
board = copy .deepcopy (state .board )
412
- if board .is_legal_move (move , self .dice_roll , player ):
423
+ if self .is_legal_move (move , self .dice_roll , player ):
413
424
legal_moves .append (move )
414
425
return legal_moves
415
426
416
427
def result (self , state , move ):
417
428
board = copy .deepcopy (state .board )
418
429
player = state .to_move
419
- board .move_checker (move [0 ], self .dice_roll [0 ], player )
430
+ self .move_checker (move [0 ], self .dice_roll [0 ], player )
420
431
if len (move ) == 2 :
421
- board .move_checker (move [1 ], self .dice_roll [1 ], player )
432
+ self .move_checker (move [1 ], self .dice_roll [1 ], player )
422
433
to_move = ('W' if player == 'B' else 'B' )
423
434
return GameState (to_move = to_move ,
424
435
utility = self .compute_utility (board , move , player ),
@@ -438,10 +449,10 @@ def get_all_moves(self, board, player):
438
449
"""All possible moves for a player i.e. all possible ways of
439
450
choosing two checkers of a player from the board for a move
440
451
at a given state."""
441
- all_points = board . points
452
+ all_points = board
442
453
taken_points = [index for index , point in enumerate (all_points )
443
454
if point [player ] > 0 ]
444
- if board .checkers_at_home (player ) == 1 :
455
+ if self .checkers_at_home (player ) == 1 :
445
456
return [(taken_points [0 ], )]
446
457
moves = list (itertools .permutations (taken_points , 2 ))
447
458
moves = moves + [(index , index ) for index , point in enumerate (all_points )
@@ -453,7 +464,7 @@ def display(self, state):
453
464
board = state .board
454
465
player = state .to_move
455
466
print ("Current State : " )
456
- for index , point in enumerate (board . points ):
467
+ for index , point in enumerate (board ):
457
468
if point ['W' ] != 0 or point ['B' ] != 0 :
458
469
print ("Point : " , index , " W : " , point ['W' ], " B : " , point ['B' ])
459
470
print ("To play : " , player )
@@ -462,37 +473,19 @@ def compute_utility(self, board, move, player):
462
473
"""If 'W' wins with this move, return 1; if 'B' wins return -1; else return 0."""
463
474
count = 0
464
475
for idx in range (0 , 24 ):
465
- count = count + board . points [idx ][player ]
476
+ count = count + board [idx ][player ]
466
477
if player == 'W' and count == 0 :
467
478
return 1
468
479
if player == 'B' and count == 0 :
469
480
return - 1
470
481
return 0
471
482
472
-
473
- class BackgammonBoard :
474
- """The board consists of 24 points. Each player('W' and 'B') initially
475
- has 15 checkers on board. Player 'W' moves from point 23 to point 0
476
- and player 'B' moves from point 0 to 23. Points 0-7 are
477
- home for player W and points 17-24 are home for B."""
478
-
479
- def __init__ (self ):
480
- """Initial state of the game"""
481
- # TODO : Add bar to Board class where a blot is placed when it is hit.
482
- point = {'W' :0 , 'B' :0 }
483
- self .points = [point .copy () for index in range (24 )]
484
- self .points [0 ]['B' ] = self .points [23 ]['W' ] = 2
485
- self .points [5 ]['W' ] = self .points [18 ]['B' ] = 5
486
- self .points [7 ]['W' ] = self .points [16 ]['B' ] = 3
487
- self .points [11 ]['B' ] = self .points [12 ]['W' ] = 5
488
- self .allow_bear_off = {'W' : False , 'B' : False }
489
-
490
483
def checkers_at_home (self , player ):
491
484
"""Return the no. of checkers at home for a player."""
492
485
sum_range = range (0 , 7 ) if player == 'W' else range (17 , 24 )
493
486
count = 0
494
487
for idx in sum_range :
495
- count = count + self .points [idx ][player ]
488
+ count = count + self .board [idx ][player ]
496
489
return count
497
490
498
491
def is_legal_move (self , start , steps , player ):
@@ -504,7 +497,7 @@ def is_legal_move(self, start, steps, player):
504
497
dest_range = range (0 , 24 )
505
498
move1_legal = move2_legal = False
506
499
if dest1 in dest_range :
507
- if self .is_point_open (player , self .points [dest1 ]):
500
+ if self .is_point_open (player , self .board [dest1 ]):
508
501
self .move_checker (start [0 ], steps [0 ], player )
509
502
move1_legal = True
510
503
else :
@@ -514,7 +507,7 @@ def is_legal_move(self, start, steps, player):
514
507
if not move1_legal :
515
508
return False
516
509
if dest2 in dest_range :
517
- if self .is_point_open (player , self .points [dest2 ]):
510
+ if self .is_point_open (player , self .board [dest2 ]):
518
511
move2_legal = True
519
512
else :
520
513
if self .allow_bear_off [player ]:
@@ -525,9 +518,9 @@ def move_checker(self, start, steps, player):
525
518
"""Move a checker from starting point by a given number of steps"""
526
519
dest = start + steps
527
520
dest_range = range (0 , 24 )
528
- self .points [start ][player ] -= 1
521
+ self .board [start ][player ] -= 1
529
522
if dest in dest_range :
530
- self .points [dest ][player ] += 1
523
+ self .board [dest ][player ] += 1
531
524
if self .checkers_at_home (player ) == 15 :
532
525
self .allow_bear_off [player ] = True
533
526
0 commit comments