def play_episode(encoder: CardPhaseStateActionEncoder, agent: AllPhaseAgent = None): if agent is None: agent = AllPhaseAgent() environment = FrenchTarotEnvironment() observation = environment.reset() done = False reagent_entries_per_player = [] actions_in_round = [] observations_in_round = [] while not done: action = agent.get_action(observation) new_observation, rewards, done, _ = environment.step(action) if isinstance(observation, CardPhaseObservation): actions_in_round.append(action) observations_in_round.append(observation) if rewards is not None: iterator = zip(observations_in_round, actions_in_round, rewards) for observation, action, reward in iterator: entry = encoder.encode(observation.player.position_towards_taker, observation, action, reward) reagent_entries_per_player.append(entry) actions_in_round = [] observations_in_round = [] observation = new_observation return reagent_entries_per_player
def test_deal_petit_sec(): card_list = copy.copy(CARDS) card_list[0] = Card.TRUMP_1 card_list[-21] = Card.SPADES_1 environment = FrenchTarotEnvironment() with pytest.raises(FrenchTarotException): environment.reset(card_list)
def setup_environment_for_card_phase(taker=0, shuffled_deck=None, chelem=False, poignee=False): environment = FrenchTarotEnvironment() observation = environment.reset(shuffled_card_deck=shuffled_deck) good = False for i in range(environment.n_players): if i == taker: observation = environment.step(Bid.GARDE_SANS)[0] good = True else: observation = environment.step(Bid.PASS)[0] if not good: raise FrenchTarotException("No taking player") announcements = [] if chelem: announcements.append(ChelemAnnouncement()) if poignee: announcements.append( PoigneeAnnouncement.largest_possible_poignee_factory( observation.player.hand[-11:-1])) environment.step(announcements) environment.step([]) environment.step([]) observation = environment.step([])[0] return environment, observation
def setup_environment() -> FrenchTarotEnvironment: environment = FrenchTarotEnvironment() environment.reset() environment.step(Bid.PASS) environment.step(Bid.PASS) environment.step(Bid.PASS) environment.step(Bid.GARDE_SANS) return environment
def setup_environment(): environment = FrenchTarotEnvironment() environment.reset() environment.step(Bid.PASS) environment.step(Bid.PASS) environment.step(Bid.PASS) environment.step(Bid.PETITE) return environment
def test_make_dog_without_trump(): environment = FrenchTarotEnvironment() environment.reset(CARDS) environment.step(Bid.PETITE) environment.step(Bid.PASS) environment.step(Bid.PASS) observation = environment.step(Bid.PASS)[0] dog = list(observation.player.hand[:6]) # taking player is always player 0 observation, reward, done, _ = environment.step(dog) assert np.isclose(reward, 3.0)
def prepare_environment_sorted_deck(): environment = FrenchTarotEnvironment() environment.reset(CARDS) environment.step(Bid.PASS) environment.step(Bid.PASS) environment.step(Bid.PASS) observation = environment.step(Bid.PETITE)[0] return environment, observation
def test_announce_simple_poignee_valid(): environment = FrenchTarotEnvironment() environment.reset(CARDS) environment.step(Bid.PASS) environment.step(Bid.PASS) environment.step(Bid.PASS) observation = environment.step(Bid.GARDE_SANS)[0] poignee = PoigneeAnnouncement.largest_possible_poignee_factory( observation.player.hand) observation, reward, done, _ = environment.step([poignee]) assert reward == 0 assert not done
def test_make_dog(): environment = FrenchTarotEnvironment() environment.reset() environment.step(Bid.PETITE) environment.step(Bid.PASS) environment.step(Bid.PASS) observation = environment.step(Bid.PASS)[0] taker_hand = observation.player.hand dog = taker_hand[2:8] observation, reward, done, _ = environment.step(dog) assert not done assert reward > 0 assert isinstance(observation, AnnouncementsPhaseObservation) assert np.all([card not in observation.player.hand for card in dog])
def test_announce_chelem_player0(): environment = FrenchTarotEnvironment() environment.reset(CARDS) environment.step(Bid.GARDE_SANS) environment.step(Bid.PASS) environment.step(Bid.PASS) environment.step(Bid.PASS) announcements = [ChelemAnnouncement()] observation, reward, done, _ = environment.step(announcements) assert reward == 0 assert not done
def test_announce_simple_poignee_no_such_cards_in_hand(): environment = FrenchTarotEnvironment() environment.reset(CARDS) environment.step(Bid.PASS) environment.step(Bid.PASS) environment.step(Bid.PASS) environment.step(Bid.GARDE_SANS) environment.step([]) environment.step([]) environment.step([]) with pytest.raises(FrenchTarotException): card_list = [ Card.TRUMP_1, Card.TRUMP_2, Card.TRUMP_3, Card.TRUMP_4, Card.TRUMP_5, Card.TRUMP_6, Card.TRUMP_7, Card.TRUMP_8, Card.TRUMP_9, Card.TRUMP_21 ] poignee = PoigneeAnnouncement.largest_possible_poignee_factory( card_list) environment.step([poignee])
def test_announce_simple_poignee_no_trump(): environment = FrenchTarotEnvironment() environment.reset(CARDS) environment.step(Bid.GARDE_SANS) environment.step(Bid.PASS) environment.step(Bid.PASS) environment.step(Bid.PASS) environment.step([]) environment.step([]) environment.step([]) with pytest.raises(FrenchTarotException): environment.step([SimplePoigneeAnnouncement(CARDS[:11])])
def test_announce_simple_poignee_excuse_accepted(): environment = FrenchTarotEnvironment() deck = copy.copy(CARDS) deck[69] = Card.EXCUSE deck[70] = Card.SPADES_1 deck[0] = Card.TRUMP_15 deck[77] = Card.TRUMP_14 environment.reset(deck) environment.step(Bid.PASS) environment.step(Bid.PASS) environment.step(Bid.PASS) environment.step(Bid.GARDE_SANS) card_list = [ Card.TRUMP_1, Card.TRUMP_2, Card.TRUMP_3, Card.TRUMP_4, Card.TRUMP_5, Card.TRUMP_6, Card.TRUMP_7, Card.TRUMP_8, Card.TRUMP_9, Card.TRUMP_10, Card.TRUMP_11, Card.TRUMP_12, Card.TRUMP_13, Card.TRUMP_16, Card.EXCUSE ] environment.step( [PoigneeAnnouncement.largest_possible_poignee_factory(card_list)])
def test_announce_simple_poignee_excuse_refused(): environment = FrenchTarotEnvironment() deck = copy.copy(CARDS) deck[77] = Card.TRUMP_16 deck[71] = Card.EXCUSE environment.reset(CARDS) environment.step(Bid.PASS) environment.step(Bid.PASS) environment.step(Bid.PASS) environment.step(Bid.GARDE_SANS) environment.step([]) environment.step([]) observation = environment.step([])[0] with pytest.raises(FrenchTarotException): environment.step([observation.player.hand])
def test_announce_chelem_wrong_string(): environment = FrenchTarotEnvironment() environment.reset(CARDS) environment.step(Bid.GARDE_SANS) environment.step(Bid.PASS) environment.step(Bid.PASS) environment.step(Bid.PASS) with pytest.raises(FrenchTarotException): environment.step(["test"])
def test_dog_has_wrong_number_of_cards(): environment = FrenchTarotEnvironment() environment.reset(CARDS) environment.step(Bid.PASS) environment.step(Bid.PETITE) environment.step(Bid.PASS) observation = environment.step(Bid.PASS)[0] dog = list(observation.player.hand[:5]) # taking player is always player 0 with pytest.raises(FrenchTarotException): environment.step(dog)
def environment(): return FrenchTarotEnvironment()
def test_randomness_when_bidding(): random_agent = RandomAgent() environment = FrenchTarotEnvironment() observation = environment.reset() actions = [random_agent.get_action(observation) for _ in range(10)] assert len(np.unique(actions)) > 1
def test_instantiate_random_player(): random_agent = RandomAgent() environment = FrenchTarotEnvironment() observation = environment.reset() action = random_agent.get_action(observation) assert isinstance(action, Bid)
def environment(): yield FrenchTarotEnvironment()