def json_to_game(json_string: str) -> Round: """ Parses a Json string into a round object, checking for correctness :param json_string: Input string :return: Round object represented by the json string. """ is_game_json_valid(json_string) # Check for validity game_dict = json.loads(json_string) players_cards = [ PlayerCards(p['index'], p['order'], [Card.with_value(c) for c in p['hand']], p['dropped_card']) for p in game_dict['players'] ] deck = [Card.with_value(c) for c in game_dict['deck']] tricks = [] if game_dict['tricks'] is not None: for r in game_dict['tricks']: if r['cards'] is not None: tricks.append( Trick(r['start_index'], [Card.with_value(c) for c in r['cards']])) else: tricks.append(Trick(r['start_index'], [])) trump = Suite( game_dict['trump']) if game_dict['trump'] is not None else None return Round(players_cards, deck, tricks, game_dict['trump_caller'], trump)
def test_mc_sim(self): cards = CardDeck(0).deal_cards() players_cards = [PlayerCards(i, i, cards[i]) for i in range(4)] flipped_card = cards[4][0] mc_sim = MC_Simulation(players_cards[0], create_random_players(), flipped_card, 0, flipped_card.suite, None) mc_sim.simulate()
def possible_deal(known_cards: [[Card]], possible_cards: [[Card]], known_player: PlayerCards, random_gen) \ -> ([PlayerCards], [Card]): while len(list(chain(*known_cards))) < 24: # Randomly select a possible card list list_indexes = list(range(5)) random_gen.shuffle(list_indexes) list_index = next( (i for i in list_indexes if len(possible_cards[i]) > 0), None) chosen_card = random_gen.choice(possible_cards[list_index]) known_cards[list_index].append(chosen_card) if (len(known_cards[list_index]) == 5 and list_index < 4) or (len(known_cards[4]) == 4 and list_index == 4): possible_cards[list_index] = [] for possible_card_list in possible_cards: _conditional_remove(possible_card_list, chosen_card) # Select a possible card, remove from sets known_cards, possible_cards = update_known_cards( known_cards, possible_cards) player_cards = [] for i in range(4): p_ind = (i + known_player.index) % 4 p_order = (i + known_player.order) % 4 player_cards.append(PlayerCards(p_ind, p_order, known_cards[p_ind])) return player_cards, known_cards[4]
def test_min_max_sim(self): cards = CardDeck(0).deal_cards() players_cards = [PlayerCards(i, i, cards[i]) for i in range(4)] game_sim = GameTreeSimulation(players_cards, create_min_max_players(), cards[4], 0, Suite.DIAMOND) game_tree = game_sim.simulate() game_stats = GameTreeStatistics(game_tree) self.assertEqual(game_stats.tree_depth, 16) self.assertEqual(game_stats.num_nodes, 24367)
def deal_new_round(self) -> GameAction: """Deal hand to each player and create a round game state object. Initialize any player AI's""" cards = self.card_deck.deal_cards() players_cards = [PlayerCards(pi, (self.current_dealer + pi) % 4, cards[pi]) for pi in range(4)] players_cards.sort(key=lambda x: (3 - x.order) % 4, reverse=True) self.cur_round = Round(players_cards, cards[4]) for player in self.players: if type(player) is FullMonteCarloPlayer: player.calculate_game_tree(self.cur_round) return GameAction.DOES_DEALER_PICK_UP
def test_random_simulation_reproducible(self): cards = CardDeck(0).deal_cards() players_cards = [PlayerCards(i, i, cards[i]) for i in range(4)] mc_sim_1 = GameTreeSimulation(players_cards, create_random_players(), cards[4], 0, Suite.CLUB) mc_tree_1 = mc_sim_1.simulate() mc_pickle_1 = pickle.dumps(mc_tree_1) mc_sim_2 = GameTreeSimulation(players_cards, create_random_players(), cards[4], 0, Suite.CLUB) mc_tree_2 = mc_sim_2.simulate() mc_pickle_2 = pickle.dumps(mc_tree_2) self.assertEqual(mc_pickle_1, mc_pickle_2)