def test_fill_full_hand(): tiles_in_hand = [ Tile(Color.RED, Shape.CIRCLE), Tile(Color.ORANGE, Shape.X), Tile(Color.YELLOW, Shape.DIAMOND), Tile(Color.GREEN, Shape.SQUARE), Tile(Color.BLUE, Shape.STAR), Tile(Color.PURPLE, Shape.CLOVER), ] bag = Bag(tiles_in_hand) hand = Hand.init_from(bag) del bag tiles_for_bag = [ Tile(Color.PURPLE, Shape.CIRCLE), Tile(Color.RED, Shape.X), Tile(Color.ORANGE, Shape.DIAMOND), Tile(Color.YELLOW, Shape.SQUARE), Tile(Color.GREEN, Shape.STAR), Tile(Color.BLUE, Shape.CLOVER), ] bag = Bag(tiles_for_bag) hand.fill_from(bag) assert set(hand.tiles) == set(tiles_in_hand) assert len(bag.tiles) == 6 assert len(hand.tiles) == 6
def test_exchange_missing_tile_raises(): tiles_in_hand = [ Tile(Color.RED, Shape.CIRCLE), Tile(Color.ORANGE, Shape.X), Tile(Color.YELLOW, Shape.DIAMOND), Tile(Color.GREEN, Shape.SQUARE), Tile(Color.BLUE, Shape.STAR), Tile(Color.PURPLE, Shape.CLOVER), ] bag = Bag(tiles_in_hand) hand = Hand.init_from(bag) del bag tiles_for_bag = [ Tile(Color.PURPLE, Shape.CIRCLE), Tile(Color.RED, Shape.X), Tile(Color.ORANGE, Shape.DIAMOND), Tile(Color.YELLOW, Shape.SQUARE), Tile(Color.GREEN, Shape.STAR), Tile(Color.BLUE, Shape.CLOVER), ] bag = Bag(tiles_for_bag) with pytest.raises(MissingTileError): hand.exchange_tiles(2 * [Tile(Color.YELLOW, Shape.DIAMOND)], bag) assert set(tiles_in_hand) == set(hand.tiles) assert set(bag.tiles) == set(tiles_for_bag)
def test_normal_move(): tiles = [Tile(Color.RED, Shape.CLOVER), Tile(Color.RED, Shape.DIAMOND), Tile(Color.GREEN, Shape.SQUARE), Tile(Color.PURPLE, Shape.SQUARE), Tile(Color.YELLOW, Shape.STAR), Tile(Color.PURPLE, Shape.STAR)] hand = Hand(tiles) player1 = Player(hand) player2 = Player([]) # TODO: Illegal board = Board([(Position(0, 0), Tile(Color.RED, Shape.DIAMOND))]) bag = Bag([Tile(Color.GREEN, Shape.CIRCLE)]) game = Game(bag, board, [player2, player1], player1) tiles_and_positions = [(Position(0, 1), Tile(Color.RED, Shape.CLOVER))] game.make_move(tiles_and_positions) assert player1.total_score() == 2 expected_tiles_on_hand = [ Tile(Color.RED, Shape.DIAMOND), Tile(Color.GREEN, Shape.SQUARE), Tile(Color.PURPLE, Shape.SQUARE), Tile(Color.YELLOW, Shape.STAR), Tile(Color.PURPLE, Shape.STAR), Tile(Color.GREEN, Shape.CIRCLE)] assert set(expected_tiles_on_hand) == set(player1.hand.tiles) assert not bag.tiles assert player2 == game.current_player
def test_early_play(): tiles = [ Tile(Color.RED, Shape.CLOVER), Tile(Color.RED, Shape.DIAMOND), Tile(Color.GREEN, Shape.SQUARE), Tile(Color.PURPLE, Shape.SQUARE), Tile(Color.YELLOW, Shape.STAR), Tile(Color.PURPLE, Shape.STAR) ] hand = Hand(tiles) player = Player(hand) board = Board([(Position(0, 0), Tile(Color.RED, Shape.DIAMOND))]) bag = Bag([Tile(Color.GREEN, Shape.CIRCLE)]) tiles_and_positions = [(Position(0, 1), Tile(Color.RED, Shape.CLOVER))] turn = BoardTurn(board, player, bag, tiles_and_positions) turn.execute() assert player.total_score() == 2 expected_tiles_on_hand = [ Tile(Color.RED, Shape.DIAMOND), Tile(Color.GREEN, Shape.SQUARE), Tile(Color.PURPLE, Shape.SQUARE), Tile(Color.YELLOW, Shape.STAR), Tile(Color.PURPLE, Shape.STAR), Tile(Color.GREEN, Shape.CIRCLE) ] assert set(expected_tiles_on_hand) == set(player.hand.tiles) assert not bag.tiles
def make_new_game(cls, num_players, seed=None): bag = Bag.make_default(seed) board = Board() players = [] for _ in range(num_players): players.append(Player.init_from(bag)) starting_player = cls.determine_starting_player(players) return cls(bag, board, players, starting_player)
def test_illegal_attempt_at_passing_both(): tiles = [Tile(Color.RED, Shape.CIRCLE)] hand = Hand(tiles) player = Player(hand) bag = Bag(['Tile in bag']) board = MockBoard(moves=['unwanted move'], possible_moves=[1, 2, 3]) turn = PassTurn(player, bag, board) with pytest.raises(IllegalPass): turn.execute() points = player.total_score() assert points == 0 assert tiles == player.hand.tiles
def test_passing_turn(): tiles = [Tile(Color.RED, Shape.CIRCLE)] hand = Hand(tiles) player = Player(hand) bag = Bag([]) board = MockBoard(moves=[], possible_moves=[1, 2, 3]) turn = PassTurn(player, bag, board) turn.execute() points = player.total_score() assert points == 0 tiles = [Tile(Color.RED, Shape.CIRCLE)] assert tiles == player.hand.tiles
def test_exchange_one_tile(): tiles_in_hand = [ Tile(Color.RED, Shape.CIRCLE), Tile(Color.ORANGE, Shape.X), Tile(Color.YELLOW, Shape.DIAMOND), Tile(Color.GREEN, Shape.SQUARE), Tile(Color.BLUE, Shape.STAR), Tile(Color.PURPLE, Shape.CLOVER), ] bag = Bag(tiles_in_hand) hand = Hand.init_from(bag) del bag tiles_for_bag = [Tile(Color.PURPLE, Shape.CIRCLE)] bag = Bag(tiles_for_bag) hand.exchange_tiles([Tile(Color.RED, Shape.CIRCLE)], bag) expected_tiles_in_hand = tiles_in_hand[1:] expected_tiles_in_hand.append(Tile(Color.PURPLE, Shape.CIRCLE)) assert set(expected_tiles_in_hand) == set(hand.tiles) expected_tiles_in_bag = [Tile(Color.RED, Shape.CIRCLE)] assert set(bag.tiles) == set(expected_tiles_in_bag)
def test_exchanging_one_of_one(): tiles = [Tile(Color.RED, Shape.CIRCLE)] hand = Hand(tiles) player = Player(hand) bag = Bag([Tile(Color.GREEN, Shape.CLOVER)]) tiles_to_exchange = [Tile(Color.RED, Shape.CIRCLE)] turn = ExchangeTilesTurn(player, bag, tiles_to_exchange) turn.execute() expected_hand = [Tile(Color.GREEN, Shape.CLOVER)] assert expected_hand == player.hand.tiles expected_bag = [Tile(Color.RED, Shape.CIRCLE)] assert expected_bag == bag.tiles assert player.total_score() == 0
def test_late_play(): tiles = [Tile(Color.RED, Shape.CLOVER), Tile(Color.RED, Shape.DIAMOND)] hand = Hand(tiles) player = Player(hand) board = Board([(Position(0, 0), Tile(Color.RED, Shape.DIAMOND))]) bag = Bag([]) tiles_and_positions = [(Position(0, 1), Tile(Color.RED, Shape.CLOVER))] turn = BoardTurn(board, player, bag, tiles_and_positions) turn.execute() assert player.total_score() == 2 expected_tiles_on_hand = [Tile(Color.RED, Shape.DIAMOND)] assert set(expected_tiles_on_hand) == set(player.hand.tiles) assert not bag.tiles
def test_fill_empty_hand(): tiles_for_hand = [ Tile(Color.RED, Shape.CIRCLE), Tile(Color.ORANGE, Shape.X), Tile(Color.YELLOW, Shape.DIAMOND), Tile(Color.GREEN, Shape.SQUARE), Tile(Color.BLUE, Shape.STAR), Tile(Color.PURPLE, Shape.CLOVER), ] bag = Bag(tiles_for_hand) hand = Hand([]) hand.fill_from(bag) assert len(hand.tiles) == hand.CAPACITY assert not bag.tiles
def test_init(): tiles_in_bag = [ Tile(Color.RED, Shape.CIRCLE), Tile(Color.ORANGE, Shape.X), Tile(Color.YELLOW, Shape.DIAMOND), Tile(Color.GREEN, Shape.SQUARE), Tile(Color.BLUE, Shape.STAR), Tile(Color.PURPLE, Shape.CLOVER) ] bag = Bag(tiles_in_bag) hand = Hand.init_from(bag) expected = set(tiles_in_bag) result = set(hand.tiles) assert expected == result
def test_pass_once(): tiles = [Tile(Color.RED, Shape.CIRCLE)] hand = Hand(tiles) player1 = Player(hand) player2 = Player([]) player3 = Player([]) bag = Bag([]) board = MockBoard(moves=[], possible_moves=[1, 2, 3]) game = Game(bag, board, [player2, player3, player1], player1) game.pass_round() points = player1.total_score() assert points == 0 tiles = [Tile(Color.RED, Shape.CIRCLE)] assert tiles == player1.hand.tiles assert player2 == game.current_player
def test_exchange_all_tiles(): tiles_in_hand = [ Tile(Color.RED, Shape.CIRCLE), Tile(Color.ORANGE, Shape.X), Tile(Color.YELLOW, Shape.DIAMOND), Tile(Color.GREEN, Shape.SQUARE), Tile(Color.BLUE, Shape.STAR), Tile(Color.PURPLE, Shape.CLOVER), ] bag = Bag(tiles_in_hand) hand = Hand.init_from(bag) del bag tiles_for_bag = [ Tile(Color.PURPLE, Shape.CIRCLE), Tile(Color.RED, Shape.X), Tile(Color.ORANGE, Shape.DIAMOND), Tile(Color.YELLOW, Shape.SQUARE), Tile(Color.GREEN, Shape.STAR), Tile(Color.BLUE, Shape.CLOVER), ] bag = Bag(tiles_for_bag) hand.exchange_tiles(copy(tiles_in_hand), bag) assert set(tiles_for_bag) == set(hand.tiles) assert set(bag.tiles) == set(tiles_in_hand)
def test_exchange_tiles(): tiles = [Tile(Color.RED, Shape.CIRCLE)] hand = Hand(tiles) player1 = Player(hand) player2 = Player([]) # TODO: Illegal state. bag = Bag([Tile(Color.GREEN, Shape.CLOVER)]) board = Board([(Position(0, 0), Tile(Color.RED, Shape.DIAMOND))]) game = Game(bag, board, [player2, player1], player1) tiles_to_exchange = [Tile(Color.RED, Shape.CIRCLE)] game.exchange_tiles(tiles_to_exchange) expected_hand = [Tile(Color.GREEN, Shape.CLOVER)] assert expected_hand == player1.hand.tiles expected_bag = [Tile(Color.RED, Shape.CIRCLE)] assert expected_bag == bag.tiles assert player1.total_score() == 0 assert player2 == game.current_player
def test_last_move(): tiles = [Tile(Color.RED, Shape.CLOVER)] hand = Hand(tiles) player1 = Player(hand, [1, 1]) player2 = Player([]) # TODO: Illegal state board = Board([(Position(0, 0), Tile(Color.RED, Shape.DIAMOND))]) bag = Bag([]) game = Game(bag, board, [player2, player1], player1) tiles_and_positions = [(Position(0, 1), Tile(Color.RED, Shape.CLOVER))] game.make_move(tiles_and_positions) assert 2+6+2 == player1.total_score() expected_tiles_on_hand = [] assert set(expected_tiles_on_hand) == set(player1.hand.tiles) assert not bag.tiles assert game.current_player is None
def test_fill_partially_filled_hand(): tiles_in_hand = [ Tile(Color.RED, Shape.CIRCLE), Tile(Color.ORANGE, Shape.X), Tile(Color.YELLOW, Shape.DIAMOND) ] hand = Hand(tiles_in_hand) tiles_for_bag = [ Tile(Color.RED, Shape.CIRCLE), Tile(Color.ORANGE, Shape.X), Tile(Color.YELLOW, Shape.DIAMOND), Tile(Color.GREEN, Shape.SQUARE), Tile(Color.BLUE, Shape.STAR), Tile(Color.PURPLE, Shape.CLOVER), ] bag = Bag(tiles_for_bag) hand.fill_from(bag) assert set(tiles_in_hand) == set(hand.tiles) assert hand.CAPACITY == len(hand.tiles) assert len(bag.tiles) == 3