def new_round(self, state: HeartsState): """ At the end of the round, the points should be distributed properly and the deck should be reshuffled to play again """ # Give the trick_winner their cards before wrapping up this round trick_winner = self.trick_winner(state) for card in self.cards_of_trick(state): state.deck[card] = trick_winner + 10 # Print final results # print("trick winner:", trick_winner, " trick#:", self.trick_number(state) - 1, # " Points: ", self.points(state)) # Update the score self.update_score(state) if self.is_finished(state): # Do not shuffle if game is over return # New round, shuffle cards, and Pass cards state.shuffle() # Make the value positive to show that we will be passing (unless zero because 0 * -1 is 0) if state.pass_type < 0: state.pass_type = state.pass_type * -1 state.pass_type += 1 if state.pass_type > 3: state.pass_type = 0
class TestHeartsState(unittest.TestCase): def setUp(self): self.state = HeartsState() def test_shuffle(self): self.assertIsNotNone(self.state.values) self.assertFalse( np.all(self.state.values == np.array([1] * 13 + [2] * 13 + [3] * 13 + [4] * 13))) def test_set_and_get_encoding(self): self.state.set_encoding(11, 'AH') self.assertEqual(self.state.values[-1], 11) self.assertEqual(self.state.get_encoding('AH'), 11) def test_hide_encoding(self): self.state.set_encoding(12, '5S') self.state.set_encoding(13, 'JD') self.state.set_encoding(21, '6H') expected_p1_state = [i for i in self.state.values] self.assertTrue( np.all(self.state.values == np.array(expected_p1_state))) for x in range(52): if expected_p1_state[x] != 1: expected_p1_state[x] = 0 expected_p1_state[43] = 21 expected_p1_state[22] = 13 expected_p1_state[29] = 12 p1_state = self.state.hide_encoding(1) self.assertTrue(np.all(p1_state == np.array(expected_p1_state)))
def pass_cards(self, action: HeartsAction, player: int, state: HeartsState): """ Handles the passing cards at the beginning of each round. Swaps cards once all 4 players have chosen three cards to pass. :param action: the Action of the Agent whose turn it is :returns unaltered state for non passing round, otherwise returns nothing. """ # Implement pass here # Pass cards to player_to_pass # Pass 3 cards Clockwise(CW) : 1,2,3,4... if state.pass_type == 1: # Add one to the player number to find the player CW to them player_to_pass = player + 1 # Loop around if player_to_pass >= 5: player_to_pass = 1 # Pass 3 cards Counter-Clockwise(CCW) : 4,3,2,1... if state.pass_type == 2: # Subtract one from the player number to find the player CCW to them player_to_pass = player - 1 # Loop around if player_to_pass <= 0: player_to_pass = 4 # Pass 3 cards Straight : 1 <-> 3 and 2 <-> 4 if state.pass_type == 3: # Add two to find the player across player_to_pass = player + 2 # Loop around if player_to_pass >= 5: # If adding two went too far, we should have subtracted two instead player_to_pass = player_to_pass - 4 # Pass Cards for card_i in action.card_index: state.deck[card_i] = player_to_pass # We know all players have passed when the fourth player has since we always start passing with the first player if player == 4: # Make the value negative so we know to no longer pass the cards until the beginning of next trick if state.pass_type > 0: state.pass_type = state.pass_type * -1
def end_of_trick(self, state: HeartsState): """ Function handles the end of a trick which entails tallying up points, determining the trick winner, setting the current player, and incrementing the pass type. """ # Find max card and then find the owner of the card max_card = self.find_max_card(state) trick_winner = self.trick_winner(state) # All of these cards belong to the trick winner (tricks won) for card in self.cards_of_trick(state): state.deck[card] = trick_winner + 10
def worker(var): # for j in range(0, 25): game_uuid = uuid.uuid4().hex adj = HeartsAdjudicator() state = HeartsState() agent_1 = get_agent() agent_2 = get_agent() agent_3 = get_agent() agent_4 = get_agent() agent_list = [0, agent_1, agent_2, agent_3, agent_4] game = GameManager(agent_list, adjudicator=adj, state=state) # Save starting game information save_game(game_uuid, agent_list) # Play a game. game.play_game() # The game is over, save the states of the game into the database. process_state_data(game_uuid, game)
for data in state_data: deck = data["deck"].tolist() action = data["actions"] score = data["scores"] writer.writerow([deck, action, score, game_uuid]) if MYSQL_SERVER: db.insert_state(directory) for i in range(0, 10000): # Create the players, the adjudicator, and the game object. game_uuid = uuid.uuid4().hex adj = HeartsAdjudicator() state = HeartsState() agent_1 = get_agent() agent_2 = get_agent() agent_3 = get_agent() agent_4 = get_agent() agent_list = [0, agent_1, agent_2, agent_3, agent_4] game = GameManager(agent_list, adjudicator=adj, state=state) # Save starting game information save_game() # Play a game. game.play_game() # The game is over, save the states of the game into the database. process_state_data()
def setUp(self): self.state = HeartsState()