Example #1
0
 def test_take_out_cards(self):
     cards = init_54_deck()
     remove_cards = [Card('S', 'A'), Card('BJ', '')]
     res = take_out_cards(cards, remove_cards)
     flag = False
     for card in res:
         if card.get_index() == 'SA' or card.get_index == 'BJ':
             flag = True
     self.assertFalse(flag)
     self.assertEqual(len(cards), len(init_54_deck()) - 2)
Example #2
0
 def test_is_in_cards(self):
     deck54 = init_54_deck()
     deck_standard = init_standard_deck()
     deck54_plus_BJ = init_54_deck()
     deck54_plus_BJ.append(Card('BJ', ''))
     self.assertTrue(is_in_cards(deck54, deck_standard))
     self.assertTrue(is_in_cards(deck54, [Card('BJ', ''), Card('RJ', '')]))
     self.assertFalse(is_in_cards(deck54, [Card('BJ', ''), Card('BJ', '')]))
     self.assertFalse(is_in_cards(deck54, [Card('BJ', ''), Card('BJ', ''), Card('D', '3')]))
     self.assertTrue(is_in_cards(deck54_plus_BJ, [Card('BJ', ''), Card('BJ', ''), Card('D', '3')]))
Example #3
0
    def __init__(self):
        '''Give dealer the deck

        Notes:
            1. deck with 54 cards including black joker and red joker
        '''
        super().__init__()
        self.deck = init_54_deck()
        self.landlord = None
Example #4
0
 def test_get_random_cards(self):
     hand = init_54_deck()
     num = 10
     chosen_cards, remained_cards = get_random_cards(hand, num)
     self.assertEqual(len(chosen_cards), num)
     self.assertEqual(len(remained_cards), len(hand) - num)
     with self.assertRaises(AssertionError):
         get_random_cards(hand, 1000)
     with self.assertRaises(AssertionError):
         get_random_cards(hand, -1)
Example #5
0
    def __init__(self):
        '''Give dealer the deck

        Notes:
            1. deck with 54 cards including black joker and red joker
        '''
        super().__init__()
        self.deck = init_54_deck()
        self.deck.sort(key=functools.cmp_to_key(doudizhu_sort_card))
        self.landlord = None
Example #6
0
 def test_get_cards_from_ranks(self):
     deck = init_54_deck()
     player = Player(0)
     player.hand = deck
     test_ranks = ['A', '2', '3']
     chosen_cards, remained_cards = get_cards_from_ranks(player, test_ranks)
     self.assertEqual(len(chosen_cards), 12)
     for card in chosen_cards:
         flag = True
         if card.rank in test_ranks:
             flag = False
         self.assertFalse(flag)
     self.assertEqual(len(remained_cards), len(deck) - 12)
     self.assertEqual(len(chosen_cards), 12)
Example #7
0
File: game.py Project: yli96/rlcard
    def init_game(self):
        ''' Initialize players and state.

        Returns:
            dict: first state in one game
            int: current player's id
        '''

        # initialize public variables
        self.current_game += 1
        self.game_result = {0: 0, 1: 0, 2: 0}
        self.histories = []
        self.trace = []
        self.played_cards = []
        self.state = {'deck': None, 'seen_cards': None, 'landlord': None,
                      'self': None, 'initial_hand': None, 'trace': self.trace,
                      'played_cards': None, 'others_hand': None,
                      'current_hand': None, 'actions': []}

        # initialize players
        self.players = [Player(num)
                        for num in range(DoudizhuGame.players_num)]

        # initialize round to deal cards and determine landlord
        self.rounder = Round()
        self.rounder.initiate(self.players)
        self.current_player = self.rounder.landlord_num

        # initialize Judger
        self.judger = Judger(self.players)

        # initialize state of landlord to be ready for proceeding round
        player = self.players[self.current_player]
        self.rounder.round_last = get_upstream_player_id(player, self.players)
        deck = init_54_deck()
        deck.sort(key=functools.cmp_to_key(doudizhu_sort_card))
        self.state['deck'] = cards2str(deck)
        self.state['landlord'] = self.rounder.landlord_num
        self.state['self'] = self.current_player
        self.state['hand'] = cards2str(player.hand)
        self.state['seen_cards'] = self.rounder.seen_cards
        self.state['current_hand'] = cards2str(player.current_hand)
        self.state['others_hand'] = self._get_others_current_hand(player)
        self.state['actions'] = list(
            self.judger.playable_cards[self.current_player])
        return copy.deepcopy(self.state), self.current_player
Example #8
0
 def test_is_in_cards(self):
     deck54 = init_54_deck()
     deck_standard = init_standard_deck()
     self.assertTrue(is_in_cards(deck54, deck_standard))
     self.assertTrue(is_in_cards(deck54, [Card('BJ', ''), Card('RJ', '')]))
     self.assertFalse(is_in_cards(deck54, [Card('BJ', ''), Card('BJ', '')]))
Example #9
0
 def test_init_54_deck(self):
     self.assertEqual(len(init_54_deck()), 54)