def test_play_roll_dice_3(monkeypatch): # Given board = Board.create(players=[1, 3]) state = GameState.create(board) dice = Dice() monkeypatch.setattr(dice, "roll", lambda: 3) game = GameEngine(board, dice) # When new_state = game.play(GameMove.roll_dice(player=1)) # Then assert new_state == game.get_state() assert new_state.number == 1 assert new_state.dice == 3 assert new_state.valid_actions == [GameMove.roll_dice(player=3)] # And When new_state = game.play(GameMove.roll_dice(player=3)) # Then assert new_state == game.get_state() assert new_state.number == 2 assert new_state.dice == 3 assert new_state.valid_actions == [GameMove.roll_dice(player=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_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_initial_gate_state(monkeypatch): # Given board = Board.create(players=[1, 3]) state = GameState.create(board) # When game = GameEngine(board) # Then assert game.state == state
def test_game_state_defaults(monkeypatch): board = Board.create() state = GameState.create(board) assert state.board == board assert state.number == 0 assert state.dice == -1 assert state.winners == [] assert state.current_player == 0 assert state.valid_actions == [GameMove.roll_dice(player=0)]
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_state(monkeypatch, client): # Given we have joined 4 players in the previous test # When we call /state rv = client.get("/state") game_state = json.loads(rv.data) board = Board.create(players=[0, 1, 2, 3]) state = GameState.create(board) # Then we want the same state as the default for 4 players assert game_state == dataclasses.asdict(state)
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_invalid_action_on_initial_state(monkeypatch): # Given board = Board.create(players=[1, 3]) state = GameState.create(board) game = GameEngine(board) # When we try to play an invalid action PieceOut with pytest.raises(Exception): game.play(GameMove.piece_out(1, 1)) # When we try to play valid action for an invalid Player with pytest.raises(Exception): game.play(GameMove.roll_dice(player=3))
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 test_state_next_player(monkeypatch): board = Board.create([0, 1, 3, 5]) state = GameState.create(board) assert state.current_player == 0 # assert state.next_player() == 1 state.current_player = 1 # assert state.next_player() == 3 state.current_player = 3 # assert state.next_player() == 5 state.current_player = 5
def test_roll(monkeypatch, client): # Given 4 players had joined in the previous tests and the game had starte # When we try to play with the correct user token rv = client.get("/play/roll", headers={"4oBe4e-user-token": player1_token}) game_state = json.loads(rv.data) board = Board.create(players=[0, 1, 2, 3]) dice = Dice() monkeypatch.setattr(dice, "roll", lambda: game_state["dice"]) game = GameEngine(board, dice) game.play(GameMove.roll_dice(player=0)) # Then we want the same state as the default for 4 players assert game_state == dataclasses.asdict(game.get_state())
def test_negative_create_wrong_players_board(monkeypatch): # player index bigger then the board # with pytest.raises(Exception): # board = Board.create(players=[6, 1], board_sides=5) # board with no players with pytest.raises(Exception): Board.create([]) # board with duplicate players with pytest.raises(Exception): Board.create([1, 1]) # board with too many players with pytest.raises(Exception): Board.create([0, 1, 2], board_sides=2)
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_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_play_roll_dice_6(monkeypatch): # Given board = Board.create(players=[1, 3]) state = GameState.create(board) dice = Dice() monkeypatch.setattr(dice, "roll", lambda: 6) game = GameEngine(board, dice) # When new_state = game.play(GameMove.roll_dice(player=1)) # Then # 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=1, piece=0, dice=6), GameMove.piece_out(player=1, piece=1, dice=6), GameMove.piece_out(player=1, piece=2, dice=6), GameMove.piece_out(player=1, piece=3, dice=6), ]
def join(player: str): global engine # TODO Where shall we keep the curren running GameEngine/s ? if player in player_name_token: token = player_name_token[player] num = player_token_number[token] return jsonify({"player_token": token, "player_num": num}) if len(player_token_name) == 4: players: Dict[str, int] = dict( (name, player_token_number[token]) for name, token in player_name_token.items()) raise ValueError("Game is full. Players are ", players) player_uuid: str = str(uuid.uuid4()) player_token_name[player_uuid] = player player_number: int = len(player_token_number) player_name_token[player] = player_uuid player_token_number[player_uuid] = player_number if len(player_token_name) == 4: board = Board.create(list(player_token_number.values())) engine = GameEngine(board) __state_to_json(engine.state) return jsonify({"player_token": player_uuid, "player_num": player_number})
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), ]
help="The algorithm used to solve the problem (a_star or dijkstra)", default='dijkstra') args = parser.parse_args() if args.game == "maze": start_maze = Maze.fixture_10_by_10() print(start_maze) if args.strategy == "a_star": path = AStar.solve(start_maze, heuristic=Maze.h1) elif args.strategy == "dijkstra": path = Dijkstra.solve(start_maze) elif args.game == "taquin": start_board = Board.random() print(start_board) if args.strategy == "a_star": path = AStar.solve(start_board, heuristic=Board.h2) elif args.strategy == "dijkstra": path = Dijkstra.solve(start_board) if path: print("path found") for index, state in enumerate(path): print("step", index) print(state) else : print("no path is found. The state is considered as not solvable")
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)]
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)]
board.push(move) return json.dumps({'value': value, 'board': board.fen()}) @app.route('/move') def move(): if board.who_win() is not None: return json.dumps({'value': 'game over', 'winner': board.who_win()}) move = computer.get_move(board) time.sleep(1) board.push(move) if board.who_win() is not None: return json.dumps({'value': 'game over', 'winner': board.who_win()}) return json.dumps({'value': 'done', 'board': board.fen()}) @app.route('/newgame') def new_game(): board.reset() return board.fen() if __name__ == '__main__': board = Board() computer = Computer() app.run(debug=True)
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 == []