Ejemplo n.º 1
0
 def compute_boards(player, die, boards):
     """ Function takes a starting board and replaces it with all possible
         boards. Finally returns a list of all possible boards,
         or the starting board(s) if no boards could be created. """
     # boards is a list containing starting boards
     # loop over the boards in boards list
     for i, brd in enumerate(boards):
         new_boards = []
         # check if bar move must be made
         if brd.get_bar(player) > 0:
             try:
                 # make a bar move, and return the resulting board
                 destination = Board.get_home(Board.get_opponent(player)) + \
                                         die * Board.get_direction(player)
                 bar_move = BarMove(player, die, brd, destination)
                 tmp_board = bar_move.make_move()
             except IllegalMoveException:
                 tmp_board = None
             # make sure a bar move was legal and a new board was generated
             # if yes, append new_boards
             if tmp_board is not None:
                 new_boards.append(tmp_board)
         # try normal or bear-off moves:
         else:   
             # loop over whole board
             for pos in range(Board.NUM_POINTS):
                 # pos occupied by player
                 if brd.get_checkers(pos, player) > 0:
                     try:
                         # try to make a normal move and return resulting board
                         destination = pos + (die * Board.get_direction(player))
                         normal_move = NormalMove(player, die, brd, pos, destination)
                         tmp_board = normal_move.make_move()
                     except IllegalMoveException, e:
                         tmp_board = None
                     # make sure normal move was legal and a new board was generated
                     # if yes, append new_boards
                     if tmp_board is not None:
                         new_boards.append(tmp_board)
             # loop over players homeboard, ie the target quadrant
             # of a player (white: 23...18 or black: 0...5)
             for pos in range(Board.get_home(player) - Board.get_direction(player),\
                 Board.get_home(player) - (7 * Board.get_direction(player)),\
                  - Board.get_direction(player)):
                     try:
                         # try to bear off checkers and return resulting board
                         bear_off_move = BearOffMove(player, die, brd, pos)
                         tmp_board = bear_off_move.make_move()
                     except IllegalMoveException, e:
                         tmp_board = None
                     # make sure bearoff move was legal and a new board was generated
                     # if yes, append new_boards
                     if tmp_board is not None:
                         new_boards.append(tmp_board)
Ejemplo n.º 2
0
    def make_move(self):
        """ Validates this movement given the provided
            board situation. """
        if not (self.die == abs(self.start - self.end)):
            #print "make_move2"
            raise IllegalMoveException("Normal move not possible: \
                                            Cannot use die for this movement!")
            
        if self.start == self.end:
            raise IllegalMoveException("Normal move not possible: \
                                Start and end must not be the same position!")
            
        if abs(self.start - self.end) > Dice.MAX_VALUE:
            raise IllegalMoveException("Normal move not possible: \
                                        Move distance larger than 6!")
            
        if self.board.get_bar(self.player) > 0:
            raise IllegalMoveException("Normal move not possible:: \
                                    Checkers from the bar must be moved first!")

        if self.board.get_checkers(self.start, self.player) == 0:
            raise IllegalMoveException("Normal move not possible: \
                                        No checkers at the start location!")
            
        if ((self.start > self.end) and (Board.get_direction(self.player) > 0) or
            (self.start < self.end) and (Board.get_direction(self.player) < 0)):
            raise IllegalMoveException("Normal move not possible: \
                                        Backward movements are not allowed!")
            
        if self.board.get_checkers(self.end, self.other_player) > 1:
            raise IllegalMoveException("Normal move not possible: \
                        End location already occupied by opponent checkers!")

        # now perform movement:
        # check first whether the move bumps the opponent
        if self.board.get_checkers(self.end, self.other_player) == 1:
            self.board.remove_from_location(self.other_player, self.end)
            self.board.move_to_bar(self.other_player)

        # now perform the move
        self.board.remove_from_location(self.player, self.start)
        self.board.move_to_location(self.player, self.end)
        
        # update move history of the board
        self.board.update_move_history(self.__str__())

        return self.board
Ejemplo n.º 3
0
 def can_use(self):
     """ Returns whether or not this movement can use the given
         dice roll to perform its movement. """     
     if self.die == abs(self.start - Board.get_home(self.player)):
         return True
     # if no checker in rolled number, the player is required to remove
     # a checker from the highest point
     elif self.die > abs(self.start - Board.get_home(self.player)):
         direction = Board.get_direction(self.player)
         # check if there are no other checkers left to start point
         # loop from start towards the bar
         for i in range(self.start - direction, Board.get_home(self.player) \
                                 - (7 * direction), - direction):
             if self.board.get_checkers(i, self.player) > 0:
                 return False
         return True
     else:
         return False