def test_last_hand_raise_valid_8_cards(self): # player -1 raised world = WorldTotalWatten() world.played_cards = [0, 1, 2, 3, 4, 5, 6, 7] world.rank = 3 world.suit = 2 world.current_player = -1 world.player_B_hand = [total_watten.get_id(4, 1)] world.player_A_hand = [total_watten.get_id(5, 0)] result = world._last_hand_raise_valid() self.assertFalse(result) world = WorldTotalWatten() world.played_cards = [0, 1, 2, 3, 4, 5, 6, 7] world.rank = 3 world.suit = 2 world.current_player = -1 world.player_B_hand = [total_watten.get_id(4, 2)] world.player_A_hand = [total_watten.get_id(5, 0)] result = world._last_hand_raise_valid() self.assertTrue(result) # player 1 raised world = WorldTotalWatten() world.played_cards = [0, 1, 2, 3, 4, 5, 6, 7] world.rank = 3 world.suit = 2 world.current_player = 1 world.player_B_hand = [total_watten.get_id(4, 1)] world.player_A_hand = [total_watten.get_id(5, 0)] result = world._last_hand_raise_valid() self.assertFalse(result) world = WorldTotalWatten() world.played_cards = [0, 1, 2, 3, 4, 5, 6, 7] world.rank = 3 world.suit = 2 world.current_player = 1 world.player_B_hand = [total_watten.get_id(4, 2)] world.player_A_hand = [total_watten.get_id(5, 2)] result = world._last_hand_raise_valid() self.assertTrue(result)
def test_is_won(self): world = WorldTotalWatten() world.refresh() world.player_A_score = 15 result = world.is_won(-1) self.assertTrue(result) world.refresh() world.player_A_score = 15 world.player_B_score = 15 self.assertRaises(InconsistentStateError, world.is_won, 1) world.refresh() world.player_B_score = 15 result = world.is_won(-1) self.assertFalse(result) world.refresh() world.player_B_score = 15 result = world.is_won(1) self.assertTrue(result)
def test_is_rank_higher(self): world = WorldTotalWatten() world.refresh() # weli played self.assertFalse(world.is_rank_higher(8, 0)) self.assertFalse(world.is_rank_higher(8, 1)) self.assertFalse(world.is_rank_higher(8, 2)) self.assertFalse(world.is_rank_higher(8, 3)) self.assertFalse(world.is_rank_higher(8, 4)) self.assertFalse(world.is_rank_higher(8, 5)) self.assertFalse(world.is_rank_higher(8, 6)) self.assertFalse(world.is_rank_higher(8, 7)) self.assertTrue(world.is_rank_higher(0, 8)) self.assertTrue(world.is_rank_higher(1, 8)) self.assertTrue(world.is_rank_higher(2, 8)) self.assertTrue(world.is_rank_higher(3, 8)) self.assertTrue(world.is_rank_higher(4, 8)) self.assertTrue(world.is_rank_higher(5, 8)) self.assertTrue(world.is_rank_higher(6, 8)) self.assertTrue(world.is_rank_higher(7, 8)) self.assertTrue(world.is_rank_higher(3, 8)) # not weli self.assertTrue(world.is_rank_higher(6, 5)) self.assertTrue(world.is_rank_higher(7, 1)) self.assertFalse(world.is_rank_higher(1, 4)) self.assertFalse(world.is_rank_higher(3, 6)) self.assertFalse(world.is_rank_higher(4, 6)) self.assertFalse(world.is_rank_higher(5, 6))
def test_compare_cards_rechte(self): world = WorldTotalWatten() # if weli is rechte, then it should win world.rank = 8 world.suit = 1 card_id_one = total_watten.get_id(8, 3) card_id_two = total_watten.get_id(5, 1) self.assertTrue(world.compare_cards(card_id_one, card_id_two)) self.assertFalse(world.compare_cards(card_id_two, card_id_one)) world.rank = 5 world.suit = 2 card_id_one = total_watten.get_id(5, 2) card_id_two = total_watten.get_id(4, 2) self.assertTrue(world.compare_cards(card_id_one, card_id_two)) self.assertFalse(world.compare_cards(card_id_two, card_id_one)) # if weli is rechte, then it should win world.rank = 8 world.suit = 1 card_id_one = total_watten.get_id(4, 3) card_id_two = total_watten.get_id(8, 3) self.assertFalse(world.compare_cards(card_id_one, card_id_two))
def test_observe(self): world = WorldTotalWatten() world.current_game_player_A_score = 1 world.current_game_player_B_score = 2 world.player_A_score = 14 world.player_B_score = 1 world.is_last_move_raise = True world.is_last_move_accepted_raise = True world.is_last_hand_raise_valid = False world.current_game_prize = 15 expected_player_A = [1, 0, 0, 1, 0, 0, 0, 0, 0, 0, # 110 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, # 120 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, # 130 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, # 140 0, 0, 0, 0, 0, 0, 0, 1, 0, 0] # 150 expected_player_B = [0, 1, 1, 0, 1, 0, 0, 0, 0, 0, # 110 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, # 120 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, # 130 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, # 140 0, 0, 0, 0, 0, 0, 0, 1, 0, 0] # 150 observation_player_A = world.observe(1, self.agent) observation_player_B = world.observe(-1, self.agent) #testing last part of observation is correct np.testing.assert_array_equal(observation_player_A[32:82], np.array(expected_player_A).reshape((50, 1))) np.testing.assert_array_equal(observation_player_B[32:82], np.array(expected_player_B).reshape((50, 1)))
def test_game_no_raise_player_A_starts(self): world = WorldTotalWatten() world.LOG.setLevel(DEBUG) world.init_world_to_state(-1, 1, 0, 0, [28, 29, 3, 15, 22], [19, 14, 7, 0, 11], [], 0, 0, 2, False, False, None, 18, 21, None, None) # [39, 44, 0, 46, 48, 3, 29, 46, 48, 14, 11, 15] world_copy = world.deepcopy() ######## MOVE ######## valid_moves = world.get_valid_moves() outcome, next_player = world.act(0, self.agent) # rank outcome, next_player = world.act(0, self.agent) # suit outcome, next_player = world.act(0, self.agent) # card outcome, next_player = world.act(1, self.agent) # raise outcome, next_player = world.act(3, self.agent) # accepted raise outcome, next_player = world.act(0, self.agent) # card -- outcome, next_player = world.act(1, self.agent) # raise outcome, next_player = world.act(3, self.agent) # accepted raise outcome, next_player = world.act(0, self.agent) # card -- outcome, next_player = world.act(0, self.agent) # card outcome, next_player = world.act(0, self.agent) # card outcome, next_player = world.act(0, self.agent) # card world.get_valid_moves()
def test_game_complete(self): # [36, 45, 14, 5, 25, 20, 46, 48, 1, 46, 48, 13, 35, 45, 15, 18, 7, 2, # 30, 46, 47, 36, 43, 27, 5, 46, 48, 6, 2, 22, 46, 47, 35, 44, 29, 8, 46, # 48, 18, 21, 27, 26, 14, 11, 9, 46, 48, 17, 34, 43, 10, 1, 46, 47, 41, # 42, 24, 17, 46, 48, 0, 14, 3, 29, 46, 48, 41, 45, 19, 8, 30, 24, 20, # 16, 34, 46, 47, 39, 44, 16, 30, 18, 46, 48, 21, 46, 47, 39, 43, 24, # 26, 14, 22, 29, 6, 5, 7] game = TotalWattenGame(self.agent, self.agent) world = WorldTotalWatten() world.init_world_to_state(1, -1, 0, 0, [25, 9, 1, 32, 14], [5, 13, 7, 10, 20], [], 0, 0, 2, False, False, None, 16, 28, None, None) game.trueboard = world cur_player = game.get_cur_player() self.assertEqual(cur_player, 0) moves = game.get_valid_moves_no_zeros() self.assertEqual(moves, [0, 1]) self.assertEqual(game.make_move(0), (0.0, 1)) cur_player = game.get_cur_player() self.assertEqual(cur_player, 1) moves = game.get_valid_moves_no_zeros() self.assertEqual(moves, [0, 1])
def test_assign_points_single_hand_ended(self): # current player: 1 world = WorldTotalWatten() world.current_player = 1 world.current_game_player_A_score = 1 world.current_game_player_B_score = 2 world._assign_points_move(True) self.assertEqual(world.current_game_player_A_score, 2) self.assertEqual(world.current_game_player_B_score, 2) world = WorldTotalWatten() world.current_player = 1 world.current_game_player_A_score = 1 world.current_game_player_B_score = 2 world._assign_points_move(False) self.assertEqual(world.current_game_player_A_score, 1) self.assertEqual(world.current_game_player_B_score, 3) # current player: 2 world = WorldTotalWatten() world.current_player = -1 world.current_game_player_A_score = 1 world.current_game_player_B_score = 2 world._assign_points_move(True) self.assertEqual(world.current_game_player_A_score, 1) self.assertEqual(world.current_game_player_B_score, 3) world = WorldTotalWatten() world.current_player = -1 world.current_game_player_A_score = 1 world.current_game_player_B_score = 2 world._assign_points_move(False) self.assertEqual(world.current_game_player_A_score, 2) self.assertEqual(world.current_game_player_B_score, 2)
def test_observe(self): world = WorldTotalWatten() world.current_game_prize = 15 result = world.observe(1, self.agent) print(result)
def test_get_valid_moves_normal(self): world = WorldTotalWatten() world.refresh() # opponent player declare the rank # valid moves: MAKE_BEST_MOVE + RAISE POINTS valid_moves = world.get_valid_moves() self.assertEqual([0, 1], valid_moves)
def test_get_valid_moves_last_move_raise(self): world = WorldTotalWatten() world.is_last_move_raise = True valid_moves = world.get_valid_moves() # after a raise a player can only accept it or fold self.assertEqual([2, 3], valid_moves)
def test_get_valid_moves_last_move_accepted_raise(self): world = WorldTotalWatten() world.is_last_move_accepted_raise = True valid_moves = world.get_valid_moves() # after an accepted raise a player can't raise again self.assertNotIn(1, valid_moves)
def test_get_valid_moves_last_move_raise_last_hand(self): world = WorldTotalWatten() world.is_last_move_raise = True world.is_last_hand_raise_valid = True valid_moves = world.get_valid_moves() # after a raise in last hand a player can only accept, fold, or fold and verify conditions for raise in last hand self.assertEqual([2, 3, 4], valid_moves)
def test_is_blinde(self): world = WorldTotalWatten() world.rank = 6 world.suit = 2 self.assertTrue(world.is_blinde(6)) self.assertFalse(world.is_blinde(3)) self.assertFalse(world.is_blinde(1)) self.assertFalse(world.is_blinde(7))
def test_observe_after_raise(self): world = WorldTotalWatten() world.rank = 3 world.suit = 1 world.current_player = 1 result, next_player = world.act(0, self.agent) result, next_player = world.act(1, self.agent) world.observe(next_player, self.agent)
def test_last_hand_raise_valid_error(self): world = WorldTotalWatten() world.played_cards = [0, 1, 2, 3, 4] world.rank = 3 world.suit = 2 world.current_player = 1 world.player_B_hand = [total_watten.get_id(4, 1)] world.player_A_hand = [total_watten.get_id(5, 0)] self.assertRaises(InconsistentStateError, world._last_hand_raise_valid)
def test_best_move_declares_rank_2(self): world = WorldTotalWatten() world.current_player = -1 result, next_player = world.act(0, self.agent) self.assertEqual(result, "continue") self.assertEqual(next_player, 1) self.assertIsNotNone(world.rank) self.assertEqual(world.is_last_move_raise, False) self.assertEqual(world.is_last_move_accepted_raise, False)
def test_set_initial_game_prize_player_B_ahead(self): world = WorldTotalWatten() self.assertEqual(world.current_game_prize, 2) world = WorldTotalWatten() world.player_B_score = 13 world.player_A_score = 4 world._set_initial_game_prize() self.assertEqual(world.current_game_prize, 4) world = WorldTotalWatten() world.player_B_score = 13 world.player_A_score = 10 world._set_initial_game_prize() self.assertEqual(world.current_game_prize, 3)
def test_get_last_played_card(self): world = WorldTotalWatten() world.played_cards = [4, 3, 2] last_played_card = world._get_last_played_card() self.assertEqual(last_played_card, 2) world = WorldTotalWatten() world.played_cards = [] last_played_card = world._get_last_played_card() self.assertEqual(last_played_card, None) world = WorldTotalWatten() world.played_cards = [123] last_played_card = world._get_last_played_card() self.assertEqual(last_played_card, 123) world = WorldTotalWatten() world.played_cards = [2, 8] last_played_card = world._get_last_played_card() self.assertEqual(last_played_card, 8)
def test_get_current_player_hand(self): world = WorldTotalWatten() world.player_A_hand = [1, 2, 3] world.player_B_hand = [4, 5, 6] world.current_player = 1 result = world._get_current_player_hand() self.assertEqual(result, [1, 2, 3]) world = WorldTotalWatten() world.player_A_hand = [1, 2, 3] world.player_B_hand = [4, 5, 6] world.current_player = -1 result = world._get_current_player_hand() self.assertEqual(result, [4, 5, 6])
def test_best_move_move_plays_card_2(self): world = WorldTotalWatten() world.current_player = -1 world.rank = 3 world.suit = 1 result, next_player = world.act(0, self.agent) self.assertEqual(result, "continue") self.assertEqual(next_player, 1) self.assertEqual(len(world.player_A_hand), 5) self.assertEqual(len(world.player_B_hand), 4)
def test_act_accept_raise(self): world = WorldTotalWatten() world.current_player = 1 world.is_last_move_accepted_raise = False world.is_last_move_raise = True result, next_player = world.act(3, self.agent) self.assertEqual(result, "continue") self.assertEqual(next_player, -1) self.assertEqual(world.is_last_move_raise, False) self.assertEqual(world.is_last_move_accepted_raise, True)
def test_best_move_declares_suit_1(self): world = WorldTotalWatten() world.current_player = 1 world.rank = 3 world.is_last_move_accepted_raise = True world.is_last_move_raise = False result, next_player = world.act(0, self.agent) self.assertEqual(result, "continue") self.assertEqual(next_player, -1) self.assertIsNotNone(world.suit) self.assertEqual(world.is_last_move_raise, False) self.assertEqual(world.is_last_move_accepted_raise, False)
def test_compare_cards_first_is_trumpfe(self): world = WorldTotalWatten() world.refresh() # only the first card is trümpfe world.rank = 4 world.suit = 2 card_id_one = total_watten.get_id(3, 2) card_id_two = total_watten.get_id(5, 1) self.assertTrue(world.compare_cards(card_id_one, card_id_two)) world.rank = 6 world.suit = 1 card_id_one = total_watten.get_id(4, 1) card_id_two = total_watten.get_id(7, 0) self.assertTrue(world.compare_cards(card_id_one, card_id_two)) # both cards are trümpfe world.rank = 4 world.suit = 2 card_id_one = total_watten.get_id(6, 2) card_id_two = total_watten.get_id(5, 2) self.assertTrue(world.compare_cards(card_id_one, card_id_two)) self.assertFalse(world.compare_cards(card_id_two, card_id_one)) world.rank = 7 world.suit = 1 card_id_one = total_watten.get_id(5, 1) card_id_two = total_watten.get_id(6, 1) self.assertFalse(world.compare_cards(card_id_one, card_id_two)) self.assertTrue(world.compare_cards(card_id_two, card_id_one)) world.rank = 8 world.suit = 3 card_id_one = total_watten.get_id(8, 3) card_id_two = total_watten.get_id(2, 1) self.assertTrue(world.compare_cards(card_id_one, card_id_two))
def test_clone(self): game = TotalWattenGame(self.agent, self.agent) world = WorldTotalWatten() world.init_world_to_state(1, -1, 0, 0, [25, 9, 1, 32, 14], [5, 13, 7, 10, 20], [], 0, 0, 2, False, False, None, 16, 28, None, None) game.trueboard = world clone_game = game.clone() obs_game = game.get_observation(game.get_cur_player()) obs_clone_game = clone_game.get_observation(clone_game.get_cur_player()) obs_str = game.get_observation_str(obs_game) clone_obs_str = clone_game.get_observation_str(obs_clone_game) self.assertEqual(obs_str, clone_obs_str)
def test_act_raise_points(self): world = WorldTotalWatten() world.current_player = -1 world.current_game_prize = 4 world.is_last_move_raise = False world.is_last_move_accepted_raise = False result, next_player = world.act(1, self.agent) self.assertEqual(result, "continue") self.assertEqual(next_player, 1) self.assertEqual(world.current_player, 1) self.assertEqual(world.current_game_prize, 5) self.assertEqual(world.is_last_move_raise, True) self.assertEqual(world.is_last_move_accepted_raise, False)
def test_remove_card_from_hand(self): world = WorldTotalWatten() world.player_A_hand = [0, 1, 2, 3, 4] world.player_B_hand = [5, 6, 7, 8, 9] world._remove_card_from_hand(3, 1) self.assertTrue(world.player_A_hand, [0, 1, 2, 4]) world._remove_card_from_hand(5, -1) self.assertTrue(world.player_A_hand, [6, 7, 8, 9]) world._remove_card_from_hand(4, 1) self.assertTrue(world.player_A_hand, [0, 1, 2]) world._remove_card_from_hand(8, -1) self.assertTrue(world.player_A_hand, [6, 7, 9])
def test_is_trumpf(self): world = WorldTotalWatten() world.rank = 6 world.suit = 2 self.assertTrue(world.is_trumpf(4, 2)) self.assertFalse(world.is_trumpf(5, 1)) self.assertFalse(world.is_trumpf(6, 2)) self.assertTrue(world.is_trumpf(0, 2)) self.assertTrue(world.is_trumpf(1, 2)) self.assertTrue(world.is_trumpf(3, 2)) world.rank = 8 world.suit = 3 self.assertTrue(world.is_trumpf(1, 3)) self.assertFalse(world.is_trumpf(5, 2))
def test_compare_cards_no_trumpfe(self): world = WorldTotalWatten() world.refresh() # different suit world.rank = 6 world.suit = 1 card_id_one = total_watten.get_id(5, 2) card_id_two = total_watten.get_id(3, 0) self.assertTrue(world.compare_cards(card_id_one, card_id_two)) self.assertTrue(world.compare_cards(card_id_two, card_id_one)) world.rank = 4 world.suit = 0 card_id_one = total_watten.get_id(6, 1) card_id_two = total_watten.get_id(7, 2) self.assertTrue(world.compare_cards(card_id_one, card_id_two)) self.assertTrue(world.compare_cards(card_id_two, card_id_one)) # same suit world.rank = 7 world.suit = 2 card_id_one = total_watten.get_id(5, 1) card_id_two = total_watten.get_id(3, 1) self.assertTrue(world.compare_cards(card_id_one, card_id_two)) self.assertFalse(world.compare_cards(card_id_two, card_id_one)) world.rank = 4 world.suit = 0 card_id_one = total_watten.get_id(1, 3) card_id_two = total_watten.get_id(5, 3) self.assertFalse(world.compare_cards(card_id_one, card_id_two)) self.assertTrue(world.compare_cards(card_id_two, card_id_one))
def test_is_rechte(self): world = WorldTotalWatten() world.rank = 6 world.suit = 2 self.assertTrue(world.is_rechte(6, 2)) self.assertFalse(world.is_rechte(6, 3)) self.assertFalse(world.is_rechte(3, 2)) self.assertFalse(world.is_rechte(1, 0)) world.rank = 4 world.suit = 2 self.assertFalse(world.is_rechte(8, -1)) self.assertFalse(world.is_rechte(4, 3)) self.assertFalse(world.is_rechte(2, 4)) self.assertFalse(world.is_rechte(5, 1)) self.assertTrue(world.is_rechte(4, 2))