def test_card_acceptance(self): temp_card = None for index in range(5): temp_card = Card(index, "trump") temp_card.visit(self.temp_player_zero) self.assertEqual( self.temp_player_zero.get_hand().get_cards_in_hand()[4], temp_card)
def test_on_fill(self): for i in range(4): self.tempPlayers[i].get_hand().play_card_at_index(self.testTrick, 0) self.assertEqual(self.testTrick.get_winning_card(), Card(-1, "null")) for i in range(4): self.assertIs(self.testTrick.get_played_cards_list()[i], Card(-1,"null")) self.assertEqual(self.testTrick.get_points_on_trick(), 0)
def test_can_call_ace_of_hearts(self): Card(0, "trump").visit(self.temp_player) #give the player the QC Card(2, "trump").visit(self.temp_player) #give the player the QS Card(9, "clubs").visit(self.temp_player) #give the player the AH Card(9, "spades").visit(self.temp_player) #give the player the AS self.temp_player._valid_call_list = self.test_call_rule_tree.validate_calls(self.temp_player.get_hand()) self.assertEqual(self.temp_player.get_valid_call_list(), [1,0,0,1,0,1,1,1])
def test_update_trick_winners_list(self): for i in range(4): # (P0, QC), (P1, 7D), (P2, QS), (P3, QH) Card(i, "trump").visit(self.temp_players[i]) for i in range(4): self.temp_players[i].get_hand().play_card_at_index(self.temp_trick, 0) self.assertEqual(self.test_round.get_trick_winners_list()[0], 0) for i in range(4): self.temp_players[i].set_initial_values() for i in range(4): Card(12-i, "hearts").visit(self.temp_players[i]) for i in range(4): self.temp_players[i].get_hand().play_card_at_index(self.temp_trick, 0) self.assertEqual(self.test_round.get_trick_winners_list()[1], 3)
def accept_a_card(self, a_card): if self._winning_card == Card(-1, "null"): self._suit_lead = a_card.get_card_suit() if a_card.visit(self._winning_card) is False: self._winning_player = self._parent_round.get_players_list()[ a_card.get_owning_player()] self._winning_card = a_card self._parent_round.notify_players_of_played_card() self._played_cards_list[a_card.get_owning_player()] = a_card self._points_on_trick += a_card.get_point_value() for card in self._played_cards_list: if card is Card(-1, "null"): return self.on_trick_fill()
def setUp(self): setup_results = general_setup() self.testTrick = setup_results["current_trick"] self.tempPlayers = setup_results["list_of_players"] for i in range(4): Card(9+i, "hearts").visit(self.tempPlayers[i]) del setup_results
def test_on_init(self): for card in self.testTrick.get_played_cards_list(): self.assertIs(card, Card(-1,"null")) self.assertIsNone(self.testTrick.get_suit_lead()) self.assertIsNotNone(self.testTrick.get_parent_round()) self.assertIsNone(self.testTrick.get_winning_player()) self.assertEqual(self.testTrick.get_points_on_trick(), 0)
def test_on_trick_finish(self): self.test_round.set_leading_player(self.temp_players[3]) initial_card_count = 0 for i in range(4): Card(12-i, "clubs").visit(self.temp_players[i]) initial_card_count += len(self.temp_players[i].get_hand().get_cards_in_hand()) for i in range(4): self.temp_players[i].get_hand().play_card_at_index(self.temp_trick, 0) post_trick_card_count = 0 for i in range(4): post_trick_card_count += len(self.temp_players[i].get_hand().get_cards_in_hand()) self.assertEqual(initial_card_count, post_trick_card_count+4) for i in range(4): self.assertEqual(self.test_round.get_trick_history()[i][0][Card(12-i, "clubs").get_card_id()], 1) #The 3rd player will have played the Ace of Clubs, the 2nd player the 10 of Clubs, the 1st the King of Clubs, the 0th the 9 of Clubs self.assertEqual(self.test_round.get_leading_player(), self.temp_players[3]) self.assertEqual(self.test_round._get_point_history()[3][0], 25)
def setUp(self): setup_results = general_setup() self.test_tree = setup_results["card_rules"] self.temp_round = setup_results["current_round"] self.temp_trick = setup_results["current_trick"] self.temp_player = setup_results["list_of_players"][0] self.temp_round.set_leading_player(setup_results["list_of_players"][1]) self.card_list = [] for i in range(15): self.card_list.append(Card(i, "trump")) for i in range(9, 15): self.card_list.append(Card(i, "clubs")) for i in range(9, 15): self.card_list.append(Card(i, "spades")) for i in range(9, 15): self.card_list.append(Card(i, "hearts")) del setup_results
def test_ace_called(self): self.card_list[0].visit( self.temp_player_list[0] ) #give player 1 both black queens and 7, 8, 9 of spades and hearts self.card_list[2].visit(self.temp_player_list[0]) for index in range(12, 15): Card(index, "spades").visit(self.temp_player_list[0]) Card(index, "hearts").visit(self.temp_player_list[0]) for index in range(9, 11): Card(index, "trump").visit( self.temp_player_list[1]) #give player 2 all aces and 10s Card(index, "clubs").visit(self.temp_player_list[1]) Card(index, "spades").visit(self.temp_player_list[1]) Card(index, "hearts").visit(self.temp_player_list[1]) for index in range(3, 9): Card(index, "trump").visit(self.temp_player_list[2] ) #give player 3 a bunch of random trump cards self.card_list[1].visit(self.temp_player_list[2]) #spitzer self.card_list[11].visit(self.temp_player_list[2]) #KD self.temp_round.update_call(self.temp_player_list[0].get_player_id(), 1) for index in range(4): self.temp_player_list[index].determine_potential_partners() self.assertEqual( self.temp_player_list[1].get_potential_partners_list(), [1, 1, 0, 0]) self.assertEqual( self.temp_player_list[0].get_potential_partners_list()[0], 1) for index in range(1, 4): self.assertAlmostEqual( self.temp_player_list[0].get_potential_partners_list()[index], 1 / 3)
def setUp(self): setup_results = general_setup() self.temp_game = setup_results["active_game"] self.temp_deck = setup_results["game_deck"] self.temp_player_list = setup_results["list_of_players"] self.temp_round = setup_results["current_round"] self.test_hand = setup_results["player_zero_hand"] self.current_trick = setup_results["current_trick"] self.card_list = [] for index in range(14): self.card_list.append(Card(index, "trump")) for index in range(9, 15): self.card_list.append(Card(index, "clubs")) for index in range(9, 15): self.card_list.append(Card(index, "spades")) for index in range(9, 15): self.card_list.append(Card(index, "hearts")) del setup_results
def test_potential_partners_history(self): for i in range(4): # (P0, QC), (P1, 7D), (P2, QS), (P3, QH) Card(i, "trump").visit(self.temp_players[i]) for i in range(4): self.temp_players[i].get_hand().play_card_at_index(self.temp_trick, 0) self.assertEqual(self.test_round._get_potential_partners_history()[0][2][0], 1) self.assertEqual(self.test_round._get_potential_partners_history()[1][3][0], 1) self.assertEqual(self.test_round._get_potential_partners_history()[2][0][0], 1) self.assertEqual(self.test_round._get_potential_partners_history()[3][1][0], 1)
def get_game_state_for_play_card(self, a_player_index): game_state = np.zeros((1, 1228), dtype=np.float32) index_of_write = 0 # The cards in hand, (This would be a 32 element list) for card in self._players_list[a_player_index].get_cards_in_hand( ): # this isn't filtering by valid or not game_state[0][card.get_card_id()] = 1 index_of_write = 32 # The cards played to the trick so far, (4*32 element list) for card in self._current_trick.get_played_cards_list(): if card is not Card(-1, "null"): game_state[0][index_of_write + card.get_card_id()] = 1 index_of_write += 32 # The players potential partners list, (4 element list) for i in range(4): game_state[0][index_of_write] = self._players_list[ a_player_index].get_potential_partners_list()[i] index_of_write += 1 # The list of cards played so far, (4*8*32 elements) for player_num in range(4): for trick_num in range(8): for card_num in range(32): game_state[0][index_of_write] = self._trick_history[ player_num][trick_num][card_num] index_of_write += 1 # The call state of the game, (This would be a 4*8 element list) for player_num in range(4): for call_num in range(8): game_state[0][index_of_write] = self._call_matrix[player_num][ call_num] index_of_write += 1 # The normalized list of points taken by each player (4 elements) # TODO: Can we change this to the points taken by each team? normalized_player_point_list = [ player.get_round_points() / 120 for player in self._players_list ] for player_num in range(4): game_state[0][index_of_write] = normalized_player_point_list[ player_num] index_of_write += 1 # The normailzed score list for each player (4 elements) normalized_player_score_list = [ player.get_total_score() / 120 for player in self._players_list ] for player_num in range(4): game_state[0][index_of_write] = normalized_player_score_list[ player_num] index_of_write += 1 return game_state
def test_on_accept_card(self): card_is_none_count = 0 self.tempPlayers[2].get_hand().get_cards_in_hand().pop() # This is currently making the hand binary value incorrect Card(0, "trump").visit(self.tempPlayers[2]) self.tempPlayers[0].get_hand().play_card_at_index(self.testTrick, 0) self.assertIsNotNone(self.testTrick.get_played_cards_list()[0]) self.assertEqual(self.testTrick.get_suit_lead(), "hearts") self.assertEqual(self.testTrick.get_winning_player(), self.tempPlayers[0]) self.tempPlayers[1].get_hand().play_card_at_index(self.testTrick, 0) for card in self.testTrick.get_played_cards_list(): if card is Card(-1,"null"): card_is_none_count += 1 self.assertEqual(card_is_none_count, 2) self.assertEqual(self.testTrick.get_suit_lead(), "hearts") self.assertEqual(self.testTrick.get_winning_player(), self.tempPlayers[0]) self.tempPlayers[2].get_hand().play_card_at_index(self.testTrick, 0) self.assertEqual(self.testTrick.get_suit_lead(), "hearts") self.assertEqual(self.testTrick.get_winning_player(), self.tempPlayers[2])
def test_trick_complete(self): current_round_points = self.temp_player_zero.get_round_points() for index in range(4): Card(index, "trump").visit(self.temp_player_list[index]) for index in range(4): self.temp_player_list[index].get_hand().play_card_at_index( self.current_trick, 0) self.assertEqual(self.temp_player_zero.get_round_points(), 9) self.assertEqual( self.temp_player_zero.get_round_points(), (current_round_points + self.temp_player_zero.get_trick_points())) self.assertEqual( self.temp_player_list[0].get_potential_partners_list(), [1, 0, 1, 0]) self.assertEqual( self.temp_player_list[1].get_potential_partners_list(), [0, 1, 0, 1]) self.assertEqual( self.temp_player_list[2].get_potential_partners_list(), [1, 0, 1, 0]) self.assertEqual( self.temp_player_list[3].get_potential_partners_list(), [0, 1, 0, 1])
def play_card(self, a_player, a_game): def can_play_a_card_in_id_list(an_id_list, index_filter_list): for card_id in an_id_list: if valid_binary_value & 1 << card_id != 0: card_index = self.find_card_index_in_hand( players_card_list, card_id) if card_index in index_filter_list: return card_index return -1 # NOTE: This presupposes that the card it finds will be valid to play, that condition must be verified outside this function! def get_index_of_first_card_in(binary_value, is_reversed=False): card_to_play_id = 0 while binary_value % 2 != 1: # this will check if the least signifiant digit in binary_value is not a 1, # which means that the players hand doesn't have the card with card_to_play_id card_to_play_id += 1 binary_value = binary_value >> 1 # shift the reversed_binary_value down to the next digit and continue looking if binary_value == 0: # no card could be found so return -1 return -1 if not is_reversed: return self.find_card_index_in_hand(players_card_list, card_to_play_id) else: return self.find_card_index_in_hand(players_card_list, 30 - card_to_play_id) # lookup what index that id is at and return that value aces_ids = [14, 20, 26] # don't include the ace of diamonds in here tens_ids = [10, 15, 21, 27] kings_ids = [11, 16, 22, 28] current_trick = a_game.get_trick() current_winning_card = current_trick.get_winning_card() players_card_list = a_player.get_cards_in_hand() number_of_cards_on_trick = sum([ 1 for i in range(4) if current_trick.get_played_cards_list()[i] is not Card( -1, "null") ]) valid_indices = [ index for index in range(len(a_player.get_valid_play_list())) if a_player.get_valid_play_list()[index] != 0 ] #^ this will generate a list containing all the indices that are valid cards to play valid_binary_value = sum([ 1 << players_card_list[index].get_card_id() for index in valid_indices ]) # NOTE: This might be useful for the ML agent #^ this will generate a number whos binary representation has 1's in the locations of the id numbers of the cards that are valid to play winning_card_indecies = [ index for index in range(len(players_card_list)) if (not players_card_list[index].visit(current_winning_card)) ] #^ this will generate a list of all the indecies of cards that can be played to take the trick with, can be empty reversed_binary_value = int( format(valid_binary_value, '#034b')[:2:-1], 2) #^ reversing the binary value so the weakest card can be picked out if len(valid_indices) == 1: return valid_indices[0] #If has a fail ace that can take the trick play that ace ace_index = can_play_a_card_in_id_list(aces_ids, winning_card_indecies) if ace_index != -1: return ace_index # If leading or 2nd: if no ace, is trump_strength > 550, if yes play highest trump card I can, if no play weakest card(highest rank/index) if number_of_cards_on_trick == 0 or number_of_cards_on_trick == 1: if self._trump_strength >= 550: valid_trump_value = valid_binary_value & 0b00000000000000000011111111111111 if valid_trump_value != 0: return get_index_of_first_card_in(valid_trump_value) return get_index_of_first_card_in(reversed_binary_value, True) # is the winning player my partner? if yes then smear, that is play points to the trick if a_player.get_potential_partners_list()[ current_winning_card.get_owning_player()] == 1: smear_lists = [tens_ids, aces_ids, kings_ids] for card_list in smear_lists: card_index = can_play_a_card_in_id_list( card_list, valid_indices) if card_index != -1: return card_index # If 3rd: if points on trick > 15 and I can take the trick, play the strongest card I can do so with, else throw off weakest card if number_of_cards_on_trick == 2: if current_trick.get_points_on_trick() >= 15 and len( winning_card_indecies) > 0: return get_index_of_first_card_in(valid_binary_value) return get_index_of_first_card_in(reversed_binary_value, True) # If last: does any card I can play take the trick? if yes play the card that can do so with the highest point value, if no throw off worst(highest rank/index) card if number_of_cards_on_trick == 3: if len(winning_card_indecies) > 0: card_list_sorted_by_points = sorted( players_card_list, key=lambda card: card.get_point_value(), reverse=True) # this list is now the cards the player has sorted so the highest point cards are first for index in range(len(card_list_sorted_by_points)): card_at_index_id = card_list_sorted_by_points[ index].get_card_id() card_unsorted_index = can_play_a_card_in_id_list( [card_at_index_id], winning_card_indecies) if card_unsorted_index != -1: return card_unsorted_index return get_index_of_first_card_in(reversed_binary_value, True)
def set_intial_values(self): self._winning_player = None self._winning_card = Card(-1, "null") self._suit_lead = None self._played_cards_list = [Card(-1, "null") for i in range(4)] self._points_on_trick = 0