def convert_g(g, g_key, class_name): """ Converts a gb or gp to a CardPlay :param g:gb or gp :type gp: PYJSON :param g_key: string representing key in PYJSON :type g_key: String :param class_name: constructor of cardplay :type class_name: Nat Nat -> CardPlay :return: the corresponding CardPlay :rtype: CardPlay """ [key, species_index, card_index] = g if key != g_key: raise ConvertPyJSONError("Wrong key") if not (is_natural(species_index) and is_natural(card_index)): raise ConvertPyJSONError("expected a natural number") return class_name(card_index, species_index)
def convert_from_pj_fat_choice(py_json): """ Convert the given py_json into a PlayerFeedVegetarian :param py_json: the PyJSON to convert :type py_json: PyJSON :return: the equivalent PlayerFeedVegetarian :rtype: PlayerFeedVegetarian """ [species_index, fat_to_store] = py_json if not is_natural_plus(fat_to_store): raise ConvertPyJSONError("Error Converting Fat Tissue Choice") return PlayerStoreFat(species_index, fat_to_store)
def convert_rt(rt): """ Converts a rt to a CardPlay :param rt: An RT represents a trait replacement for a species board. Specifically, [b, i, j] specifies that board b’s i’s trait card is replaced with the j’s card from the player’s card sequence. :type rt: PYJSON :return: CardPlay """ if not is_list_of_nat(rt) and len(rt) == 3: raise ConvertPyJSONError('Error decoding RT') [species_index, replaced_card_index , new_card_index] = rt return ReplaceCards(new_card_index, species_index, replaced_card_index)
def convert_bt(bt): """ Converts a bt to a CardPlay :param bt: A BT represents a species board addition to the right of the existing sequence of boards for the corresponding player. Specifically, [i, j, ..., k] uses the first of the player’s cards (i) to "pay" for the new board and uses the remaining (up to three) cards (j, ..., k) as traits. :type bt: PYJSON :return: CardPlay """ if not is_list_of_nat(bt) and 1 <= len(bt) <= 4: raise ConvertPyJSONError('Expected a list of natural numbers') exchanged_card = bt[0] return ExchangeForSpecies(exchanged_card, bt[1:])
def convert_from_pj_feeding_choice(py_json): """ Converts the given PyJSON into a PlayerFeedingChoice :param py_json: the PyJSON to convert :type py_json: PyJSON :return: the equivalent PlayerFeedingChoice :rtype: PlayerFeedingChoice """ if py_json is False: return PlayerForgoAttack() elif is_natural(py_json): return convert_from_pj_veg_choice(py_json) elif is_list_of_nat(py_json): if len(py_json) == 2: return convert_from_pj_fat_choice(py_json) elif len(py_json) == 3: return convert_from_pj_carn_choice(py_json) raise ConvertPyJSONError("Error converting player feeding choice")
def convert_from_pj_state(pj_state): """ Converts the given PyJSON State to the player state, watering hole and other player states :param pj_state: The PyJSON State :type pj_state: PyJSON State :return: (watering_hole, other_players, own_player) :rtype: (Nat, [PlayerState, ...], PlayerState) """ if not (isinstance(pj_state, list) and len(pj_state) == 5): raise ConvertPyJSONError("Cannot convert Action4") [pj_food, pj_species, pj_hand, wh, other_lob] = pj_state species_list = convert_from_pj_los(pj_species) hand = convert_from_pj_loc(pj_hand) own_player_state = PlayerState(1, species_list, pj_food, hand) other_player_states = convert_lob_to_lop(other_lob) return wh, other_player_states, own_player_state
def convert_new_action4(action4): """ Converts an action4 pyjson in to a card index and a list of cardplays :param action4: Every natural number in an Action4 represent an index into a sequence of species boards or cards. The first natural number specifies the card that a player is turning into food. :type action4: PYJSON :return: [idx, locp] where idx is the index of the food card and locp is the list of CardPlays :rtype: [Nat, [CardPlay, ...]] """ if not(isinstance(action4, list) and len(action4) == 5): raise ConvertPyJSONError("Cannot convert Action4") [card_index, logp, logb, lobt, lort] = action4 exchange_for_species = [convert_bt(bt) for bt in lobt] exchange_pop = [new_convert_gp(gp) for gp in logp] exchange_body = [new_convert_gb(gb) for gb in logb] replace_cards = [convert_rt(rt) for rt in lort] return [card_index, exchange_for_species + exchange_pop + exchange_body + replace_cards]