def test_custom_board_init(monkeypatch): board = Board.create([0, 1, 2, 3, 4], 1, 5, 10, 3) # Redundant asserts assert board.players == [0, 1, 2, 3, 4] assert board.pieces_per_player == 1 assert board.board_sides == 5 assert board.board_side_length == 10 assert board.finish_zone_length == 3 # Consistency asserts assert board.player_shift == board.board_side_length * board.board_sides // len( board.players ) assert board.path_zone_length == len(board.players) * board.player_shift assert ( board.end_progress == board.player_shift * len(board.players) + board.finish_zone_length + 1 ) assert len(board.pieces) == len(board.players) * board.pieces_per_player # Explicit asserts assert board.pieces == [ Piece(0, 0, 0), Piece(0, 1, 0), Piece(0, 2, 0), Piece(0, 3, 0), Piece(0, 4, 0), ]
def test_do_not_move_piece_to_end_on_bigger_dice(monkeypatch): # Given we have started the game and a piece is in the safe zone board = Board.create(players=[0, 2], pieces_per_player=1) state = GameState.create(board) dice = Dice() monkeypatch.setattr(dice, "roll", lambda: 5) game = GameEngine(board, dice) state.board.pieces[0].progress = board.end_progress - 3 # When we roll the dice with 5 which is bigger then we need to # get to the goal new_state = game.play(GameMove.roll_dice(player=0)) # Then the piece should not go to the goal and it should be the # next player turn assert new_state == game.get_state() assert new_state.number == 1 assert new_state.dice == 5 assert new_state.valid_actions == [ GameMove.roll_dice(player=2), ] assert new_state.board.pieces == [ Piece(number=0, player=0, progress=board.end_progress - 3), Piece(number=0, player=2, progress=0), ] assert new_state.winners == []
def test_board_is_on_target(): board = Board.create() p0_on_target = board.is_on_target(piece=Piece(number=0, player=0, progress=61)) assert not p0_on_target p0_on_target = board.is_on_target(piece=Piece(number=0, player=0, progress=62)) assert p0_on_target p0_on_target = board.is_on_target(piece=Piece(number=0, player=0, progress=66)) assert not p0_on_target
def test_board_is_on_start(): board = Board.create() p0_on_start = board.is_on_start(piece=Piece(number=0, player=0, progress=0)) assert p0_on_start p0_on_start = board.is_on_start(piece=Piece(number=0, player=0, progress=1)) assert not p0_on_start p0_on_start = board.is_on_start(piece=Piece(number=0, player=0, progress=2)) assert not p0_on_start
def test_board_is_on_finish(): board = Board.create() p0_on_finish = board.is_on_finish(piece=Piece(number=0, player=0, progress=56)) assert not p0_on_finish p0_on_finish = board.is_on_finish(piece=Piece(number=0, player=0, progress=61)) assert p0_on_finish p0_on_finish = board.is_on_finish(piece=Piece(number=0, player=0, progress=62)) assert not p0_on_finish
def test_board_is_on_path(): board = Board.create() p0_on_path = board.is_on_path(piece=Piece(number=0, player=0, progress=0)) assert not p0_on_path p0_on_path = board.is_on_path(piece=Piece(number=0, player=0, progress=1)) assert p0_on_path p0_on_path = board.is_on_path(piece=Piece(number=0, player=0, progress=10)) assert p0_on_path p0_on_path = board.is_on_path(piece=Piece(number=0, player=0, progress=61)) assert not p0_on_path
def __on_piece_out(self, piece: Piece, dice: int) -> GameState: assert self.state.board.is_on_start(piece) assert dice == 6 piece.progress = 1 self.__knock_out_other_players(piece) self.state.valid_actions = [GameMove.roll_dice(piece.player)] self.state.number = self.state.number + 1 return self.state
def test_board_relative_position(): board = Board.create() # Test relative position for each player rel_pos_p0 = board.relative_position(piece=Piece(number=0, player=0, progress=20)) assert rel_pos_p0 == 20 rel_pos_p1 = board.relative_position(piece=Piece(number=0, player=1, progress=20)) assert rel_pos_p1 == 34 rel_pos_p2 = board.relative_position(piece=Piece(number=0, player=2, progress=20)) assert rel_pos_p2 == 48 rel_pos_p3 = board.relative_position(piece=Piece(number=0, player=3, progress=20)) assert rel_pos_p3 == 6 # Test a position outside of path_zone with pytest.raises(Exception): board.relative_position(piece=Piece(number=0, player=0, progress=61))
def test_2_players_board_init(monkeypatch): """Make sure if we have just two players in a 4 corner board for them to be at the opposite corners instead of next to each other. """ board = Board.create([1, 3]) # Redundant asserts assert board.players == [1, 3] # Defaults asserts assert board.pieces_per_player == 4 assert board.board_sides == 4 assert board.board_side_length == 14 assert board.finish_zone_length == 5 # Consistency asserts assert board.player_shift == board.board_side_length * board.board_sides // len( board.players ) assert board.path_zone_length == len(board.players) * board.player_shift assert ( board.end_progress == board.player_shift * len(board.players) + board.finish_zone_length + 1 ) assert len(board.pieces) == len(board.players) * board.pieces_per_player # Explicit asserts assert board.pieces == [ Piece(0, 1, 0), Piece(1, 1, 0), Piece(2, 1, 0), Piece(3, 1, 0), Piece(0, 3, 0), Piece(1, 3, 0), Piece(2, 3, 0), Piece(3, 3, 0), ]
def __on_move_piece(self, piece: Piece, dice: int) -> GameState: def __is_winning(piece: Piece) -> bool: winner = True for _piece in self.state.board.pieces: if _piece.player == piece.player: winner = winner and _piece.progress == self.state.board.end_progress return winner b = self.state.board if piece.progress + dice < b.end_progress + 1: piece.progress = piece.progress + dice if b.is_on_path(piece): self.__knock_out_other_players(piece) if __is_winning(piece): self.state.winners.append(piece.player) if len(self.state.winners) >= len(b.players) - 1: self.state.valid_actions = [] else: self.state.valid_actions = [self.__on_end_move()] self.state.number = self.state.number + 1 return self.state
def test_3_players_6_corner_board_init(monkeypatch): """Make sure if we have just 3 players in a 5 corner board for them to be at the opposite corners instead of next to each other. """ board = Board.create([0, 2, 3], board_sides=6, board_side_length=9) # Redundant asserts assert board.players == [0, 2, 3] assert board.board_sides == 6 assert board.board_side_length == 9 # Defaults asserts assert board.finish_zone_length == 5 assert board.pieces_per_player == 4 # Consistency asserts assert board.player_shift == board.board_side_length * board.board_sides // len( board.players ) assert board.path_zone_length == len(board.players) * board.player_shift # end_progress == path_zone_length + finish_zone_length + 1 THAT IS # end_progress == (board_sides * board_side_length) + finish_zone_length + 1 assert ( board.end_progress == board.player_shift * len(board.players) + board.finish_zone_length + 1 ) assert len(board.pieces) == len(board.players) * board.pieces_per_player # Explicit asserts assert board.pieces == [ Piece(0, 0, 0), Piece(1, 0, 0), Piece(2, 0, 0), Piece(3, 0, 0), Piece(0, 2, 0), Piece(1, 2, 0), Piece(2, 2, 0), Piece(3, 2, 0), Piece(0, 3, 0), Piece(1, 3, 0), Piece(2, 3, 0), Piece(3, 3, 0), ]
def test_default_board_init(monkeypatch): board = Board.create() # Defaults asserts assert board.players == [0, 1, 2, 3] assert board.pieces_per_player == 4 assert board.board_sides == 4 assert board.board_side_length == 14 assert board.finish_zone_length == 5 # Consistency asserts assert board.player_shift == 14 assert ( board.end_progress == board.player_shift * len(board.players) + board.finish_zone_length + 1 ) assert len(board.pieces) == len(board.players) * board.pieces_per_player # Explicit asserts assert list(filter(lambda p: p.player == 0, board.pieces)) == [ Piece(0, 0, 0), Piece(1, 0, 0), Piece(2, 0, 0), Piece(3, 0, 0), ] assert list(filter(lambda p: p.player == 1, board.pieces)) == [ Piece(0, 1, 0), Piece(1, 1, 0), Piece(2, 1, 0), Piece(3, 1, 0), ] assert list(filter(lambda p: p.player == 2, board.pieces)) == [ Piece(0, 2, 0), Piece(1, 2, 0), Piece(2, 2, 0), Piece(3, 2, 0), ] assert list(filter(lambda p: p.player == 3, board.pieces)) == [ Piece(0, 3, 0), Piece(1, 3, 0), Piece(2, 3, 0), Piece(3, 3, 0), ]
def test_do_move_take_out_of_home_and_knock_out(monkeypatch): dice4 = Dice() monkeypatch.setattr(dice4, "roll", lambda: 4) dice6 = Dice() monkeypatch.setattr(dice6, "roll", lambda: 6) b = Board.create(players=[0, 1], pieces_per_player=1) g = GameEngine(b) assert GameMove.roll_dice(0) in g.state.valid_actions g.dice = dice4 s = g.play(GameMove.roll_dice(0)) assert GameMove.roll_dice(1) in s.valid_actions g.dice = dice6 s = g.play(GameMove.roll_dice(1)) s = g.play(GameMove.piece_out(1, 0)) assert s.board.pieces == [Piece(0, 0, 0), Piece(0, 1, 1)] assert s.valid_actions == [GameMove.roll_dice(1)] s = g.play(GameMove.roll_dice(1)) s = g.play(GameMove.move_piece(1, 0, 6)) assert s.board.pieces == [Piece(0, 0, 0), Piece(0, 1, 7)] assert s.valid_actions == [GameMove.roll_dice(1)] s = g.play(GameMove.roll_dice(1)) s = g.play(GameMove.move_piece(1, 0, 6)) assert s.board.pieces == [Piece(0, 0, 0), Piece(0, 1, 13)] assert s.valid_actions == [GameMove.roll_dice(1)] s = g.play(GameMove.roll_dice(1)) s = g.play(GameMove.move_piece(1, 0, 6)) assert s.board.pieces == [Piece(0, 0, 0), Piece(0, 1, 19)] assert s.valid_actions == [GameMove.roll_dice(1)] s = g.play(GameMove.roll_dice(1)) s = g.play(GameMove.move_piece(1, 0, 6)) assert s.board.pieces == [Piece(0, 0, 0), Piece(0, 1, 25)] assert s.valid_actions == [GameMove.roll_dice(1)] g.dice = dice4 s = g.play(GameMove.roll_dice(1)) s = g.play(GameMove.move_piece(1, 0, 4)) assert s.board.pieces == [Piece(0, 0, 0), Piece(0, 1, 29)] assert s.valid_actions == [GameMove.roll_dice(0)] g.dice = dice6 s = g.play(GameMove.roll_dice(0)) assert GameMove.piece_out(0, 0) in s.valid_actions s = g.play(GameMove.piece_out(0, 0)) assert s.board.pieces == [Piece(0, 0, 1), Piece(0, 1, 0)]
def test_play_until_the_end_two_players_once_piece(monkeypatch): # Given we have started the game board = Board.create(players=[0, 2], pieces_per_player=1) state = GameState.create(board) dice = Dice() monkeypatch.setattr(dice, "roll", lambda: 6) game = GameEngine(board, dice) # When we roll the dice new_state = game.play(GameMove.roll_dice(player=0)) # Then the state should be as expected assert new_state == game.get_state() assert new_state.number == 1 assert new_state.dice == 6 assert new_state.valid_actions == [ GameMove.piece_out(player=0, piece=0, dice=6), ] # And When we play getting out with the first peice new_state = game.play(GameMove.piece_out(player=0, piece=0, dice=6)) # Then the first piece should be out assert new_state.current_player == 0 assert new_state.number == 2 assert new_state.dice == 6 assert new_state.board.pieces == [ Piece(number=0, player=0, progress=1), Piece(number=0, player=2, progress=0), ] assert new_state.valid_actions == [GameMove.roll_dice(player=0)] # And When we row the dice again with 6 new_state = game.play(GameMove.roll_dice(player=0)) # Then we should should be able to move the piece forward assert new_state.number == 3 assert new_state.dice == 6 assert new_state.valid_actions == [GameMove.move_piece(player=0, piece=0, dice=6)] # And When we move the piece new_state = game.play(GameMove.move_piece(player=0, piece=0, dice=6)) # Then it should go forward and we should be able to roll the dice again assert new_state.number == 4 assert new_state.winners == [] assert new_state.board.pieces == [ Piece(number=0, player=0, progress=7), Piece(number=0, player=2, progress=0), ] assert new_state.valid_actions == [GameMove.roll_dice(player=0)] # And When we roll the dice again with 6 new_state = game.play(GameMove.roll_dice(player=0)) # Then we should be able to move the piece forward assert new_state.dice == 6 assert new_state.number == 5 assert new_state.valid_actions == [GameMove.move_piece(player=0, piece=0, dice=6)] # And When we move the piece new_state = game.play(GameMove.move_piece(player=0, piece=0, dice=6)) # Then the piece should go forward and we should be able to roll the dice again assert new_state.number == 6 assert new_state.board.pieces == [ Piece(number=0, player=0, progress=13), Piece(number=0, player=2, progress=0), ] assert new_state.winners == [] assert new_state.valid_actions == [GameMove.roll_dice(player=0)] # And When we position the piece toward the end of the board new_state.board.pieces[0].progress = board.end_progress - 6 # And When we roll the dice again with 6 new_state = game.play(GameMove.roll_dice(player=0)) # Then we should be able to move the piece forward into the safe zone and to the goal assert new_state.dice == 6 assert new_state.number == 7 assert new_state.valid_actions == [GameMove.move_piece(player=0, piece=0, dice=6)] # And When we move the piece new_state = game.play(GameMove.move_piece(player=0, piece=0, dice=6)) # Then the piece should go forward and we should be able to roll the dice again assert new_state.number == 8 assert new_state.board.pieces == [ Piece(number=0, player=0, progress=board.end_progress), Piece(number=0, player=2, progress=0), ] assert new_state.winners == [0] assert new_state.valid_actions == []
def test_move_blocked(monkeypatch): dice1 = Dice() monkeypatch.setattr(dice1, "roll", lambda: 1) dice5 = Dice() monkeypatch.setattr(dice5, "roll", lambda: 5) dice6 = Dice() monkeypatch.setattr(dice6, "roll", lambda: 6) b = Board.create(players=[0, 1], pieces_per_player=2) g = GameEngine(b) assert GameMove.roll_dice(0) in g.state.valid_actions g.dice = dice5 s = g.play(GameMove.roll_dice(0)) assert GameMove.roll_dice(1) in s.valid_actions g.dice = dice6 s = g.play(GameMove.roll_dice(1)) s = g.play(GameMove.piece_out(1, 0)) assert s.board.pieces == [Piece(0, 0), Piece(1, 0), Piece(0, 1, 1), Piece(1, 1)] assert GameMove.roll_dice(1) in s.valid_actions s = g.play(GameMove.roll_dice(1)) s = g.play(GameMove.piece_out(1, 1)) assert s.board.pieces == [Piece(0, 0), Piece(1, 0), Piece(0, 1, 1), Piece(1, 1, 1)] assert GameMove.roll_dice(1) in s.valid_actions s = g.play(GameMove.roll_dice(1)) s = g.play(GameMove.move_piece(1, 0, 6)) s = g.play(GameMove.roll_dice(1)) s = g.play(GameMove.move_piece(1, 1, 6)) assert s.board.pieces == [Piece(0, 0), Piece(1, 0), Piece(0, 1, 7), Piece(1, 1, 7)] assert GameMove.roll_dice(1) in s.valid_actions s = g.play(GameMove.roll_dice(1)) s = g.play(GameMove.move_piece(1, 0, 6)) s = g.play(GameMove.roll_dice(1)) s = g.play(GameMove.move_piece(1, 1, 6)) assert s.board.pieces == [ Piece(0, 0), Piece(1, 0), Piece(0, 1, 13), Piece(1, 1, 13), ] assert GameMove.roll_dice(1) in s.valid_actions s = g.play(GameMove.roll_dice(1)) s = g.play(GameMove.move_piece(1, 0, 6)) s = g.play(GameMove.roll_dice(1)) s = g.play(GameMove.move_piece(1, 1, 6)) assert s.board.pieces == [ Piece(0, 0), Piece(1, 0), Piece(0, 1, 19), Piece(1, 1, 19), ] assert GameMove.roll_dice(1) in s.valid_actions s = g.play(GameMove.roll_dice(1)) s = g.play(GameMove.move_piece(1, 0, 6)) s = g.play(GameMove.roll_dice(1)) s = g.play(GameMove.move_piece(1, 1, 6)) assert s.board.pieces == [ Piece(0, 0), Piece(1, 0), Piece(0, 1, 25), Piece(1, 1, 25), ] assert GameMove.roll_dice(1) in s.valid_actions g.dice = dice5 s = g.play(GameMove.roll_dice(1)) s = g.play(GameMove.move_piece(1, 0, 5)) assert s.board.pieces == [ Piece(0, 0), Piece(1, 0), Piece(0, 1, 30), Piece(1, 1, 25), ] assert s.valid_actions == [GameMove.roll_dice(0)] s = g.play(GameMove.roll_dice(0)) # i.e. player 0 can't move assert s.valid_actions == [GameMove.roll_dice(1)] s = g.play(GameMove.roll_dice(1)) s = g.play(GameMove.move_piece(1, 1, 5)) assert s.board.pieces == [ Piece(0, 0), Piece(1, 0), Piece(0, 1, 30), Piece(1, 1, 30), ] assert s.valid_actions == [GameMove.roll_dice(0)] g.dice = dice6 s = g.play(GameMove.roll_dice(0)) s = g.play(GameMove.piece_out(0, 0)) assert s.board.pieces == [ Piece(0, 0, 1), Piece(1, 0), Piece(0, 1, 30), Piece(1, 1, 30), ] assert s.valid_actions == [GameMove.roll_dice(0)] g.dice = dice1 s = g.play(GameMove.roll_dice(0)) # i.e. player 0 can't move: piece 1 is not out, and piece 0 is blocked assert s.valid_actions == [GameMove.roll_dice(1)]
def test_do_move_and_knock_out(monkeypatch): dice1 = Dice() monkeypatch.setattr(dice1, "roll", lambda: 1) dice5 = Dice() monkeypatch.setattr(dice5, "roll", lambda: 5) dice6 = Dice() monkeypatch.setattr(dice6, "roll", lambda: 6) b = Board.create(players=[0, 1], pieces_per_player=1) g = GameEngine(b) assert GameMove.roll_dice(0) in g.state.valid_actions g.dice = dice1 s = g.play(GameMove.roll_dice(0)) assert GameMove.roll_dice(1) in s.valid_actions g.dice = dice6 s = g.play(GameMove.roll_dice(1)) s = g.play(GameMove.piece_out(1, 0)) assert s.board.pieces == [Piece(0, 0, 0), Piece(0, 1, 1)] assert GameMove.roll_dice(1) in s.valid_actions s = g.play(GameMove.roll_dice(1)) s = g.play(GameMove.move_piece(1, 0, 6)) assert s.board.pieces == [Piece(0, 0, 0), Piece(0, 1, 7)] assert GameMove.roll_dice(1) in s.valid_actions s = g.play(GameMove.roll_dice(1)) s = g.play(GameMove.move_piece(1, 0, 6)) assert s.board.pieces == [Piece(0, 0, 0), Piece(0, 1, 13)] assert GameMove.roll_dice(1) in s.valid_actions s = g.play(GameMove.roll_dice(1)) s = g.play(GameMove.move_piece(1, 0, 6)) assert s.board.pieces == [Piece(0, 0, 0), Piece(0, 1, 19)] assert GameMove.roll_dice(1) in s.valid_actions g.dice = dice5 s = g.play(GameMove.roll_dice(1)) s = g.play(GameMove.move_piece(1, 0, 5)) assert s.board.pieces == [Piece(0, 0, 0), Piece(0, 1, 24)] assert GameMove.roll_dice(0) in s.valid_actions g.dice = dice6 s = g.play(GameMove.roll_dice(0)) assert GameMove.piece_out(0, 0) in s.valid_actions s = g.play(GameMove.piece_out(0, 0)) assert s.board.pieces == [Piece(0, 0, 1), Piece(0, 1, 24)] g.dice = dice1 s = g.play(GameMove.roll_dice(0)) s = g.play(GameMove.move_piece(0, 0, 1)) assert s.board.pieces == [Piece(0, 0, 2), Piece(0, 1, 24)] g.dice = dice6 s = g.play(GameMove.roll_dice(1)) assert GameMove.move_piece(1, 0, 6) in s.valid_actions s = g.play(GameMove.move_piece(1, 0, 6)) # player 1 hits player 0 and sends her home assert s.board.pieces == [Piece(0, 0, 0), Piece(0, 1, 30)]
def test_do_move_blocked_out_of_home(monkeypatch): dice4 = Dice() monkeypatch.setattr(dice4, "roll", lambda: 4) dice6 = Dice() monkeypatch.setattr(dice6, "roll", lambda: 6) b = Board.create(players=[0, 1], pieces_per_player=2) g = GameEngine(b) assert GameMove.roll_dice(0) in g.state.valid_actions g.dice = dice4 s = g.play(GameMove.roll_dice(0)) assert GameMove.roll_dice(1) in s.valid_actions g.dice = dice6 s = g.play(GameMove.roll_dice(1)) s = g.play(GameMove.piece_out(1, 0)) assert s.board.pieces == [Piece(0, 0), Piece(1, 0), Piece(0, 1, 1), Piece(1, 1)] assert GameMove.roll_dice(1) in s.valid_actions g.dice = dice6 s = g.play(GameMove.roll_dice(1)) s = g.play(GameMove.piece_out(1, 1)) assert s.board.pieces == [Piece(0, 0), Piece(1, 0), Piece(0, 1, 1), Piece(1, 1, 1)] assert GameMove.roll_dice(1) in s.valid_actions s = g.play(GameMove.roll_dice(1)) s = g.play(GameMove.move_piece(1, 0, 6)) s = g.play(GameMove.roll_dice(1)) s = g.play(GameMove.move_piece(1, 1, 6)) assert s.board.pieces == [Piece(0, 0), Piece(1, 0), Piece(0, 1, 7), Piece(1, 1, 7)] assert GameMove.roll_dice(1) in s.valid_actions s = g.play(GameMove.roll_dice(1)) s = g.play(GameMove.move_piece(1, 0, 6)) s = g.play(GameMove.roll_dice(1)) s = g.play(GameMove.move_piece(1, 1, 6)) assert s.board.pieces == [ Piece(0, 0), Piece(1, 0), Piece(0, 1, 13), Piece(1, 1, 13), ] assert GameMove.roll_dice(1) in s.valid_actions s = g.play(GameMove.roll_dice(1)) s = g.play(GameMove.move_piece(1, 0, 6)) s = g.play(GameMove.roll_dice(1)) s = g.play(GameMove.move_piece(1, 1, 6)) assert s.board.pieces == [ Piece(0, 0), Piece(1, 0), Piece(0, 1, 19), Piece(1, 1, 19), ] assert GameMove.roll_dice(1) in s.valid_actions s = g.play(GameMove.roll_dice(1)) s = g.play(GameMove.move_piece(1, 0, 6)) s = g.play(GameMove.roll_dice(1)) s = g.play(GameMove.move_piece(1, 1, 6)) assert s.board.pieces == [ Piece(0, 0), Piece(1, 0), Piece(0, 1, 25), Piece(1, 1, 25), ] assert GameMove.roll_dice(1) in s.valid_actions g.dice = dice4 s = g.play(GameMove.roll_dice(1)) s = g.play(GameMove.move_piece(1, 0, 4)) assert s.board.pieces == [ Piece(0, 0), Piece(1, 0), Piece(0, 1, 29), Piece(1, 1, 25), ] assert s.valid_actions == [GameMove.roll_dice(0)] s = g.play(GameMove.roll_dice(0)) # i.e. player 0 can't move assert s.valid_actions == [GameMove.roll_dice(1)] s = g.play(GameMove.roll_dice(1)) s = g.play(GameMove.move_piece(1, 1, 4)) assert s.board.pieces == [ Piece(0, 0), Piece(1, 0), Piece(0, 1, 29), Piece(1, 1, 29), ] assert s.valid_actions == [GameMove.roll_dice(0)] assert s.board.pieces == [ Piece(0, 0, 0), Piece(1, 0, 0), Piece(0, 1, 29), Piece(1, 1, 29), ] g.dice = dice6 s = g.play(GameMove.roll_dice(0)) # i.e. player 0 can't move, but since she drew 6, repeats turn assert s.current_player == 0 assert s.valid_actions == [GameMove.roll_dice(0)]