def test_get_legal_actions_when_cannot_put_same_suit_nor_beat(self): num_players = 3 np_random = np.random.RandomState() cur_round = Round(num_players, np_random) player = Player(0, np_random) cards = { Card(Spades, Ten), Card(Diamonds, Nine), Card(Spades, Queen), } player.hand = cards stock = [(2, Card(Clubs, Ace))] active_marriage = Hearts expected = cards result = cur_round.get_legal_actions(stock, active_marriage, player) self.assertSetEqual(expected, result)
def test_get_legal_actions_when_stock_empty(self): num_players = 3 np_random = np.random.RandomState() cur_round = Round(num_players, np_random) player = Player(0, np_random) cards = { Card(Spades, Ten), Card(Diamonds, Nine), Card(Hearts, Queen), Card(Spades, King), Card(Clubs, Ace), } player.hand = cards stock = [] active_marriage = None expected = cards result = cur_round.get_legal_actions(stock, active_marriage, player) self.assertSetEqual(expected, result)
def test_deal_cards(self): np_random = np.random.RandomState() players = [Player(player_id, np_random) for player_id in range(3)] dealer = Dealer(np_random) dealer.shuffle() for player_id in range(3): dealer.deal_cards(players[player_id], 8) self.assertEqual(8, len(players[player_id].hand)) self.assertRaises(ImpossibleCardsDealException, lambda: dealer.deal_cards(players[0], 8))
def init_game(self) -> Tuple[Dict, int]: """ Initialize the game of Thousand Schnapsen This version supports three-player and four-players Thousand Schnapsen game (in four-player version dealer sits) Returns: (tuple): Tuple containing: (dict): The first state of the game (int): Current player's id """ # Initialize a dealer that can deal cards self.dealer = Dealer(self.np_random) # Initialize three players to play the game self.players = [ Player(player_id, self.np_random) for player_id in range(self.num_players) ] # Initialize a judger class which will decide who wins in the end self.judger = Judger(self.np_random) # Shuffle and deal cards self.dealer.shuffle() for player in self.players: self.dealer.deal_cards(player, CARDS_PER_PLAYER_COUNT) # Init game state (we assume for now that player with id 0 always starts) self.game_pointer = 0 self.stock = [] self.active_marriage = None self.used_marriages = set() # Initialize a round self.round = Round(self.num_players, self.np_random) # Count the round. There are 8 rounds in each game. self.round_counter = 1 # Save the history of actions for stepping back to the last state. self.history = [] player_state = self.get_state(self.game_pointer) return player_state, self.game_pointer
def test_proceed_round(self): num_players = 3 np_random = np.random.RandomState() cur_round = Round(num_players, np_random) game_pointer = 0 players = [ Player(player_id, np_random) for player_id in range(num_players) ] stock = [(2, Card(Diamonds, Ten))] used_marriages = set() card = Card(Diamonds, Queen) players[game_pointer].hand = [card] expected = (1, None) result = cur_round.proceed_round(game_pointer, players, stock, used_marriages, card) self.assertEqual(expected, result) self.assertEqual((game_pointer, card), stock[1]) self.assertEqual(0, len(players[game_pointer].hand))
def test_proceed_round_when_marriage_activated(self): num_players = 3 np_random = np.random.RandomState() cur_round = Round(num_players, np_random) game_pointer = 0 players = [ Player(player_id, np_random) for player_id in range(num_players) ] stock = [] used_marriages = set() card = Card(Diamonds, Queen) players[game_pointer].hand = [card, Card(Diamonds, King)] players[game_pointer].points = 0 expected = (1, Diamonds) result = cur_round.proceed_round(game_pointer, players, stock, used_marriages, card) self.assertEqual(expected, result) self.assertEqual((game_pointer, card), stock[0]) self.assertEqual(80, players[game_pointer].points) self.assertEqual(1, len(players[game_pointer].hand))