@@ -53,7 +53,7 @@ def max_value(state, dice_roll):
5353 game .dice_roll = dice_roll
5454 return v
5555
56- def min_value (state , dice_roll ):
56+ def min_value (state , dice_roll ):
5757 v = infinity
5858 for a in game .actions (state ):
5959 v = min (v , chance_node (state , a ))
@@ -395,10 +395,21 @@ class Backgammon(Game):
395395 rolling a pair of dice."""
396396
397397 def __init__ (self ):
398+ """Initial state of the game"""
398399 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+
400409 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' ))
402413
403414 def actions (self , state ):
404415 """Returns a list of legal moves for a state."""
@@ -409,16 +420,16 @@ def actions(self, state):
409420 legal_moves = []
410421 for move in moves :
411422 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 ):
413424 legal_moves .append (move )
414425 return legal_moves
415426
416427 def result (self , state , move ):
417428 board = copy .deepcopy (state .board )
418429 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 )
420431 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 )
422433 to_move = ('W' if player == 'B' else 'B' )
423434 return GameState (to_move = to_move ,
424435 utility = self .compute_utility (board , move , player ),
@@ -438,10 +449,10 @@ def get_all_moves(self, board, player):
438449 """All possible moves for a player i.e. all possible ways of
439450 choosing two checkers of a player from the board for a move
440451 at a given state."""
441- all_points = board . points
452+ all_points = board
442453 taken_points = [index for index , point in enumerate (all_points )
443454 if point [player ] > 0 ]
444- if board .checkers_at_home (player ) == 1 :
455+ if self .checkers_at_home (player ) == 1 :
445456 return [(taken_points [0 ], )]
446457 moves = list (itertools .permutations (taken_points , 2 ))
447458 moves = moves + [(index , index ) for index , point in enumerate (all_points )
@@ -453,7 +464,7 @@ def display(self, state):
453464 board = state .board
454465 player = state .to_move
455466 print ("Current State : " )
456- for index , point in enumerate (board . points ):
467+ for index , point in enumerate (board ):
457468 if point ['W' ] != 0 or point ['B' ] != 0 :
458469 print ("Point : " , index , " W : " , point ['W' ], " B : " , point ['B' ])
459470 print ("To play : " , player )
@@ -462,37 +473,19 @@ def compute_utility(self, board, move, player):
462473 """If 'W' wins with this move, return 1; if 'B' wins return -1; else return 0."""
463474 count = 0
464475 for idx in range (0 , 24 ):
465- count = count + board . points [idx ][player ]
476+ count = count + board [idx ][player ]
466477 if player == 'W' and count == 0 :
467478 return 1
468479 if player == 'B' and count == 0 :
469480 return - 1
470481 return 0
471482
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-
490483 def checkers_at_home (self , player ):
491484 """Return the no. of checkers at home for a player."""
492485 sum_range = range (0 , 7 ) if player == 'W' else range (17 , 24 )
493486 count = 0
494487 for idx in sum_range :
495- count = count + self .points [idx ][player ]
488+ count = count + self .board [idx ][player ]
496489 return count
497490
498491 def is_legal_move (self , start , steps , player ):
@@ -504,7 +497,7 @@ def is_legal_move(self, start, steps, player):
504497 dest_range = range (0 , 24 )
505498 move1_legal = move2_legal = False
506499 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 ]):
508501 self .move_checker (start [0 ], steps [0 ], player )
509502 move1_legal = True
510503 else :
@@ -514,7 +507,7 @@ def is_legal_move(self, start, steps, player):
514507 if not move1_legal :
515508 return False
516509 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 ]):
518511 move2_legal = True
519512 else :
520513 if self .allow_bear_off [player ]:
@@ -525,9 +518,9 @@ def move_checker(self, start, steps, player):
525518 """Move a checker from starting point by a given number of steps"""
526519 dest = start + steps
527520 dest_range = range (0 , 24 )
528- self .points [start ][player ] -= 1
521+ self .board [start ][player ] -= 1
529522 if dest in dest_range :
530- self .points [dest ][player ] += 1
523+ self .board [dest ][player ] += 1
531524 if self .checkers_at_home (player ) == 15 :
532525 self .allow_bear_off [player ] = True
533526
0 commit comments