def test_game_data_for_public(self): controller = GameController() controller._trump = DurakCard('6H') controller._deck = [DurakCard('7S'), DurakCard('8D')] controller._on_table = [DurakCard('9C'), DurakCard('TH')] controller._discarded = [DurakCard('JD'), DurakCard('QS')] controller._player1.cards = CardSet(cards=(DurakCard('AC'), DurakCard('8S'), DurakCard('KS')), trump=controller._trump) controller._player2.cards = CardSet(cards=(DurakCard('KD'), ), trump=controller._trump) controller._to_move = controller._player1 self.assertDictEqual( controller.get_game_data_for(controller.PLAYER1), controller._get_game_data_for(controller._player1)) self.assertDictEqual( controller.get_game_data_for(controller.PLAYER2), controller._get_game_data_for(controller._player2)) self.assertDictEqual( controller.get_game_data_for(controller.MOVER), controller._get_game_data_for(controller._player1)) self.assertDictEqual( controller.get_game_data_for(controller.RESPONDER), controller._get_game_data_for(controller._player2))
def test_cards_that_can_beat_trump_including_trumps(self): card_set = CardSet( ['7H', 'KH', 'AH', '6S', 'TS', 'QS', '8D', 'AC'], self.trump ) self.assertEqual(card_set.cards_that_can_beat('TH'), [ DurakCard('KH'), DurakCard('AH'), ])
def test_game_data_for_private(self): controller = GameController() controller._trump = DurakCard('6H') controller._deck = [DurakCard('7S'), DurakCard('8D')] controller._on_table = [DurakCard('9C'), DurakCard('TH')] controller._discarded = [DurakCard('JD'), DurakCard('QS')] controller._player1.cards = CardSet(cards=(DurakCard('AC'), DurakCard('8S'), DurakCard('KS')), trump=controller._trump) controller._player2.cards = CardSet(cards=(DurakCard('KD'), ), trump=controller._trump) self.assertDictEqual( controller._get_game_data_for(controller._player1), { 'trump': str(controller._trump), 'deck_count': len(controller._deck), 'enemy_count': len(controller._player2.cards), 'on_table': map(str, controller._on_table), 'discarded': map(str, controller._discarded), }) self.assertDictEqual( controller._get_game_data_for(controller._player2), { 'trump': str(controller._trump), 'deck_count': len(controller._deck), 'enemy_count': len(controller._player1.cards), 'on_table': map(str, controller._on_table), 'discarded': map(str, controller._discarded), })
def test_successful_register_give_more(self): controller = GameController() controller._state = controller.States.GIVING_MORE controller._no_response = True controller._to_move = controller._player1 controller._deck = [DurakCard('JH')] controller._trump = DurakCard('6H') controller._player1.cards = CardSet(cards=(DurakCard('AC'), DurakCard('6S'), DurakCard('6H')), trump=controller._trump) controller._player2.cards = CardSet(cards=(DurakCard('7C'), DurakCard('7H'), DurakCard('7D')), trump=controller._trump) controller._on_table = Table([DurakCard('6D')]) controller.register_give_more([DurakCard('6S'), DurakCard('6H')]) self.assertItemsEqual(controller._player1.cards, [DurakCard('AC')]) self.assertSequenceEqual(controller._on_table, [DurakCard('6D')]) self.assertItemsEqual( controller._on_table.given_more, [DurakCard('6S'), DurakCard('6H')]) self.assertEqual(controller._state, controller.States.DEALING)
def test_cards_that_can_beat_trump_including_trumps(self): card_set = CardSet(['7H', 'KH', 'AH', '6S', 'TS', 'QS', '8D', 'AC'], self.trump) self.assertEqual(card_set.cards_that_can_beat('TH'), [ DurakCard('KH'), DurakCard('AH'), ])
def test_card_groups_not_including_trumps(self): card_set = CardSet(['7H', '8S', '8C', '9C', '6H', '6C', '6D'], self.trump) self.assertEqual(card_set.card_groups(including_trumps=False), [ {DurakCard('6C'), DurakCard('6D')}, {DurakCard('8S'), DurakCard('8C')}, ])
def test_register_give_more_is_error_if_player_can_not_give_these_cards( self): controller = GameController() controller._state = controller.States.GIVING_MORE controller._no_response = True controller._to_move = controller._player1 controller._trump = DurakCard('6H') controller._player1.cards = CardSet(cards=(DurakCard('AC'), DurakCard('AH'), DurakCard('6H')), trump=controller._trump) controller._player2.cards = CardSet(cards=( DurakCard('7C'), DurakCard('7H'), DurakCard('7D'), DurakCard('7S'), ), trump=controller._trump) controller._on_table = [DurakCard('6D')] with self.assertRaises(exes.InvalidCard): controller.register_give_more( [DurakCard('AC'), DurakCard('AH'), DurakCard('6H')])
def test_card_groups_not_including_trumps(self): card_set = CardSet( ['7H', '8S', '8C', '9C', '6H', '6C', '6D'], self.trump ) self.assertEqual(card_set.card_groups(including_trumps=False), [ {DurakCard('6C'), DurakCard('6D')}, {DurakCard('8S'), DurakCard('8C')}, ])
def test_sorted_cards_returns_trumps_last(self): card_set = CardSet(['7H', '8S', 'TC', '9C', '6H'], self.trump) self.assertEqual(card_set.sorted_cards(), [ DurakCard('8S'), DurakCard('9C'), DurakCard('TC'), DurakCard('6H'), DurakCard('7H'), ])
def test_cards_that_can_be_added_to_not_including_trumps(self): card_set = CardSet(['7H', '7S', 'AH', 'AD', 'TS', 'QS', '8D', 'AC'], self.trump) results = card_set.cards_that_can_be_added_to(['9C', '7D', 'AS'], including_trumps=False) self.assertEqual(results, [ DurakCard('7S'), DurakCard('AC'), DurakCard('AD'), ])
def test_check_for_game_over_is_draw_if_both_players_have_no_cards(self): controller = GameController() controller._trump = DurakCard('6H') controller._deck = [] controller._player1.cards = CardSet(cards=(), trump=controller._trump) controller._player2.cards = CardSet(cards=(), trump=controller._trump) controller._state = controller.States.MOVING controller._logger_enabled = False controller._check_for_game_over() self.assertTrue(controller._state is None) self.assertTrue(controller._winner is None)
def test_cards_that_can_be_added_to_not_including_trumps(self): card_set = CardSet( ['7H', '7S', 'AH', 'AD', 'TS', 'QS', '8D', 'AC'], self.trump ) results = card_set.cards_that_can_be_added_to( ['9C', '7D', 'AS'], including_trumps=False ) self.assertEqual(results, [ DurakCard('7S'), DurakCard('AC'), DurakCard('AD'), ])
def test_check_for_game_over_does_nothing_if_responder_can_beat_with_last_card( self): controller = GameController() controller._state = controller.States.RESPONDING controller._deck = [] controller._trump = DurakCard('6H') controller._player1.cards = CardSet(cards=(), trump=controller._trump) controller._player2.cards = CardSet(cards=(DurakCard('KD'), ), trump=controller._trump) controller._to_move = controller._player1 controller._on_table = [DurakCard('QD')] controller._check_for_game_over() self.assertEqual(controller._state, controller.States.RESPONDING)
def test_check_for_game_over_does_nothing_if_both_players_have_cards(self): controller = GameController() controller._trump = DurakCard('6H') controller._deck = [] controller._player1.cards = CardSet(cards=(DurakCard('AC'), DurakCard('8S')), trump=controller._trump) controller._player2.cards = CardSet(cards=(DurakCard('KD'), DurakCard('KS')), trump=controller._trump) controller._state = controller.States.MOVING controller._check_for_game_over() self.assertEqual(controller._state, controller.States.MOVING)
def test_get_first_to_move_by_trump_if_only_no_trumps_exist(self): controller = GameController() controller._trump = DurakCard('6H') controller._player1.cards = CardSet(cards=(DurakCard('AC'), DurakCard('8S')), trump=controller._trump) controller._player2.cards = CardSet(cards=(DurakCard('KS'), DurakCard('KD')), trump=controller._trump) with patch('durak.controller.random') as random_mock: result = controller._get_first_to_move_by_trump() self.assertEqual(result, random_mock.choice.return_value) random_mock.choice.assert_called_once_with( [controller._player1, controller._player2])
def deal(self): if self._state != self.States.DEALING: raise exes.InvalidAction(expected=self._state, got=self.States.DEALING) if self._logger_enabled: self._logger.log_after_move(self._on_table, self._on_table.given_more) player1_cards_before = CardSet(self._player1.cards, trump=self._trump) player2_cards_before = CardSet(self._player2.cards, trump=self._trump) if self._no_response: self._to_respond.cards.update(self._on_table) self._to_respond.cards.update(self._on_table.given_more) self._no_response = False else: self._to_move = self._to_respond self._discarded.extend(self._on_table) self._on_table.clear() self._state = self.States.MOVING for player in (self._to_respond, self._to_move): if not self._deck: break cards_needed = 6 - len(player.cards) if cards_needed > 0: player.cards.update(self._deck[:cards_needed]) self._deck = self._deck[cards_needed:] self._check_for_game_over() if not self.is_game_over(): if self._logger_enabled: self._logger.log_before_move( set(self._player1.cards), set(self._player2.cards), self.to_move, self.deck_count, ) return { 'player1_cards': (CardSet(self._player1.cards, trump=self._trump) - player1_cards_before), 'player2_cards': (CardSet(self._player2.cards, trump=self._trump) - player2_cards_before), }
def test_register_response_not_allows_moving_if_moving_has_no_cards(self): controller = GameController() controller._state = controller.States.RESPONDING controller._to_move = controller._player2 controller._trump = DurakCard('6H') controller._deck = [DurakCard('JH')] controller._player1.cards = CardSet(cards=(DurakCard('AC'), DurakCard('AH')), trump=controller._trump) controller._player2.cards = CardSet(cards=(), trump=controller._trump) controller._on_table = [DurakCard('6D')] controller.register_response(DurakCard('AH')) self.assertEqual(controller.state, controller.States.DEALING)
def test_register_give_more_is_error_if_max_count_is_exceeded(self): controller = GameController() controller._state = controller.States.GIVING_MORE controller._no_response = True controller._to_move = controller._player1 controller._trump = DurakCard('6H') controller._player1.cards = CardSet(cards=(DurakCard('AC'), DurakCard('AH')), trump=controller._trump) controller._player2.cards = CardSet(cards=(DurakCard('7C'), ), trump=controller._trump) controller._on_table = [DurakCard('6D')] with self.assertRaises(exes.TooMuchGiveMoreCards): controller.register_give_more([DurakCard('6S'), DurakCard('6H')])
def test_get_first_to_move_by_trump_if_both_trumps_exist(self): controller = GameController() controller._trump = DurakCard('6H') controller._player1.cards = CardSet(cards=(DurakCard('7H'), DurakCard('8S')), trump=controller._trump) controller._player2.cards = CardSet(cards=(DurakCard('9H'), DurakCard('KD')), trump=controller._trump) self.assertEqual(controller._get_first_to_move_by_trump(), controller._player1) controller._player1.cards = CardSet(cards=(DurakCard('AH'), DurakCard('8S')), trump=controller._trump) self.assertEqual(controller._get_first_to_move_by_trump(), controller._player2)
def test_init_accepts_cards_and_trump(self): cards = [DurakCard('7', 'S'), 'TD', DurakCard('KC')] card_set = CardSet(cards, self.trump) self.assertEqual(card_set._trump, self.trump) self.assertItemsEqual( card_set, {DurakCard('7S'), DurakCard('TD'), DurakCard('KC')})
def start_new_game(self, ignore_winner=True): self._deck = list(DurakCard.all()) random.shuffle(self._deck) self._trump = self._deck[-1] self._logger.reset() if self._logger_enabled: self._logger.log_before_game(self._player1.name, self._player2.name, self._deck, self._trump) self._player1.cards = CardSet(cards=self._deck[:6], trump=self._trump) self._player2.cards = CardSet(cards=self._deck[6:12], trump=self._trump) self._deck = self._deck[12:] if not ignore_winner and self._winner is not None: self._to_move = self._winner else: self._to_move = self._get_first_to_move_by_trump() self._winner = None self._discarded = [] self._on_table = Table() self._state = self.States.MOVING self._no_response = False if self._logger_enabled: self._logger.log_before_move( set(self._player1.cards), set(self._player2.cards), self.to_move, self.deck_count, ) return { 'player1_cards': CardSet(self._player1.cards, trump=self._trump), 'player2_cards': CardSet(self._player2.cards, trump=self._trump), 'trump': DurakCard(self._trump), }
def test_register_response_is_error_if_player_can_not_add_this_card(self): controller = GameController() controller._state = controller.States.RESPONDING controller._to_move = controller._player2 controller._trump = DurakCard('6H') controller._player1.cards = CardSet(cards=(DurakCard('AC'), DurakCard('AH')), trump=controller._trump) controller._on_table = [DurakCard('6D')] with self.assertRaises(exes.InvalidCard): controller.register_response(DurakCard('AC'))
def test_register_response_goes_to_giving_more_if_card_is_none(self): controller = GameController() controller._state = controller.States.RESPONDING controller._to_move = controller._player1 controller._trump = DurakCard('6H') controller._player1.cards = CardSet(cards=(DurakCard('AC'), ), trump=controller._trump) controller.register_response(None) self.assertEqual(controller._state, controller.States.GIVING_MORE) self.assertTrue(controller._no_response)
def test_register_response_goes_to_dealing_if_card_is_none_and_mover_has_no_cards( self): controller = GameController() controller._state = controller.States.RESPONDING controller._to_move = controller._player1 controller._trump = DurakCard('6H') controller._player1.cards = CardSet(cards=(), trump=controller._trump) controller.register_response(None) self.assertEqual(controller._state, controller.States.DEALING) self.assertTrue(controller._no_response)
def test_register_move_is_error_if_player_does_have_this_card(self): controller = GameController() controller._state = controller.States.MOVING controller._to_move = controller._player1 controller._trump = DurakCard('6H') controller._player1.cards = CardSet(cards=(DurakCard('AC'), DurakCard('AH')), trump=controller._trump) controller._on_table = [] with self.assertRaises(exes.PlayerDoesNotHaveCard): controller.register_move(DurakCard('6D'))
def test_check_for_game_over_player_wins_if_has_no_cards(self): controller = GameController() controller._trump = DurakCard('6H') controller._deck = [] controller._player1.cards = CardSet(cards=(), trump=controller._trump) controller._player2.cards = CardSet(cards=(DurakCard('KD'), DurakCard('KS')), trump=controller._trump) controller._state = controller.States.MOVING controller._logger_enabled = False controller._check_for_game_over() self.assertTrue(controller._state is None) self.assertEqual(controller._winner, controller._player1) controller._player1.cards = CardSet(cards=(DurakCard('AC'), DurakCard('8S')), trump=controller._trump) controller._player2.cards = CardSet(cards=(), trump=controller._trump) controller._state = controller.States.MOVING controller._check_for_game_over() self.assertTrue(controller._state is None) self.assertEqual(controller._winner, controller._player2)
def test_register_response_moves_card_from_player_to_table(self): controller = GameController() controller._state = controller.States.RESPONDING controller._to_move = controller._player2 controller._trump = DurakCard('6H') controller._deck = [DurakCard('JH')] controller._player1.cards = CardSet(cards=(DurakCard('AC'), DurakCard('AH')), trump=controller._trump) controller._on_table = [DurakCard('6D')] controller.register_response(DurakCard('AH')) self.assertItemsEqual(controller._player1.cards, [DurakCard('AC')]) self.assertEqual(controller._on_table, [DurakCard('6D'), DurakCard('AH')])
def test_successful_register_move(self): controller = GameController() controller._state = controller.States.MOVING controller._to_move = controller._player1 controller._trump = DurakCard('6H') controller._deck = [DurakCard('6H')] controller._player1.cards = CardSet(cards=(DurakCard('AC'), DurakCard('7H')), trump=controller._trump) controller._on_table = [DurakCard('AD'), DurakCard('AS')] controller.register_move(DurakCard('AC')) self.assertItemsEqual(controller._player1.cards, (DurakCard('7H'), )) self.assertEqual(controller._on_table, [DurakCard('AD'), DurakCard('AS'), DurakCard('AC')]) self.assertEqual(controller._state, controller.States.RESPONDING)
def cards(self): return CardSet(self._buttons_dict.iterkeys(), self._trump)
def test_lowest_trump_is_none_if_no_trumps(self): card_set = CardSet(['7S', 'AD', 'TS', 'QS', '8D', 'AC'], self.trump) self.assertTrue(card_set.lowest_trump() is None)
def init(self, trump): self._cards = CardSet([], trump) return 'ok'
class DummyEngine(BaseEngine): def init(self, trump): self._cards = CardSet([], trump) return 'ok' def deal(self, cards, gamedata): self._cards.update(cards) return 'ok' def move(self, on_table, gamedata): assert self._cards if not on_table: cards = self._cards.cards_that_can_be_added_to(on_table) else: cards = self._cards.cards_that_can_be_added_to( on_table, including_trumps=False ) if cards: result = cards[0] self._cards.remove(result) return result return '' def respond(self, on_table, gamedata): assert self._cards cards = self._cards.cards_that_can_beat(on_table[-1]) if cards: result = cards[0] self._cards.remove(result) return result return '' def give_more(self, on_table, gamedata): assert self._cards max_count = gamedata['enemy_count'] - 1 cards = self._cards.cards_that_can_be_added_to( on_table, including_trumps=False )[:max_count] self._cards.difference_update(cards) return ' '.join(map(str, cards))
def test_lowest_trump_is_none_if_no_trumps(self): card_set = CardSet( ['7S', 'AD', 'TS', 'QS', '8D', 'AC'], self.trump ) self.assertTrue(card_set.lowest_trump() is None)
def test_lowest_trump(self): card_set = CardSet( ['7S', 'AH', 'AD', 'TS', '7H', 'QS', '8D', 'AC'], self.trump ) self.assertEqual(card_set.lowest_trump(), DurakCard('7H'))
def test_cards_that_can_be_added_to_returns_all_cards_if_to_is_empty(self): card_set = CardSet( ['7H', '7S', 'AH', 'AD', 'TS', 'QS', '8D', 'AC'], self.trump ) results = card_set.cards_that_can_be_added_to([]) self.assertEqual(results, card_set.sorted_cards())
def _to_card_set(self, cards): return CardSet(cards, trump=self.opened_trump)
class DummyEngine(BaseEngine): def init(self, trump): self._cards = CardSet([], trump) return 'ok' def deal(self, cards, gamedata): self._cards.update(cards) return 'ok' def move(self, on_table, gamedata): assert self._cards if not on_table: cards = self._cards.cards_that_can_be_added_to(on_table) else: cards = self._cards.cards_that_can_be_added_to( on_table, including_trumps=False) if cards: result = cards[0] self._cards.remove(result) return result return '' def respond(self, on_table, gamedata): assert self._cards cards = self._cards.cards_that_can_beat(on_table[-1]) if cards: result = cards[0] self._cards.remove(result) return result return '' def give_more(self, on_table, gamedata): assert self._cards max_count = gamedata['enemy_count'] - 1 cards = self._cards.cards_that_can_be_added_to( on_table, including_trumps=False)[:max_count] self._cards.difference_update(cards) return ' '.join(map(str, cards))
def test_is_trump(self): card_set = CardSet([], self.trump) self.assertTrue(card_set._is_trump('7H')) self.assertFalse(card_set._is_trump('8S')) self.assertFalse(card_set._is_trump('9C')) self.assertFalse(card_set._is_trump('TD'))
def test_not_trumps(self): card_set = CardSet(['7H', '8S', '9C', 'TD', '6H'], self.trump) self.assertEqual( card_set.not_trumps(), {DurakCard('8S'), DurakCard('9C'), DurakCard('TD')} )
def test_cards_that_can_beat_not_trump_not_including_trumps(self): card_set = CardSet( ['7H', 'KH', 'AH', '6S', 'TS', 'QS', '8D', 'AC'], self.trump ) results = card_set.cards_that_can_beat('8S', including_trumps=False) self.assertEqual(results, [DurakCard('TS'), DurakCard('QS')])