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)
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
def make_move(self): """ Validates the movement given the provided board situation. """ if not (self.die == abs(Board.get_home(self.other_player) - self.end)): raise IllegalMoveException("Off-Bar not possible: \ Die cannot be used to perform this movement!") # check if bar is empty and raise error if yes if self.board.get_bar(self.player) == 0: raise IllegalMoveException("Off-Bar not possible: \ No checkers on the Bar!") # check if end position is in the homeboard of the other player # if not raise Error, checkers leaving the bar must be placed in the # homeboard of the opponent (your starting quadrant) if not Board.in_home_board(self.other_player, self.end): raise IllegalMoveException("Off-Bar not possible: \ Checkers must go into the opponents home board!") # check if there is more than one opponent checker at end position # and raise error when yes if self.board.get_checkers(self.end, self.other_player) > 1: raise IllegalMoveException("Off-Bar not possible: \ Location occupied by other player") # now make the movement: # first kick enemy checkers onto the bar if there are any 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) self.board.remove_from_bar(self.player) 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