PreLab 10

Critter Tournament
Due by the beginning of lecture on Wednesday, November 9.


Read these two sci-fi short stories: The Nine Billion Names of God by Arthur C. Clarke, and The Last Question by Isaac Asimov.

1. Did you do the reading? (Yes or no will suffice.)

This week you will write a set of classes that define the behaviour of various animals in a 2-dimensional grid world. Your animals will roam the world, and, should they happen upon another animal, will fight to the death. We will first describe some particular animals we want you to create, and then ask you to come up with an animal (or more) of your choosing. Please write or type up your solutions, and hand in a paper copy before the beginning of class on Tuesday. Remember, no late prelabs allowed!


In this part of the prelab you'll practice going through the steps of an inductive proof. In particular, you'll use induction to prove that the sum of the first n odd-indexed Fibonacci numbers is the next even-indexed Fibonacci number. For example, the first eight Fibonacci numbers are 1, 1, 2, 3, 5, 8, 13, 21. The sum of the first, third, fifth and seventh Fibonacci numbers (1 + 2 + 5 + 13) is the eighth Fibonacci number (21).

2. The first odd number is 1. The second odd number is 3, and the third is 5. What is the ith odd number, as a function of i?

3. Our statement S(n), in English, is that the sum of the first n odd-indexed Fibonacci numbers is the subsequent Fibonacci number. Using fx to denote the xth Fibonacci number, restate S(n) symbolically using Sigma (summation) notation. Your summation symbol should appear on the left-hand-side of the equals symbol.

4. To prove this statement holds for all values of n, we need to start with a base case. Show that S(n) is indeed true when n = 1 by solving for the left-hand-side (LHS) and the right-hand-side (RHS) of S(1) and observing that they are (hopefully) equal.

5. We now argue that if S(k) is true for a fixed but arbitrary value of k (the inductive hypothesis), then S(k+1) is also true (the inductive step). Specify these two statements explicitly by filling in the blanks:

Inductive Hypthesis: Suppose that for some integer k, S(k) is true. That is, suppose ___________________.

Inductive Step: We now want to show that S(k+1) must then be true as well. That is, we want to show that ___________________.

6. How does the LHS of S(k+1) relate to the LHS of S(k)? In this case, the LHS of S(k+1) equals the LHS of S(k) plus something. Figure out what that something is, and write an equation specifying that relationship. That is, you should write something that looks like

______________ = ______________ + _______________

where the first blank contains the LHS of S(k+1), the second contains the LHS of S(k), and the last contains whatever is needed to make the equation true.

7. Continue deriving from there until you reach the RHS of S(k+1). You'll probably need to make use of both the inductive hypothesis and the definition of the Fibonacci numbers.

And you're done! If you want more practice, here are a few other statements you can try proving inductively on your own:

S(n): 20 + 21 + 22 + ... + 2n = 2n+1-1. This holds for all n ≥ 0.

S(n): 2n < n! This holds for all n ≥ 4.

S(n): 12 + 22 + 32 + ... + n2 = n(n+1)(2n+1)/6. This holds for all n ≥ 0.

Critter Summary

Before you start, let's take a look at this screenshot of the animal world, which is shown below.

What you see here is a grid with mice (labeled 'M'), stones (labeled 'S'), elephants (labeled 'E'), tigers (labeled 'T'), and chameleons (labeled 'C'). This is their initial placement in the grid world. The Go button starts the simulation; on each round of the simulation, each critter (stones count as critters here, albeit relatively uninteresting ones) is asked which direction it wants to move. On each round, each critter can move one square to the north, south, east, west, or stay at its current location. Critters move around in a world of finite size, but the world is toroidal, as it was in our game of life (going off the end to the right brings you back to the left and vice versa; going off the end to the top brings you back to the bottom and vice versa).

As the simulation runs, animals can collide by moving onto the same location. When two animals collide, they fight to the death. The winning animal survives and the losing animal is removed from the simulation. The following table summarizes the possible fighting choices each animal can make and which animal will win in each case. To help you remember which beats which, notice that the starting letters and win/loss ratings of "roar, pounce, scratch" correspond to those of "rock, paper, scissors." If the animals make the same choice, the winner is chosen at random.

Critter #2
Critter #1 ROAR  random winner  #2 wins #1 wins
POUNCE #1 wins  random winner  #2 wins
  SCRATCH   #2 wins #1 wins  random winner 

Critter Behaviour

Each critter you implement must have some specific state and functionality defined. In particular, each critter will implement the following methods, amongst other things:

    def fight(self, oppInfo):
    def getColor(self):
    def getMove(self, info):
    def getChar(self):

