class BarMove(object): """ Methods for removing checkers from the Bar. """ def __init__(self, player, die, board, end): self.player = player self.other_player = Board.get_opponent(player) self.die = die self.board = Board(board) # check if end is on the board if not Board.on_board(end): raise IllegalMoveException("End is not on the Board!") else: self.end = end def move_possible(self): """ Checks if""" 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 def __str__(self): """ Returns string representation of this movement. """ return "bar --> %s" %(self.end + 1) def __eq__(self, other): """ Returns whether this movement is equal to the provided object. """ if isinstance(other, BarMovement): return other.end == self.end else: return False def __ne__(self, other): return not self == other
class NormalMove(object): """ Methods for a normal movement. """ def __init__(self, player, die, board, start, end): self.player = player self.other_player = Board.get_opponent(player) self.die = die self.board = Board(board) if (not Board.on_board(start)) or (not Board.on_board(end)): raise IllegalMoveException("Start or end is not on the board!") else: self.start = start self.end = end 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 def __str__(self): """ Returns a String representation of this movement. """ return "%s --> %s" %(self.start + 1, self.end + 1) def __eq__(self, other): """ Returns whether this movement is equal to the provided object. """ if isinstance(other, NormalMovement): return (other.start == self.start) and (other.end == self.end) else: return False def __ne__(self, other): return not self == other