def test_next_feeding(self): self.species_4.traits = [TraitCard(CARNIVORE)] self.species_5.traits, self.species_5.fat_storage = ([ TraitCard(FATTISSUE) ], 0) # Test if fat_tissue_species self.assertEqual( Player.next_feeding(self.player_1, 10, [self.player_2, self.player_3]), FatFeeding(1, 3)) # Test if hungry_herbivores self.species_5.traits = [] self.assertEqual( Player.next_feeding(self.player_1, 10, [self.player_2]), HerbivoreFeeding(2)) # Test if hungry_carnivore self.species_5.traits = [TraitCard(CARNIVORE)] self.species_6.traits = [TraitCard(CARNIVORE)] self.assertEqual( Player.next_feeding(self.player_1, 10, [self.player_2, self.player_3]), CarnivoreFeeding(0, 0, 0)) # Test no attackable species self.assertEqual(Player.next_feeding(self.player_1, 10, []), NoFeeding())
def test_warning_call(self): self.left_neighbor.traits = [TraitCard(WARNINGCALL)] self.right_neighbor.traits = [TraitCard(WARNINGCALL)] self.assertFalse(self.defender.is_attackable(self.attacker, left_neighbor=self.left_neighbor)) self.assertFalse(self.defender.is_attackable(self.attacker, right_neighbor=self.right_neighbor)) self.attacker.traits.append(TraitCard(AMBUSH)) self.assertTrue(self.defender.is_attackable(self.attacker, left_neighbor=self.left_neighbor))
def validate_cards(self, total_deck): """ Validates this species by checking that each of its TraitCards is unique and possible :param total_deck: a list of TraitCards representing all valid card possibilities :raise ValueError if invalid cards exist on any species """ TraitCard.validate_all_unique(self.traits, total_deck)
def validate_cards(self): """ Validates that all cards known by this dealer are valid possibilities and unique :raise: ValueError if duplicate or invalid cards exist """ total_deck = Dealer.make_deck() TraitCard.validate_all_unique(self.deck, total_deck) PlayerState.validate_all_cards(self.list_of_players, total_deck)
def validate_cards(self, total_deck): """ Validates that the TraitCards in this PlayerState's hand and on its Species boards are all possible and unique :param total_deck: a list of TraitCards representing all valid card possibilities :raise ValueError if duplicate or invalid cards exist on this player """ TraitCard.validate_all_unique(self.hand, total_deck) Species.validate_all_cards(self.species, total_deck)
def test_largest_fatty_need(self): self.species_1.traits = [TraitCard("fat-tissue")] self.species_2.traits = [TraitCard("fat-tissue")] self.species_4.traits = [TraitCard("fat-tissue")] self.assertEqual( Species.largest_fatty_need([self.species_1, self.species_4]), self.species_4) self.assertEqual( Species.largest_fatty_need([self.species_1, self.species_2]), self.species_1)
def test_compare_cards(self): card0 = TraitCard("climbing", 0) card1 = TraitCard("burrowing", 3) card2 = TraitCard("climbing", -1) cards = [card0, card1, card2] cards.sort(TraitCard.compare) self.assertEqual(cards, [card1, card2, card0]) self.assertEqual(TraitCard.compare(card0, card1), 1) self.assertEqual(TraitCard.compare(card1, card2), -1) self.assertEqual(TraitCard.compare(card0, card2), 1)
def validate_attributes(self): """ Validates the attributes of this PlayerState :raise AssertionError if any attributes are out of bounds """ assert(isinstance(self.name, int) and self.name >= MIN_PLAYER_ID) assert(isinstance(self.food_bag, int) and self.food_bag >= MIN_FOOD_BAG) assert(isinstance(self.hand, list)) TraitCard.validate_all_attributes(self.hand) assert(isinstance(self.species, list)) Species.validate_all_attributes(self.species)
def validate_attributes(self): """ Validates the attributes of this Dealer :raise AssertionError if any attributes are out of bounds """ assert (isinstance(self.list_of_players, list) and LOP_MAX >= len(self.list_of_players) >= LOP_MIN) PlayerState.validate_all_attributes(self.list_of_players) assert (isinstance(self.watering_hole, int) and self.watering_hole >= MIN_WATERING_HOLE) assert (isinstance(self.deck, list) and LOC_MAX >= len(self.deck)) TraitCard.validate_all_attributes(self.deck)
def test_warning_call(self): self.left_neighbor.traits = [TraitCard("warning-call")] self.right_neighbor.traits = [TraitCard("warning-call")] self.assertFalse( self.defender.is_attackable(self.attacker, left_neighbor=self.left_neighbor)) self.assertFalse( self.defender.is_attackable(self.attacker, right_neighbor=self.right_neighbor)) self.attacker.traits.append(TraitCard("ambush")) self.assertTrue( self.defender.is_attackable(self.attacker, left_neighbor=self.left_neighbor))
def test_feed_fatty(self): self.species_4.traits = [TraitCard("fat-tissue")] self.species_1.traits = [TraitCard("fat-tissue")] self.species_5.traits = [TraitCard("fat-tissue")] self.assertEqual( Player.feed_fatty([self.species_4, self.species_1, self.species_5], 10), [self.species_5, 3]) self.assertEqual( Player.feed_fatty([self.species_4, self.species_1, self.species_5], 1), [self.species_5, 1]) self.assertEqual( Player.feed_fatty([self.species_4, self.species_1], 10), [self.species_4, 3])
def validate_attributes(self): """ Validates the attributes of this Species :raise AssertionError if any attributes are out of bounds """ assert(isinstance(self.population, int) and MAX_POP >= self.population >= MIN_POP) assert(isinstance(self.food, int) and MAX_FOOD >= self.food >= MIN_FOOD) assert(isinstance(self.body, int) and MAX_BODY >= self.body >= MIN_BODY) assert(all([isinstance(self.traits, list), MAX_TRAITS >= len(self.traits), len(self.trait_names()) == len(set(self.trait_names()))])) TraitCard.validate_all_attributes(self.traits) if self.fat_storage is not False: assert(isinstance(self.body, int) and self.body >= self.fat_storage >= MIN_FATFOOD)
def choose(self, choice): """ Returns the players Action representing how this player wants to use their cards for the current round. :param choice: A Choice object representing the state of the species before and after the current player. :return: An Action representing how the player is using their cards for the round. """ cards = [] for i, card in enumerate(self.player_state.hand): cards.append({"card": card, "index": i}) cards.sort(lambda c0, c1: TraitCard.compare(c0['card'], c1['card'])) food_card = cards[0]['index'] action = Action(food_card, [], [], [], []) action.species_additions.append(BoardAddition(cards[1]['index'], [cards[2]['index']])) if len(self.player_state.hand) > 3: action.pop_grows.append(PopGrow(len(self.player_state.species), cards[3]['index'])) if len(self.player_state.hand) > 4: action.body_grows.append(BodyGrow(len(self.player_state.species), cards[4]['index'])) if len(self.player_state.hand) > 5: action.trait_replacements.append(ReplaceTrait(len(self.player_state.species), 0, cards[5]['index'])) return action
def test_symbiosis(self): self.defender.traits = [TraitCard(SYMBIOSIS)] self.defender.body = 3 self.right_neighbor.body = 5 self.assertFalse(self.defender.is_attackable(self.attacker, right_neighbor=self.right_neighbor)) self.right_neighbor.body = 2 self.assertTrue(self.defender.is_attackable(self.attacker, right_neighbor=self.right_neighbor))
def show_changes(self, other_player): """ Creates a string representation of the changed attributes between a PlayerState before and after an imperative function is called on it. :param other_player: The PlayerState after it has been modified :return: String of attribute changes, or "" if unchanged. """ changes = [] if self.name != other_player.name: changes.append(CHANGE_TEMPLATE % ("name", str(self.name), str(other_player.name))) if self.food_bag != other_player.food_bag: changes.append( CHANGE_TEMPLATE % ("food_bag", str(self.food_bag), str(other_player.food_bag))) hand_changes = TraitCard.show_all_changes(self.hand, other_player.hand) if hand_changes: changes.append(hand_changes) species_changes = Species.show_all_changes(self.species, other_player.species) if species_changes: changes.append(species_changes) if self.active != other_player.active: changes.append(CHANGE_TEMPLATE % ("active", self.active, other_player.active)) return ", ".join(changes)
def setUp(self): self.carnivore = TraitCard(CARNIVORE, 3) self.burrowing = TraitCard(BURROWING, 2) self.fattissue = TraitCard(FATTISSUE, 4) self.foraging = TraitCard(FORAGING, 2) self.horns = TraitCard(HORNS, 6) self.cooperation = TraitCard(COOPERATION, 1) self.scavenger = TraitCard(SCAVENGER, 2) self.species_1 = Species(4, 4, 4, []) self.species_2 = Species(4, 4, 4) self.species_3 = Species(4, 4, 3) self.species_4 = Species(4, 3, 3) self.species_5 = Species(3, 1, 3) self.species_6 = Species(4, 3, 3) self.species_7 = Species(4, 4, 4) self.species_list = [ self.species_2, self.species_4, self.species_3, self.species_5, self.species_1 ] self.player_1 = PlayerState( species=[self.species_4, self.species_5, self.species_6], hand=[self.carnivore]) self.player_2 = PlayerState(species=[self.species_1], hand=[self.carnivore, self.fattissue]) self.player_3 = PlayerState( species=[self.species_2, self.species_3, self.species_7], hand=[self.foraging])
def setUp(self): # Traits (Trait, Food-value) self.carnivore = TraitCard(CARNIVORE, 3) self.burrowing = TraitCard(BURROWING, 2) self.fattissue = TraitCard(FATTISSUE, 4) self.foraging = TraitCard(FORAGING, 2) self.horns = TraitCard(HORNS, 6) self.cooperation = TraitCard(COOPERATION, 1) self.scavenger = TraitCard(SCAVENGER, 2) # Species (Population, Food, Body, Traits, Fat-Storage) self.species1 = Species(1, 0, 2, [self.cooperation]) self.species2 = Species(6, 2, 1, [self.carnivore]) self.species3 = Species(3, 3, 3, [self.fattissue], 0) self.species4 = Species(5, 5, 5, [self.burrowing]) self.species5 = Species(5, 3, 4, [self.foraging]) self.species6 = Species( 2, 1, 7, [self.carnivore, self.fattissue, self.scavenger], 0) self.species7 = Species(7, 1, 6, [self.horns]) self.player1_species = [self.species1, self.species2] self.player2_species = [self.species3, self.species4, self.species5] self.player3_species = [self.species6, self.species7] # Players (Name, Bag, Hand, Species) self.player1 = PlayerState(1, 0, [self.fattissue], self.player1_species) self.player2 = PlayerState(2, 3, [self.fattissue, self.carnivore], self.player2_species) self.player3 = PlayerState(3, 6, [self.burrowing], self.player3_species) self.public_player1 = PlayerState(1, False, False, self.player1_species) self.public_player2 = PlayerState(2, False, False, self.player2_species) self.public_player3 = PlayerState(3, False, False, self.player3_species) self.list_of_players = [self.player1, self.player2, self.player3] # Dealer (List of Players, Watering Hole, Deck) self.dealer1 = Dealer(self.list_of_players, 10, []) # Actions self.food_card_action1 = FoodCardAction(0) self.food_card_action2 = FoodCardAction(1) self.grow_action_pop = GrowAction(POPULATION, 0, 0) self.grow_action_body = GrowAction(BODY, 1, 1) self.add_species_action1 = AddSpeciesAction(0, [1]) self.add_species_action2 = AddSpeciesAction(0, []) self.replace_trait_action1 = ReplaceTraitAction(0, 0, 0) self.replace_trait_action2 = ReplaceTraitAction(2, 0, 1) self.replace_trait_action3 = ReplaceTraitAction(0, 2, 0) # Action4 self.action4_1 = Action4(self.player1, [self.food_card_action1]) self.action4_2 = Action4( self.player2, [self.food_card_action2, self.grow_action_pop])
def create_deck(self): """ Creates a deck of TraitCards. """ for trait in TraitCard.traits: num_cards = 7 if trait == "carnivore": num_cards = 17 self.deck.extend(TraitCard.gen_cards(num_cards, trait)) self.deck.sort(TraitCard.compare)
def setUp(self): # Traits (Trait, Food-value) self.carnivore = TraitCard(CARNIVORE, 3) self.burrowing = TraitCard(BURROWING, 2) self.fattissue = TraitCard(FATTISSUE, 2) self.foraging = TraitCard(FORAGING, 2) self.horns = TraitCard(HORNS, 0) self.cooperation = TraitCard(COOPERATION, 1) self.scavenger = TraitCard(SCAVENGER, 2) # Species (Population, Food, Body, Traits, Fat-Storage) self.species1 = Species(1, 0, 2, [self.cooperation]) self.species2 = Species(6, 2, 1, [self.carnivore]) self.species3 = Species(3, 3, 3, [self.fattissue], 0) self.species4 = Species(5, 5, 5, [self.burrowing]) self.species5 = Species(5, 3, 4, [self.foraging]) self.species6 = Species( 2, 1, 7, [self.carnivore, self.fattissue, self.scavenger], 0) self.species7 = Species(7, 1, 6, [self.horns]) self.player1_species = [self.species1, self.species2] self.player2_species = [self.species3, self.species4, self.species5] self.player3_species = [self.species6, self.species7] # Players (Name, Bag, Hand, Species) self.player1 = PlayerState(1, 0, [self.horns, self.foraging], self.player1_species, ext_player=Player()) self.player2 = PlayerState(2, 3, [self.carnivore, self.fattissue], self.player2_species, ext_player=Player()) self.player3 = PlayerState(3, 6, [self.burrowing], self.player3_species, ext_player=Player()) self.public_player1 = PlayerState(1, False, False, self.player1_species) self.public_player2 = PlayerState(2, False, False, self.player2_species) self.public_player3 = PlayerState(3, False, False, self.player3_species) self.list_of_players = [self.player1, self.player2, self.player3] # Dealer (List of Players, Watering Hole, Deck) self.dealer1 = Dealer(self.list_of_players, 10, []) # Action self.action4_1 = Action4(FoodCardAction(1), [GrowAction(POPULATION, 0, 0)], [], [], []) self.action4_2 = Action4(FoodCardAction(0), [], [], [], [ReplaceTraitAction(1, 0, 1)]) self.action4_3 = Action4(FoodCardAction(0), [], [], [], []) self.action4_list = [self.action4_1, self.action4_2, self.action4_3]
def test_largest_fatty_need(self): self.species_1.traits = self.species_2.traits = self.species_4.traits = [ TraitCard(FATTISSUE) ] self.species_1.fat_storage = self.species_2.fat_storage = self.species_4.fat_storage = 0 self.assertEqual( Player.largest_fatty_need([self.species_1, self.species_4]), self.species_1) self.assertEqual( Player.largest_fatty_need([self.species_1, self.species_2]), self.species_1)
def make_deck(cls): """ Makes a full, shuffled deck of TraitCards :return: list of TraitCards """ deck = [] for trait in TRAITS_LIST: food_range = (CARN_FOOD_MAX if trait == CARNIVORE else HERB_FOOD_MAX) for food_val in range(-food_range, food_range): deck.append(TraitCard(trait, food_val)) random.shuffle(deck) return deck
def setUp(self): self.trait1 = TraitCard(CARNIVORE, 2) self.trait2 = TraitCard(SCAVENGER, 3) self.trait3 = TraitCard(FATTISSUE, 2) self.trait4 = TraitCard(FORAGING, 3) self.species_1 = Species(4, 4, 4, [self.trait3], 3) self.species_2 = Species(4, 4, 4) self.species_3 = Species(4, 4, 3) self.species_4 = Species(4, 3, 3) self.species_5 = Species(3, 1, 3) self.species_6 = Species(4, 3, 3) self.species_7 = Species(4, 4, 4) self.player_1 = PlayerState( 1, 2, [self.trait1, self.trait2], [self.species_4, self.species_5, self.species_6]) self.player_2 = PlayerState(2, 10, [], [self.species_1]) self.player_3 = PlayerState( 3, 4, [self.trait4], [self.species_2, self.species_3, self.species_7]) self.dealer_1 = Dealer([self.player_1, self.player_2, self.player_3], 10, [self.trait1, self.trait2])
def setUp(self): self.attacker = Species() self.attacker.traits = [TraitCard(CARNIVORE)] self.defender = Species() self.left_neighbor = Species() self.right_neighbor = Species() self.species_1 = Species(4, 4, 4) self.species_2 = Species(4, 4, 4) self.species_3 = Species(4, 4, 3) self.species_4 = Species(4, 3, 3) self.species_5 = Species(3, 3, 3) self.species_list = [self.species_2, self.species_4, self.species_3, self.species_5, self.species_1]
def make_deck(cls): """ Makes a full, sorted deck of TraitCards :return: list of TraitCards """ deck = [] for trait in TRAITS_LIST: food_range = (CARN_FOOD_MAX if trait == CARNIVORE else HERB_FOOD_MAX) for food_val in range(-food_range, food_range + 1): deck.append(TraitCard(trait, food_val)) deck = sorted(deck, key=lambda card: (card.trait, card.food_points)) return deck
def test_next_feeding(self): self.species_4.traits = [TraitCard("carnivore")] self.species_5.traits = [TraitCard("fat-tissue")] # Test if fat_tissue_species self.assertEqual( Player.next_feeding(self.player_1, 10, [self.player_2, self.player_3]), [self.species_5, 3]) # Test if hungry_herbivores self.species_5.traits = [] self.assertEqual( Player.next_feeding(self.player_1, 10, [self.player_2]), self.species_6) # Test if hungry_carnivore self.species_5.traits = [TraitCard("carnivore")] self.species_6.traits = [TraitCard("carnivore")] self.assertEqual( Player.next_feeding(self.player_1, 10, [self.player_2, self.player_3]), [self.species_4, self.player_2, self.species_1]) # Test exception with self.assertRaises(Exception): Player.next_feeding(self.player_2, 10, [self.player_1])
def choose(self, choice): cards = [] for i, card in enumerate(self.player_state.hand): cards.append({"card": card, "index": i}) cards.sort(lambda c0, c1: TraitCard.compare(c0['card'], c1['card'])) food_card = cards[0]['index'] action = Action(food_card, [], [], [], []) action.species_additions.append(BoardAddition(cards[1]['index'], [cards[2]['index']])) if len(self.player_state.hand) > 3: action.pop_grows.append(PopGrow(len(self.player_state.species), cards[3]['index'])) if len(self.player_state.hand) > 4: action.body_grows.append(BodyGrow(len(self.player_state.species), cards[4]['index'])) if len(self.player_state.hand) > 5: action.trait_replacements.append(ReplaceTrait(len(self.player_state.species), 0, cards[5]['index'])) return action
def show_changes(self, species2): """ Shows a string representation of the differences between this Species and the given Species. :param species2: The Species we are comparing to this Species :return: String representing the differences """ changes = [] if self.population != species2.population: changes.append(CHANGE_TEMPLATE % (POPULATION, self.population, species2.population)) if self.food != species2.food: changes.append(CHANGE_TEMPLATE % (FOOD, self.food, species2.food)) if self.body != species2.body: changes.append(CHANGE_TEMPLATE % (BODY, self.body, species2.body)) trait_changes = TraitCard.show_all_changes(self.traits, species2.traits) if trait_changes: changes.append('[traits: ' + trait_changes + ']') if self.fat_storage != species2.fat_storage: changes.append(CHANGE_TEMPLATE % (FATTISSUE, self.fat_storage, species2.fat_storage)) return '[' + ", ".join(changes) + ']' if changes else ''
def show_changes(self, dealer2): """ Shows a string representation of the differences between this Dealer and the given Dealer. :param dealer2: The Dealer we are comparing to this Dealer :return: String """ changes = [] old_players = self.players_to_dict() new_players = dealer2.players_to_dict() for i in range(0, len(self.list_of_players)): name = self.list_of_players[i].name old_player = old_players.get(name) new_player = new_players.get(name) if not old_player.equal_attributes(new_player): changes.append('Player ' + str(name) + ': ' + old_player.show_changes(new_player)) if self.watering_hole != dealer2.watering_hole: changes.append( CHANGE_TEMPLATE % ('watering_hole', self.watering_hole, dealer2.watering_hole)) deck_changes = TraitCard.show_all_changes(self.deck, dealer2.deck) if deck_changes: changes.append('deck: ' + deck_changes) return ", ".join(changes)
def setUp(self): self.carnivore_trait = TraitCard(CARNIVORE, 3) self.burrowing_trait = TraitCard(BURROWING, 2) self.fattissue_trait = TraitCard(FATTISSUE, 4) self.foraging_trait = TraitCard(FORAGING, 2) self.horns_trait = TraitCard(HORNS, 6) self.cooperation_trait = TraitCard(COOPERATION, 1) self.scavenger_trait = TraitCard(SCAVENGER, 2) self.species1 = Species(1, 0, 2, [self.cooperation_trait]) self.species2 = Species(6, 2, 1, [self.carnivore_trait]) self.species3 = Species(3, 3, 3, [self.fattissue_trait], 0) self.species4 = Species(5, 5, 5, [self.burrowing_trait]) self.species5 = Species(5, 3, 4, [self.foraging_trait]) self.species6 = Species( 2, 1, 7, [self.carnivore_trait, self.fattissue_trait, self.scavenger_trait], 0) self.species7 = Species(7, 1, 6, [self.horns_trait]) self.player1_species = [self.species1, self.species2] self.player2_species = [self.species3, self.species4, self.species5] self.player3_species = [self.species6, self.species7] self.player1 = PlayerState(1, 0, [], self.player1_species) self.player2 = PlayerState( 2, 3, [self.carnivore_trait, self.fattissue_trait], self.player2_species) self.player3 = PlayerState(3, 6, [self.burrowing_trait], self.player3_species) self.public_player1 = PlayerState(1, None, None, self.player1_species) self.public_player2 = PlayerState(2, None, None, self.player2_species) self.public_player3 = PlayerState(3, None, None, self.player3_species) self.list_of_players = [self.player1, self.player2, self.player3] self.dealer1 = Dealer(self.list_of_players, 10, [])