def json_to_player(cls, json_player):
        """
        Converts a JSON Player+ to a PlayerState
        :param json_player: a JSON Player+ as specified by the data definition at
                            http://www.ccs.neu.edu/home/matthias/4500-s16/8.html
        :return: a PlayerState object
        """
        gdict = globals()
        if len(json_player) == PLAYER_LENGTH:
            [[gdict[ID], player_id], [gdict[SPECIES], json_los],
             [gdict[BAG], food_bag]] = json_player
            cards = []
        else:
            [[gdict[ID], player_id], [gdict[SPECIES], json_los],
             [gdict[BAG], food_bag], [gdict[CARDS], cards]] = json_player

        player_species = [
            cls.json_to_species(json_species) for json_species in json_los
        ]
        player_hand = [cls.json_to_trait(trait_card) for trait_card in cards]
        player_obj = PlayerState(name=player_id,
                                 hand=player_hand,
                                 food_bag=food_bag,
                                 species=player_species)
        player_obj.validate_attributes()
        return player_obj
示例#2
0
    def json_to_player(cls, json_player):
        if len(json_player) == PLAYER_LENGTH:
            [[globals()[ID], player_id], [globals()[SPECIES], json_los],
             [globals()[BAG], food_bag]] = json_player
            cards = []
        elif len(json_player) == PLAYER_PLUS_LENGTH:
            [[globals()[ID], player_id], [globals()[SPECIES], json_los],
             [globals()[BAG], food_bag], [globals()[CARDS],
                                          cards]] = json_player
        else:
            raise AssertionError

        assert (all([
            player_id >= MIN_PLAYER_ID, food_bag >= MIN_FOOD_BAG,
            isinstance(player_id, int),
            isinstance(food_bag, int),
            isinstance(json_los, list),
            isinstance(cards, list)
        ]))

        player_species = [
            cls.json_to_species(json_species) for json_species in json_los
        ]
        if cards:
            cards = [cls.json_to_trait(trait_card) for trait_card in cards]
        player_obj = PlayerState(name=player_id,
                                 hand=cards,
                                 food_bag=food_bag,
                                 species=player_species)
        return player_obj
 def json_boards_to_player(cls, jboards):
     """
     Convert a JSON Boards to a PlayerState
     :param jboards: a JSON Boards as specified in
                     http://www.ccs.neu.edu/home/matthias/4500-s16/r_remote.html
     :return: a PlayerState object
     """
     species = [cls.json_to_species(jspecies) for jspecies in jboards]
     return PlayerState(species=species)
 def state_json_to_player(cls, rp_json):
     """
     Converts a remote protocol JSON player state to a PlayerState
     :param rp_json: a remote protocol JSON state as specified in
                     http://www.ccs.neu.edu/home/matthias/4500-s16/r_remote.html
     :return: a PlayerState object
     """
     [food_bag, json_species, json_hand] = rp_json
     assert (isinstance(food_bag, int) and food_bag >= MIN_FOOD_BAG)
     species = [cls.json_to_species(jspecies) for jspecies in json_species]
     hand = [cls.json_to_trait(jtrait) for jtrait in json_hand]
     return PlayerState(food_bag=food_bag, hand=hand, species=species)
 def json_to_choice_lop(cls, json_los_list):
     """
     Converts a List of List of JSON Species to a List of Player_States
     :param json_los_list: the list of JSON LOS as specified by the data definition at
                        http://www.ccs.neu.edu/home/matthias/4500-s16/12.html
     :return: a List of Player_State.
     """
     result = []
     for json_los in json_los_list:
         species_list = []
         for json_spec in json_los:
             species_list.append(cls.json_to_species(json_spec))
         result.append(PlayerState(species=species_list))
     return result
