示例#1
0
    def _move_turtle(self, action: Action):
        turtle = self._find_turtle(action.get_color())
        if action.does_move_last_turtle() and not self.board.is_last(turtle):
            raise ValueError('Arrow card can move only the last turtle.' +
                             f'Turtle {turtle} is not one of the last turtles')

        self.board.move(turtle, action.get_offset())
def test_first_player_should_be_active_after_two_moves(game):
    first_player = game.players[0]
    second_player = game.players[1]

    game.play(first_player.person, Action(Card(0, 'RED', 'PLUS')))
    game.play(second_player.person, Action(Card(5, 'RED', 'PLUS')))

    assert game.active_player == first_player
def test_first_player_should_be_active_after_three_moves(cards):
    people = [Person(0, 'Piotr'), Person(1, 'Marta'), Person(2, 'Ewa')]
    turtles = [Turtle('GREEN'), Turtle('RED'), Turtle('BLUE')]
    game = Game(people, turtles, cards)

    game.play(people[0], Action(Card(0, 'RED', 'PLUS')))
    game.play(people[1], Action(Card(5, 'RED', 'PLUS')))

    assert game.active_player == game.players[2]
def test_play_should_return_ranking_when_someone_wins(game, people):
    for _ in range(8):
        active = game.active_player
        game.play(active.person, Action(active.cards[0]))

    active = game.active_player
    ranking = game.play(active.person, Action(active.cards[0]))

    assert ranking == [people[1], people[0]]
def test_raises_when_plays_arrow_arrow_incorrect_color(people, cards, turtles):
    player = people[0]
    all_cards = [Card(4 * HAND_SIZE, 'RAINBOW', 'ARROW_ARROW')] + cards
    game = Game(people, turtles, all_cards)

    for _ in range(2):
        active = game.active_player
        game.play(active.person, Action(active.cards[-1]))

    with pytest.raises(ValueError):
        game.play(player,
                  Action(Card(4 * HAND_SIZE, 'RAINBOW', 'ARROW_ARROW'), 'RED'))
def test_play_returns_ranking_when_autonomous_turtle_finishes(cards, people):
    turtles = [Turtle('GREEN'), Turtle('RED'), Turtle('YELLOW')]
    # we add one green card in order to make Piotr win
    all_cards = [Card(4 * HAND_SIZE, 'GREEN', 'PLUS')] + cards
    game = Game(people, turtles, all_cards)

    # one more move is required becuase we moved the green turtle once
    for _ in range(8 + 1):
        active = game.active_player
        game.play(active.person, Action(active.cards[0]))

    active = game.active_player
    ranking = game.play(active.person, Action(active.cards[0]))

    assert ranking == [Person(0, 'Piotr'), Person(1, 'Marta')]
def test_card_played_by_player_should_be_recent_on_stack(cards, people, game):
    player = game.players[0]
    card = Card(0, 'RED', 'PLUS')

    game.play(player.person, Action(card))

    assert game.stacks.get_recent() == card
def test_player_should_not_have_played_card_after_move(cards, people, game):
    player = game.players[0]
    card = Card(0, 'RED', 'PLUS')

    game.play(player.person, Action(card))

    assert card not in player.cards
def test_player_should_keep_getting_hands_until_can_move(people, turtles):
    # Initial state:
    # First player cards: 0,1,2,3,4
    # Second player cards: 5,6,7,8,9
    #
    # First player plays 0 and takes next card (10)
    # Second player can't make any move, so he should take another hand:
    # 11, 12, 13, 14, 15
    # He still can't make any move so he takes another hand:
    # 16, 17, 18, 19, 20

    cards = [Card(i, 'GREEN', 'PLUS') for i in range(HAND_SIZE)]
    cards += [
        Card(i, 'RED', 'MINUS') for i in range(HAND_SIZE, 3 * HAND_SIZE + 1)
    ]
    cards += [
        Card(i, 'GREEN', 'PLUS')
        for i in range(3 * HAND_SIZE, 4 * HAND_SIZE + 1)
    ]

    game = Game(people, turtles, cards)
    player = game.players[0]

    game.play(player.person, Action(Card(0, 'GREEN', 'PLUS')))

    assert game.players[1].cards == cards[3 * HAND_SIZE + 1:4 * HAND_SIZE + 1]
def test_player_should_get_new_card_after_move(game, cards):
    player = game.players[0]

    game.play(player.person, Action(Card(0, 'RED', 'PLUS')))

    next_card_from_stack = cards[2 * HAND_SIZE]
    assert len(player.cards) == HAND_SIZE
    assert player.cards[0] == next_card_from_stack
