6 — Incremental refinement, the second step

Due Sunday, 21 February, midnight Wednesday, 24 February, noon

Your software architect has determined that the dealer can perform some actions of the feeding step without input from the players. For example, if a player possesses a single hungry species and there is food available at the watering hole, the rules dictate that the player will feed this species. Similarly, if a player is in possession of a single hungry carnivore and only one other player has an attackable species, then the former will launch the attack on the latter. The dealer ought to ask the player, however, if it is willing to attack one its own species with such a single hungry carnivore. In other situations, the dealer must interact with the player and acquire the necessary information before it can take the next step in the feeding cycle.

Since this interaction is the most complex part of the player-dealer interaction, your architect asked for the attackable? method last week and now requests a method that implements a sample strategy for picking the next species to be fed.

Rule Change After some reflection, your software architect has also decided to disallow duplicate trait cards for a species.

Rule Reinforcements The architect also re-considered two additional points:
  • Carnivores cannot attack themselves or, succinctly, carnivores are not cannibals.

  • If a species becomes extinct due to an attack, its food is lost to the player and the species board is immediately removed from the player’s possession. The player receives two cards at the end of the turn for this loss.

Task 1 Design a data representation for the players of the Evolution game.

Develop a function (method) that determines a player’s next species to be fed. Recall that this may be either a vegetarian or a carnivore. In the latter case, the response must not only specify which carnivore to feed but also which species it will attack and to which player this species belongs.

The function has the following kinds of contracts:
  • Its type signature dictates that it consumes representations of the other players, in case one of the player’s carnivores must attack a species of some other player. The result type ought to describe the following possibilities:
    1. one of its own species with the fat-tissue trait and the number of tokens it wishes to store

    2. one of its own vegetarian species

    3. one of its own carnivores plus a different player and one of this player’s species

    4. an indication that it does not wish to feed any species anymore.

  • Its behavioral contract adds that the second and third kind of results must be "hungry" species and that in the third case, the first species can successfully attack the second one.

  • The sequencing constraints of the method implies that there is some food at the watering hole and that the player is in possession of at least two hungry species or of a carnivore that must decide which species to attack, if any.

Strategy This particular player feeds the species according to the following strategy:
  1. If any species has a fat-tissue trait, the player will first request as much food as this trait allows. In case, more than one species has the fat-tissue trait, the player picks the one with the largest need for fat-tissue food. Finally, if two or more species have a largest need for fat-tissue food, the player goes by an order on species (specified below) and, secondary to that, uses the order of species boards.

  2. If any of its vegetarians is still hungry, it feeds the largest one of those next. Ties are broken by the order of the species boards.

  3. If there are no hungry vegetarians, the player use the largest carnivore that can attack some other species from the other players It attacks the largest species that can be attacked. A tie is broken with the order in which the players are given; the first player’s species in this ordering is picked; within the player, the strategy breaks ties via the ordering of the player’s species boards.

  4. The player will not use one of its carnivores to attack any of its own species.

Ordering Species are ordered in a lexicographic manner, starting with the (plain) population attribute, followed by the food that the species has been fed so far, and finally the (plain) body size.

Delivery Create a sub-directory called feeding and place all necessary files/modules for the data representation and the method into this sub-directory.

Task 2 Develop a test harness for the method that accepts a single JSON expression from STDIN and writes its results to STDOUT after turning it into JSON.

The input consists of a single Feeding. The output is one of:
  • false, meaning the only possibility is to have one of this player’s species attack some other species and the player chooses to forgo any attack now and for the rest of this turn;

  • Species+, meaning the species is vegetarian and gets the next token of food;

  • [Species+, Nat], meaning the species comes with a fat-tissue trait and wishes to store the specified number of food tokens;

  • [Species+, Player, Species+], meaning the first species attacks the second species, which belongs to the given player.

Here are the relevant data definitions:

A Feeding is [Player, Natural+, LOP]. The natural number in the middle specifies how many tokens of food are left at the watering hole.

A Player is




A LOP is [Player, ..., Player]; the list might be empty.

A LOS is [Species+, ..., Species+]; the list might be empty.

A Natural+ is a JSON number interpretable as a natural number larger than, or equal to, 1.

A Natural is a JSON number interpretable as a natural number.

A Species+ is one of:
  • a regular Species

  • a Species with a "fat-food" field:





         ["fat-food" ,Nat]]

    The additional field specifies how much food is stored on the species’s fat-tissue card. The specification is inconsistent if the traits list does not come with the trait "fat-tissue".

Delivery Place an executable called xfeed into your main folder (for this project). Make sure it can be run from the command line like this:

    ./xfeed < some-json-input > some-json-output

The xfeed executable must use the method from task 1. It may assume that all test inputs satisfy the JSON specification above.

Three Bonus Points Create a script called compile that (byte-code)compiles xfeed. Place it into your main folder (for this project). Make sure it can be run from the command line like this Exit status is another one of those things you need to pick up as you turn into a software dev.


and that its exit status is 0 if the compilation is successful. If compile exists and it exits with a status other than 0, we will not run xfeed.

Implied by 4 — From interfaces to protocols Develop up to five pairs of JSON files that represent unit tests for ./xfeed. The input file contains a single JSON expressions that plays the role of a message piped into your harness; the output file contains the expected JSON return messages.

Task 3 Study how a dealer (1) decides whether to call the above method for a player when it is the player’s turn to feed a species and (2) applies the results of the above method to the state of the game.

Delivery Write a one-page PDF memo and name it feed.pdf. There is no need for any header material other than a subject line and your names. Place the document into the folder for this project.





What should xfeed produce if the JSON is well-formed but violates some other constraint(s)?


Your program should not return anything in this case.


The rest was moved to FAQ on Evolution.