For example, a Stone may be implemented with the following functionality, where ROAR and CENTER are defined critter constants:

import critter
import color    

class Stone(critter.Critter):

    def fight(self, oppInfo):
        return critter.ROAR  # Stones always ROAR, obvi.
    def getColor(self):
        return color.GRAY    # Stones are drawn GRAY
    def getMove(self, info):
        return critter.CENTER  # Stones never move

    def getChar(self):
        return 'S'  # Displayed with an 'S'

Critters To Implement

In your lab you will be implementing the following five critters, which must have the following behaviour:


 fighting behaviour  always SCRATCH
 movement behaviour  alternates between EAST and SOUTH in a zigzag pattern (first EAST, then SOUTH, then EAST, then SOUTH,...)
 character  'M'


 fighting behaviour  always ROAR
 movement behaviour  moves 3 steps in a random direction from NORTH, SOUTH, EAST, or WEST, then chooses a new random direction from the same four and repeats
 character  'T'


 fighting behaviour  if opponent is a Tiger ('T'), then ROAR; otherwise POUNCE
 movement behaviour  first go SOUTH steps times, then go WEST steps times, then go NORTH steps times, then go EAST steps times (a clockwise square pattern), then repeats
 character  'E'

The Chameleon

 fighting behaviour the strategy that beats the most used strategy against the chameleons so far (not just this one chameleon)
 color the color used by this particular chameleon's last opponent
 movement behaviour  if the chameleons have lost the majority of their fights so far, then use avoidance. Otherwise, move towards opponents.
 character 'C'

Your Own Critter

 fighting behaviour  you decide
 movement behaviour  you decide
 character  you decide

You will decide the behaviour of your own critter. Your critter should be named after yourself. Please use your first name followed by your last initial. For example, my critter would be named TomW.

What Should Your Critter Do?

Part of your lab grade will be based upon writing creative and non-trivial behaviour for your own critter. The following are some guidelines and hints about how to write an interesting critter.

When a critter moves, it has access to some important but limited information that it may wish to use to guide its movement behaviour. For example, it has access to its current x and y coordinates, as well as the width and height of the grid world. It can also find out what kind of critters are to its east, west, north, south, north-east, north-west, south-east, or south-west if there are any critter there at all. Note that you can see diagonally, but you cannot move diagonally (in one step).

8. A non-confrontational (or introverted) critter may always want to move away from other critters. If such a critter sees another critter to the north it shouldn't move north, but additionally, south is probably safer than either east or west. Unless of course there is another critter to the southwest, in which case maybe east is the best bet.

One could imagine (unpleasantly) making a couple hundred if-statements to handle every possible configuration of neighbors. But we'd like to be a bit smarter (and more concise). One idea is this: make a variable for each of the four cardinal directions representing the "danger score" of moving in that direction, plus a danger score for not moving at all. For each neighbor you see, increase the danger scores in a way that represents the threat posed by that neighbor. Then pick the direction with the lowest danger score.

Give pseudocode that determines where such a critter should move based on its neighbours to the north, south, east, west, north-east, north-west, south-east, and south-west. In generating danger scores, the precise numbers you choose are up to you, but try to make sure they make lead to reasonable choices in various situations.

When a critter fights another critter (by both moving into the same square), you can find out the display character of the critter you are fighting against and adjust your strategy accordingly.

9. If you are fighting a Mouse, what fighting behaviour should you have in order to always win? How about against an Elephant? Given the strategy for the Chameleon, what would be a reasonable attack to use against the Chameleon if you knew there were equal numbers of Mouse, Tiger and Elephant critters in the arena.

Your critter can use any character you like as its display character. In fact, critters are asked what display character to use on each round of the simulation, so you can have a critter that displays itself differently over time.

Your critter does not have to play a fixed strategy against each type of critter; you can have a critter that learns its strategy over time, based on its previous fighting results. For example, if critter has fought with a Tiger three times, and the Tiger always Pounces, then the next time it encounters a Tiger, it would seem that Scratching may be a good fighting strategy.

10. Describe the behavior of your own critter by filling out the "you decide" entries in the Your Own Critter table further up in this prelab. Try to make it interesting without getting overly ambitious (there is only so much information available to you). Don't specify actions that go beyond the scope of what a critter is allowed to do -- long-range attacks, teleportation, and invulnerability are all no-nos.

Honor Code

If you followed the Honor Code in this assignment, write the following sentence attesting to the fact at the top of your homework.

I affirm that I have adhered to the Honor Code in this assignment.