def test_check_card_in_hand_before_cost(self, player, other_player): player.mana = 3 card_not_in_hand_and_also_too_expensive = Card(7) player.hand = [Card(1), Card(3)] with pytest.raises(InvalidMove, match=r'(?i).*not in hand.*'): player.attack(other_player, card_not_in_hand_and_also_too_expensive)
def test_rank_num(self): c1 = Card(rank='K', suit='h') self.assertEqual(13, c1.rank_num()) c2 = Card(rank='Q', suit='h') self.assertEqual(12, c2.rank_num()) c3 = Card(rank='8', suit='h') self.assertEqual(8, c3.rank_num()) self.assertGreater(c1.rank_num(), c2.rank_num()) self.assertGreater(c2.rank_num(), c3.rank_num())
def test_when_no_mana_left(self, finish_turn_mock, game, p0): game.attacker = p0 p0.mana = 3 card_that_cost_3_mana = Card(3) p0.hand = [Card(1), card_that_cost_3_mana, Card(7)] finish_turn_mock.assert_not_called() game.play_card(card_that_cost_3_mana) finish_turn_mock.assert_called_once()
def test_describe_key_attributes_of_players( self, game, p0: Player): p0.health = 22 p0.mana_slots = 8 p0.mana = 3 p0_hand = [Card(3), Card(5), Card(1)] p0.hand = p0_hand assert game.status['players']['First']['health'] == 22 assert game.status['players']['First']['mana_slots'] == 8 assert game.status['players']['First']['mana'] == 3 assert game.status['players']['First']['hand'] == p0_hand
def test_other_player_health_gets_to_zero__game_won(self): attacker = Player('attacker', Deck()) victim = Player('victim', Deck()) game = Game(attacker, victim) victim.health = 2 kill_card = Card(4) attacker.mana_slots = 8 attacker.mana = 4 attacker.hand = [Card(2), kill_card, Card(8)] game.attacker = attacker assert game.status['finished'] is False game.play_card(kill_card) assert game.status['finished'] is True assert game.status['winner'] == 'attacker'
def __init__(self, n_cards=5): """Initialize the hand of the player.""" self.states = INIT_ARRAY.copy() self.hand = Hand(0) for _ in range(n_cards): new_card = Card(color=Card.UNK, value=Card.UNK, hand=self.hand) self.hand.add_card(new_card, game_start=True)
def test_deck_not_empty__draw(self, deck_draw_card_mock, player, deck): assert deck.cards != [] card_to_be_drawn = Card(4) deck_draw_card_mock.return_value = card_to_be_drawn player._draw_card() deck_draw_card_mock.assert_called_once() assert card_to_be_drawn in player.hand
def test_when_no_cards_in_hand(self, finish_turn_mock, game, p0): game.attacker = p0 p0.mana = 7 last_card_in_hand = Card(3) p0.hand = [last_card_in_hand] finish_turn_mock.assert_not_called() game.play_card(last_card_in_hand) finish_turn_mock.assert_called_once()
def generate_game_state(self, allowed_moves: Tuple[Moves]) -> State: return State( community_cards=tuple(Card(c.rank, c.suit, c.value) for c in self._community_cards), total_players=self._total_players, total_chips=self._total_players * self._init_chips, nbr_of_active_players=self._players.count() - len(self._players.find_by_move(Moves.FOLD)), current_phase=self._current_phase, is_raising_capped=self._is_raising_capped(), allowed_moves=allowed_moves, pot=self._pot, current_bet=self._current_bet )
def __init__(self): super().__init__() self.users = {} self.matches = {} self.decks = {"main": [], "characters": []} self.actions = { "load_cards": { "f": self.aLoadCards, "p": {} }, "list_matches": { "f": self.aListMatches, "p": {} }, "create_match": { "f": self.aCreateMatch, "p": { "matchname": True, "matchpassword": False } }, "join_match": { "f": self.aJoinMatch, "p": { "matchid": True, "matchpassword": False } }, "leave_match": { "f": self.aLeaveMatch, "p": {} }, "start_match": { "f": self.aStartMatch, "p": { "matchid": True } }, } # Loading cards from files with open("cards.json", 'r') as file: cards = json.loads(file.read()) for data in cards: for card in data["cards"]: self.decks["main"].append(Card(card, data)) with open("characters.json", 'r') as file: cards = json.loads(file.read()) for data in cards: self.decks["characters"].append(Character(data))
def test_hand_full__throw_card_away__overload_special_rule( self, deck_draw_card_mock, player, deck): card_to_be_drawn = Card(4) deck_draw_card_mock.return_value = card_to_be_drawn player.hand = [Card(1), Card(2), Card(3), Card(4), Card(5)] assert len(player.hand) == Player.MAX_HAND_SIZE player._draw_card() deck_draw_card_mock.assert_called_once() assert card_to_be_drawn not in player.hand
def test_get_player_announcements_point_function_should_return_addition_of_announcements_points( self): team2 = Team('Yagodka', (Person('Pesho'), Person('Tosho'))) team2.teammates[1].cards = [ Card('K', 'S'), Card('Q', 'S'), Card('J', 'S'), Card('A', 'S'), Card('7', 'S'), Card('8', 'S'), Card('9', 'S') ] team2.teammates[1].cards = Utls.sort_cards(team2.teammates[1].cards) contracts = 'all trumps' result = Game.get_player_announcements_ploints(contracts, team2.teammates[1]) contracts = 'no trumps' result2 = Game.get_player_announcements_ploints( contracts, team2.teammates[1]) contracts = 'S' result3 = Game.get_player_announcements_ploints( contracts, team2.teammates[1]) contracts = 'H' result4 = Game.get_player_announcements_ploints( contracts, team2.teammates[1]) self.assertEqual(result, 90) self.assertEqual(result2, 0) self.assertEqual(result3, 90) self.assertEqual(result4, 70)
def test_poker(self) -> None: self.prepare_game( [ Card('2', 'Spade', 1), Card('2', 'Club', 1), Card('Ace', 'Heart', 13), Card('Ace', 'Diamond', 13), Card('King', 'Spade', 12), ], [Card('2', 'Heart', 1), Card('2', 'Heart', 1)], [Card('Ace', 'Club', 13), Card('Ace', 'Spade', 13)], [Card('King', 'Club', 12), Card('King', 'Heart', 12)]) self.table._find_players_final_hand() self.assertEqual(FinalHandType.POKER, self.player_1.final_hand_type) self.assertEqual(6600000, self.player_1.score) self.assertEqual(FinalHandType.POKER, self.player_2.final_hand_type) self.assertEqual(85800000, self.player_2.score) self.assertEqual(FinalHandType.FULL_HOUSE, self.player_3.final_hand_type) self.assertEqual(6000013, self.player_3.score)
from game import curse, estate, duchy, province, copper, silver, gold from game import Card, TrashDecision, DiscardDecision # simple actions village = Card('Village', 3, actions=2, cards=1) woodcutter = Card('Woodcutter', 3, coins=2, buys=1) smithy = Card('Smithy', 4, cards=3) festival = Card('Festival', 5, coins=2, actions=2, buys=1) market = Card('Market', 5, coins=1, cards=1, actions=1, buys=1) laboratory = Card('Laboratory', 5, cards=2, actions=1) def chapel_action(game): newgame = game.current_player().make_decision(TrashDecision(game, 0, 4)) return newgame def cellar_action(game): newgame = game.current_player().make_decision(DiscardDecision(game)) card_diff = game.state().hand_size() - newgame.state().hand_size() return newgame.replace_current_state(newgame.state().draw(card_diff)) def warehouse_action(game): newgame = game.current_player().make_decision(DiscardDecision(game, 3, 3)) return newgame def council_room_action(game): return game.change_other_states(delta_cards=1)
from game import Card, CardType, CardLink from game import Resource, ResourceType, Invention, InventionType from game import Point, Coin, Discount, War, WarPoint, CardMatch, StageBonus cards_registry = [ # epoch I ## brown Card( name='CLAY POOL', card_type=CardType.RAW, epoch=1, players_limit=5, rewards=[Resource(produce=[ResourceType.CLAY])], ), Card( name='CLAY POOL', card_type=CardType.RAW, epoch=1, players_limit=3, rewards=[Resource(produce=[ResourceType.CLAY])], ), Card( name='ORE VEIN', card_type=CardType.RAW, epoch=1, players_limit=3, rewards=[Resource(produce=[ResourceType.ORE])], ), Card( name='ORE VEIN', card_type=CardType.RAW,
def test_pair(self) -> None: self.prepare_game( [ Card('2', 'Heart', 1), Card('Ace', 'Diamond', 13), Card('3', 'Diamond', 2), Card('5', 'Club', 4), Card('9', 'Heart', 8), ], [Card('2', 'Spade', 1), Card('King', 'Spade', 12)], [Card('Ace', 'Club', 13), Card('7', 'Heart', 6)], [Card('Queen', 'Diamond', 11), Card('Queen', 'Spade', 11)]) self.table._find_players_final_hand() self.assertEqual(FinalHandType.PAIR, self.player_1.final_hand_type) self.assertEqual(20, self.player_1.score) self.assertEqual(FinalHandType.PAIR, self.player_2.final_hand_type) self.assertEqual(260, self.player_2.score) self.assertEqual(FinalHandType.PAIR, self.player_3.final_hand_type) self.assertEqual(220, self.player_3.score)
def test_two_pairs(self) -> None: self.prepare_game([ Card('2', 'Diamond', 1), Card('3', 'Spade', 2), Card('3', 'Club', 2), Card('King', 'Heart', 12), Card('Ace', 'Diamond', 13), ], [Card('2', 'Spade', 1), Card('6', 'Club', 5)], [Card('6', 'Heart', 5), Card('6', 'Diamond', 5)], [Card('Ace', 'Spade', 13), Card('King', 'Club', 12)]) self.table._find_players_final_hand() self.assertEqual(FinalHandType.TWO_PAIRS, self.player_1.final_hand_type) self.assertEqual(601, self.player_1.score) self.assertEqual(FinalHandType.TWO_PAIRS, self.player_2.final_hand_type) self.assertEqual(1502, self.player_2.score) self.assertEqual(FinalHandType.TWO_PAIRS, self.player_3.final_hand_type) self.assertEqual(3912, self.player_3.score)
def test_straight_flush(self) -> None: self.prepare_game([ Card('3', 'Heart', 2), Card('4', 'Heart', 3), Card('5', 'Heart', 4), Card('6', 'Spade', 5), Card('8', 'Club', 7), ], [Card('Ace', 'Heart', 13), Card('2', 'Heart', 1)], [Card('7', 'Spade', 6), Card('9', 'Diamond', 8)], [Card('6', 'Heart', 5), Card('7', 'Heart', 6)]) self.table._find_players_final_hand() self.assertEqual(FinalHandType.STRAIGHT_FLUSH, self.player_1.final_hand_type) self.assertEqual(86000000, self.player_1.score) self.assertEqual(FinalHandType.STRAIGHT, self.player_2.final_hand_type) self.assertEqual(120000, self.player_2.score) self.assertEqual(FinalHandType.STRAIGHT_FLUSH, self.player_3.final_hand_type) self.assertEqual(129000000, self.player_3.score) self.reset() self.prepare_game( [ Card('Queen', 'Club', 11), Card('Jack', 'Club', 10), Card('10', 'Club', 9), Card('8', 'Diamond', 7), Card('8', 'Spade', 7), ], [Card('Ace', 'Club', 13), Card('King', 'Club', 12)], [Card('9', 'Diamond', 8), Card('8', 'Heart', 7)], [Card('9', 'Club', 8), Card('8', 'Club', 7)]) self.table._find_players_final_hand() self.assertEqual(FinalHandType.ROYAL_FLUSH, self.player_1.final_hand_type) self.assertEqual(286000000, self.player_1.score) self.assertEqual(FinalHandType.STRAIGHT, self.player_2.final_hand_type) self.assertEqual(165000, self.player_2.score) self.assertEqual(FinalHandType.STRAIGHT_FLUSH, self.player_3.final_hand_type) self.assertEqual(236500000, self.player_3.score)
def player_ready_to_attack(self, player, attack_card): player.mana_slots = 8 player.mana = 6 player.hand = [Card(1), attack_card, Card(7)] return player
def generate_deck(self): deck = [] for i in range(8): card = Card(3, "Generic Curse", CardType.CURSE) card.card_ally = Character(0, CharacterType.ALLY) deck.append(card) for i in range(3): card = Card(1, "Cackling Goon", CardType.ALLY) card.card_ally = CacklingGoon() deck.append(card) card = Card(3, "Dragon Form", CardType.EFFECT) card.targeted = True card.code = dragon_form_effect deck.append(card) card = Card(3, "Savage Goon", CardType.ALLY) card.card_ally = Character(4, CharacterType.ALLY) deck.append(card) card = Card(2, "Sinister Goon", CardType.ALLY) card.card_ally = SinisterGoon() deck.append(card) card = Card(0, "Vanish", CardType.EFFECT) card.code = vanish_effect deck.append(card) for i in range(2): card = Card(0, "Malice", CardType.CONDITION) deck.append(card) card = Card(0, "Tyranny", CardType.CONDITION) deck.append(card) card = Card(1, "Raven", CardType.ALLY) card.card_ally = Character(1, CharacterType.ALLY) deck.append(card) card = Card(1, "Spinning Wheel", CardType.ALLY) # CardType.ITEM card.card_ally = Character(0, CharacterType.ALLY) deck.append(card) card = Card(1, "Staff", CardType.ALLY) # CardType.ITEM card.card_ally = Character(0, CharacterType.ALLY) deck.append(card) return deck
def attack_card(self): return Card(3)
def tris(self) -> None: self.prepare_game([ Card('King', 'Club', 13), Card('2', 'Club', 1), Card('8', 'Heart', 7), Card('Ace', 'Diamond', 13), Card('10', 'Spade', 9), ], [Card('2', 'Heart', 1), Card('2', 'Spade', 1)], [Card('8', 'Diamond', 7), Card('8', 'Club', 7)], [Card('Ace', 'Heart', 13), Card('Ace', 'Spade', 13)]) self.table._find_players_final_hand() self.assertEqual(FinalHandType.TRIS, self.player_1.final_hand_type) self.assertEqual(4000, self.player_1.score) self.assertEqual(FinalHandType.TRIS, self.player_2.final_hand_type) self.assertEqual(32000, self.player_2.score) self.assertEqual(FinalHandType.TRIS, self.player_3.final_hand_type) self.assertEqual(52000, self.player_3.score)
def test_card_not_in_hand(self, player, other_player): player.mana = 8 card_not_in_hand = Card(7) player.hand = [Card(1), Card(3)] with pytest.raises(InvalidMove, match=r'(?i).*not in hand.*'): player.attack(other_player, card_not_in_hand)
def test_can_not_attack_self(self, player): with pytest.raises(InvalidMove, match=r'(?i).*can not attack self.*'): player.attack(player, Card(3))
def test_not_enough_mana(self, player, other_player): player.mana = 4 card_too_expensive = Card(7) player.hand = [Card(1), Card(3), card_too_expensive] with pytest.raises(InvalidMove, match=r'(?i).*not enough.*'): player.attack(other_player, card_too_expensive)
def test_state_interpretation(self) -> None: player = Dummy(10) player.receive_cards([Card('7', 'Spade', 6), Card('10', 'Diamond', 9)]) state = State(community_cards=(Card('10', 'Spade', 9), Card('6', 'Diamond', 5), Card('4', 'Club', 3), Card('2', 'Spade', 1), Card('Queen', 'Diamond', 11)), total_players=3, total_chips=30, nbr_of_active_players=3, current_phase=Phases.RIVER, is_raising_capped=False, allowed_moves=(Moves.CHECK, Moves.FOLD, Moves.RAISE), pot=3, current_bet=2) player.spend_chips(4) inter = StateInterpreterV1() interpretable_state = InterpretableState( game_state=state, hand=tuple(player.get_hand()), current_chips_amount=player.get_amount_of_chips()) # H D S C expected = [ 0., 0., 1., 0., # 2 0., 0., 0., 0., # 3 0., 0., 0., 1., # 4 0., 0., 0., 0., # 5 0., 1., 0., 0., # 6 0., 0., 1., 0., # 7 0., 0., 0., 0., # 8 0., 0., 0., 0., # 9 0., 1., 1., 0., # 10 0., 0., 0., 0., # Jack 0., 1., 0., 0., # Queen 0., 0., 0., 0., # King 0., 0., 0., 0., # Ace # CHIPS 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 1., 1., 1., 1., 1., 1., # is raising capped 0., ] actual = inter.interpret(interpretable_state) self.assertEqual(expected, actual)
def test_cards_left(self, _random_module): deck = Deck() deck.cards = [Card(0), Card(0), Card(8)] assert deck.cards_left() == 3 deck.draw_card() assert deck.cards_left() == 2
def test_cards(self): # Aševi self.assertEqual( aces, set([ Card(Suit.KARO, Rank.AS), Card(Suit.PIK, Rank.AS), Card(Suit.TREF, Rank.AS), Card(Suit.HERC, Rank.AS) ])) # Kraljevi self.assertEqual( kings, set([ Card(Suit.KARO, Rank.KRALJ), Card(Suit.PIK, Rank.KRALJ), Card(Suit.TREF, Rank.KRALJ), Card(Suit.HERC, Rank.KRALJ) ])) # Dame self.assertEqual( queens, set([ Card(Suit.KARO, Rank.DAMA), Card(Suit.PIK, Rank.DAMA), Card(Suit.TREF, Rank.DAMA), Card(Suit.HERC, Rank.DAMA) ])) # Desetke self.assertEqual( tens, set([ Card(Suit.KARO, Rank.X), Card(Suit.PIK, Rank.X), Card(Suit.TREF, Rank.X), Card(Suit.HERC, Rank.X) ]))
def test_attack_power_equals_mana_cost(self): cost = 4 card = Card(cost) assert card.mana_cost == cost assert card.attack_power == cost
def test_announcements_list_of_player_returns_expected_consecutive_cards_and_belotes( self): team1 = Team('Malinka', (Person('Ivan'), Person('Gosho'))) team2 = Team('Yagodka', (Person('Pesho'), Person('Tosho'))) team3 = Team('Kapinka', (Person('Panda'), Person('Panda2'))) team1.teammates[0].cards = [ Card('K', 'S'), Card('Q', 'S'), Card('J', 'S'), Card('A', 'S') ] #quarte and belot team1.teammates[0].cards = Utls.sort_cards(team1.teammates[0].cards) team1.teammates[1].cards = [ Card('K', 'S'), Card('Q', 'S'), Card('J', 'S'), Card('A', 'S'), Card('T', 'S') ] team1.teammates[1].cards = Utls.sort_cards(team1.teammates[1].cards) team2.teammates[0].cards = [ Card('K', 'S'), Card('Q', 'S'), Card('J', 'S'), Card('A', 'S'), Card('T', 'S'), Card('9', 'S') ] team2.teammates[0].cards = Utls.sort_cards(team2.teammates[0].cards) team2.teammates[1].cards = [ Card('K', 'S'), Card('Q', 'S'), Card('J', 'S'), Card('A', 'S'), Card('7', 'S'), Card('8', 'S'), Card('9', 'S') ] team2.teammates[1].cards = Utls.sort_cards(team2.teammates[1].cards) team3.teammates[0].cards = [ Card('K', 'S'), Card('Q', 'S'), Card('J', 'S'), Card('A', 'S'), Card('7', 'D'), Card('8', 'D'), Card('9', 'D'), Card('T', 'D') ] team3.teammates[0].cards = Utls.sort_cards(team3.teammates[0].cards) team3.teammates[1].cards = [ Card('K', 'S'), Card('Q', 'S'), Card('J', 'S'), Card('A', 'S'), Card('9', 'S'), Card('9', 'D'), Card('9', 'H'), Card('9', 'C') ] team3.teammates[1].cards = Utls.sort_cards(team3.teammates[1].cards) result = Game.take_player_annoucements(team1.teammates[0]) result2 = Game.take_player_annoucements(team1.teammates[1]) result3 = Game.take_player_annoucements(team2.teammates[0]) result4 = Game.take_player_annoucements(team2.teammates[1]) result5 = Game.take_player_annoucements(team3.teammates[0]) result6 = Game.take_player_annoucements(team3.teammates[1]) self.assertEqual(result, [['Quarte', 'S', '14'], ['Belote', 'S']]) self.assertEqual(result2, [['Quinte', 'S', '14'], ['Belote', 'S']]) self.assertEqual(result3, [['Quinte', 'S', '14'], ['Belote', 'S']]) self.assertEqual( result4, [['Quarte', 'S', '14'], ['Tierce', 'S', '9'], ['Belote', 'S']]) self.assertEqual( result5, [['Quarte', 'D', '10'], ['Quarte', 'S', '14'], ['Belote', 'S']]) self.assertEqual( result6, [['Carre', '9'], ['Quarte', 'S', '14'], ['Belote', 'S']])
def test_invalid_card__throw_error(self, game): card_not_in_game = Card(3) with pytest.raises(GameError, match=r'(?i).*not in hand.*'): game.play_card(card_not_in_game)