def test_it_should_be_third_player_turn_after_removing_second_player(cards):
    turtles = [Turtle('GREEN'), Turtle('RED'), Turtle('BLUE')]
    people = (Person(0, 'Piotr'), Person(1, 'Marta'), Person(2, 'Other'))
    game = Game(people, turtles, cards)

    game.play(people[0], Action(Card(0, 'RED', 'PLUS')))
    game.remove_player(people[1])

    assert game.active_player.person == people[2]
def test_next_player_should_get_new_hand_if_he_cant_move(people, turtles):
    # Initial state:
    # First player cards: 0,1,2,3,4
    # Second player cards: 5,6,7,8,9
    #
    # First player plays 0 and takes next card (10)
    # Second player can't make any move, so he should take another hand:
    # 11, 12, 13, 14, 15

    cards = [Card(i, 'GREEN', 'PLUS') for i in range(HAND_SIZE)]
    cards += [Card(i, 'RED', 'MINUS') for i in range(HAND_SIZE, 2 * HAND_SIZE)]
    cards += [
        Card(i, 'GREEN', 'PLUS') for i in range(2 * HAND_SIZE, 4 * HAND_SIZE)
    ]

    game = Game(people, turtles, cards)
    player = game.players[0]

    game.play(player.person, Action(Card(0, 'GREEN', 'PLUS')))

    assert game.players[1].cards == cards[2 * HAND_SIZE + 1:3 * HAND_SIZE + 1]
def test_should_raise_when_player_doesnt_have_given_card(cards, people, game):
    player = people[0]

    with pytest.raises(ValueError):
        game.play(player, Action(Card(42, 'RED', 'ARROW')))
def test_should_raise_when_inactive_player_tries_to_move(cards, people, game):
    inactive_player = people[1]

    with pytest.raises(ValueError):
        game.play(inactive_player, Action(Card(0, 'RED', 'ARROW')))
def test_should_raise_exception_when_other_player_tries_to_move(cards, game):
    other = Person(123, 'Unknown')

    with pytest.raises(ValueError):
        game.play(other, Action(Card(0, 'RED', 'ARROW')))
def test_play_should_return_none_when_there_is_no_winner_yet(game, people):
    player = people[0]

    winner = game.play(player, Action(Card(0, 'RED', 'PLUS')))

    assert winner is None
def test_get_color_should_return_card_color_when_action_color_is_not_set():
    action = Action(Card(0, 'RED', 'PLUS'))

    actual = action.get_color()

    assert actual == 'RED'
def test_should_raise_when_action_color_is_present_and_card_is_not_rainbow():
    with pytest.raises(ValueError):
        Action(Card(0, 'RED', 'PLUS'), 'RED')
def test_get_color_should_return_action_color_when_it_is_present():
    action = Action(Card(0, 'RAINBOW', 'PLUS'), 'RED')

    actual = action.get_color()

    assert actual == 'RED'
def test_does_move_last_turtle_should_return_false_when_card_symbol_is_plus():
    action = Action(Card(0, 'RAINBOW', 'PLUS'), 'RED')

    actual = action.does_move_last_turtle()

    assert not actual
def test_does_move_last_turtle_returns_true_when_card_symbol_is_arrow_arrow():
    action = Action(Card(0, 'RAINBOW', 'ARROW_ARROW'), 'RED')

    actual = action.does_move_last_turtle()

    assert actual
def test_next_player_should_become_active_after_move(game):
    player = game.players[0]

    game.play(player.person, Action(Card(0, 'RED', 'PLUS')))

    assert game.active_player == game.players[1]
def test_init_should_raise_when_color_is_not_set_on_rainbow_card():
    with pytest.raises(ValueError):
        Action(Card(0, 'RAINBOW', 'PLUS'))
def test_init_should_raise_when_color_is_set_on_regular_card():
    with pytest.raises(ValueError):
        Action(Card(0, 'RED', 'PLUS'), 'BLUE')
def test_raises_when_turtle_with_given_color_doesnt_exist(cards, people, game):
    player = people[0]

    with pytest.raises(ValueError):
        game.play(player, Action(Card(0, 'BLUE', 'PLUS')))
def test_corresponding_turtle_should_move(cards, people, game):
    player = people[0]

    game.play(player, Action(Card(0, 'RED', 'PLUS')))

    assert game.board.further_fields[0] == [Turtle('RED')]