示例#6
0
    def setUp(self):
        self.jt_1 = CARNIVORE
        self.jt_2 = FATTISSUE
        self.jt_3 = BURROWING
        self.jt_4 = CLIMBING

        self.jSpecies_1 = [[FOOD, 2], [BODY, 2], [POPULATION, 2],
                           [TRAITS, [self.jt_1]]]
        self.jSpecies_2 = [[FOOD, 2], [BODY, 3], [POPULATION, 3],
                           [TRAITS, [self.jt_2]], [FATFOOD, 0]]
        self.jSpecies_3 = [[FOOD, 2], [BODY, 3], [POPULATION, 4],
                           [TRAITS, [self.jt_3, self.jt_4]]]
        self.jSpecies_4 = [[FOOD, 2], [BODY, 2], [POPULATION, 2],
                           [TRAITS, [self.jt_3, self.jt_4]]]

        self.jPlayer_1 = [[ID, 1], [SPECIES, [self.jSpecies_1]], [BAG, 2]]
        self.jPlayer_2 = [[ID, 2], [SPECIES, [self.jSpecies_2]], [BAG, 1]]
        self.jPlayer_3 = [[ID, 3],
                          [SPECIES, [self.jSpecies_3, self.jSpecies_4]],
                          [BAG, 3]]
        self.jPlayer_4 = [[ID, 2], [SPECIES, []], [BAG, 1]]

        self.json_feeding = [
            self.jPlayer_1, 10, [self.jPlayer_2, self.jPlayer_3]
        ]
        self.json_feed_1 = False
        self.json_feed_2 = self.jSpecies_3
        self.json_feed_3 = [self.jSpecies_2, 3]
        self.json_feed_4 = [self.jSpecies_1, self.jPlayer_2, self.jSpecies_2]

        self.t_1 = TraitCard(CARNIVORE)
        self.t_2 = TraitCard(FATTISSUE)
        self.t_3 = TraitCard(BURROWING)
        self.t_4 = TraitCard(CLIMBING)

        self.species_1 = Species(2, 2, 2, [self.t_1])
        self.species_2 = Species(3, 2, 3, [self.t_2], 0)
        self.species_3 = Species(4, 2, 3, [self.t_3, self.t_4])
        self.species_4 = Species(2, 2, 2, [self.t_3, self.t_4])

        self.player_1 = PlayerState(name=1,
                                    food_bag=2,
                                    species=[self.species_1])
        self.player_2 = PlayerState(name=2,
                                    food_bag=1,
                                    species=[self.species_2])
        self.player_3 = PlayerState(name=3,
                                    food_bag=3,
                                    species=[self.species_3, self.species_4])
        self.player_4 = PlayerState(name=2, food_bag=1, species=[])

        self.feeding = [self.player_1, 10, [self.player_2, self.player_3]]
        self.feed_1 = False
        self.feed_2 = self.species_3
        self.feed_3 = [self.species_2, 3]
        self.feed_4 = [self.species_1, self.player_2, self.species_2]

        self.jSpecies_card1 = [2, WARNINGCALL]
        self.jSpecies_card2 = [5, CARNIVORE]
        self.jSpecies_card3 = [-3, BURROWING]
        self.jSpecies_card4 = [3, AMBUSH]
        self.jSpecies_card5 = [0, SCAVENGER]

        self.species_card1 = TraitCard(WARNINGCALL, 2)
        self.species_card2 = TraitCard(CARNIVORE, 5)
        self.species_card3 = TraitCard(BURROWING, -3)
        self.species_card4 = TraitCard(AMBUSH, 3)
        self.species_card5 = TraitCard(SCAVENGER, 0)

        self.jList_of_players = [
            self.jPlayer_1, self.jPlayer_4, self.jPlayer_3
        ]
        self.jDeck = [
            self.jSpecies_card1, self.jSpecies_card2, self.jSpecies_card3,
            self.jSpecies_card4, self.jSpecies_card5
        ]
        self.jDealer = [self.jList_of_players, 12, self.jDeck]

        self.list_of_players = [self.player_1, self.player_4, self.player_3]
        self.deck = [
            self.species_card1, self.species_card2, self.species_card3,
            self.species_card4, self.species_card5
        ]

        self.dealer1 = Dealer(self.list_of_players, 12, self.deck)