def test_card_array_zero_vector(self): """ Test with zero vector representations. """ self.assertTrue( np.all(get_cards_array(4, 0) == np.zeros(NUM_CARD_VALUES))) self.assertTrue( np.all(get_cards_array(0, 0) == np.zeros(NUM_CARD_VALUES))) self.assertFalse( np.all(get_cards_array(0, 4) == np.zeros(NUM_CARD_VALUES)))
def decide_action_to_take(self, already_played, board, always_use_best, print_luck, possible_actions): """ Returns (possible_qvalues, action_index, action_taken, random_choice, best_decision_made_randomly) """ # Ask for action while True: cmd = input( "Enter your move (<card_type> <num_cards> [<num_jokers>]; or 'pass'): " ) if cmd == "pass": if not np.all(board == 0): action_index = 0 break else: print("Invalid move.") continue try: cards_to_play = list(map(int, cmd.split())) except: print("Invalid move.") continue if len(cards_to_play) == 2 and cards_to_play[0] >= 1 \ and cards_to_play[0] <= 12 and cards_to_play[1] >= 1 \ and cards_to_play[1] <= self.hand[cards_to_play[0] - 1]: card_array_to_play = get_cards_array(cards_to_play[0] - 1, cards_to_play[1]) elif len(cards_to_play) == 3 and cards_to_play[0] >= 1 \ and cards_to_play[0] <= 12 and cards_to_play[1] >= 0 \ and cards_to_play[1] <= self.hand[cards_to_play[0] - 1] \ and cards_to_play[2] in [0, 1, 2] \ and cards_to_play[1] + cards_to_play[2] >= 1: card_array_to_play = get_cards_array( cards_to_play[0] - 1, cards_to_play[1]) + \ get_cards_array(JOKER, cards_to_play[2]) else: print("Invalid move.") continue if np.any(np.all(card_array_to_play == possible_actions, axis=1)) \ and not np.all(card_array_to_play == board): action_index = np.where( np.all(card_array_to_play == possible_actions, axis=1))[0][0] break else: print("Invalid move.") continue return (None, action_index, possible_actions[action_index], False, False)
def test_card_array_non_zero_vector(self): """ Test with non-zero vector representations. """ self.assertTrue( np.all( get_cards_array(1, 2) == np.array( [0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]))) self.assertTrue( np.all( get_cards_array(4, 3) == np.array( [0, 0, 0, 0, 3, 0, 0, 0, 0, 0, 0, 0, 0]))) self.assertFalse( np.all( get_cards_array(4, 3) == np.array( [0, 2, 0, 0, 3, 0, 0, 0, 0, 0, 0, 0, 0])))
def random_initial_cards(cards_to_use=AVAILABLE_CARDS): """ Random initial state for the game. """ deck = np.array([], dtype=np.int8) for card_type in range(NUM_CARD_VALUES): deck = np.append(deck, np.array( [card_type for _ in range(cards_to_use[card_type])], dtype=np.int8)) np.random.shuffle(deck) chunk = deck.shape[0] // NUM_PLAYERS remainder = deck.shape[0] % NUM_PLAYERS first_player_initialized = False for playerIndex in range(NUM_PLAYERS): beginOfChunk = playerIndex * chunk + min(playerIndex, remainder) endOfChunk = (playerIndex + 1) * chunk + \ min(playerIndex + 1, remainder) player = np.zeros(NUM_CARD_VALUES, dtype=np.int8) for card in deck[beginOfChunk:endOfChunk]: player += get_cards_array(card, 1) if first_player_initialized: player_initial_hands = np.vstack([player_initial_hands, player]) else: first_player_initialized = True player_initial_hands = player return player_initial_hands
def test_agent_has_to_pass(self): """ Agent has to pass his turn to the next player without playing any cards. """ agent = QLearningAgent(0) agent.debug = False hand = get_cards_array(5, 2) agent.start_episode(hand, 0) already_played = get_cards_array(2, 2) board = get_cards_array(2, 2) finished, new_already_played, new_board, _ = \ agent.do_step(already_played, board, 0) self.assertFalse(finished) self.assertTrue(np.all(new_already_played == already_played)) self.assertTrue(np.all(new_board == board)) self.assertTrue(np.all(agent.hand == hand))
def test_perform_final_step(self): """ Performs final step and checks state. """ agent = QLearningAgent(0) agent.debug = False hand = get_cards_array(2, 1) agent.start_episode(hand, 0) already_played = np.zeros(NUM_CARD_VALUES, dtype=np.int8) board = np.zeros(NUM_CARD_VALUES, dtype=np.int8) finished, new_already_played, new_board, _ = \ agent.do_step(already_played, board, 0) self.assertTrue(finished) self.assertFalse(np.all(new_already_played == 0)) self.assertFalse(np.all(new_board == 0)) self.assertTrue(np.all(agent.hand == 0)) self.assertAlmostEqual( agent.qtable.get_qtable_entry(already_played, board, hand).iloc[0, 0], agent.alpha)