Presentation is loading. Please wait.

Presentation is loading. Please wait.

Copyright 2010 by Pearson Education 1 Assignment 11: Critters.

Similar presentations


Presentation on theme: "Copyright 2010 by Pearson Education 1 Assignment 11: Critters."— Presentation transcript:

1 Copyright 2010 by Pearson Education 1 Assignment 11: Critters

2 Copyright 2010 by Pearson Education 2 Overview of Critters Critters is a 2-D rectangular simulation world with moving animals that interact with other animals. Different kinds of animals are implemented in the simulation (or game) that move around in the world and fight with the other animals. Fighting happens when two animals move into the same space, and it results in the death of one of the two. Each animal is described by a Class that represents its unique rules of moving and fighting. You will write four Classes – one for each of 4 animals. The simulation engine will be the client of (i.e. will use) your 4 classes. The engine is provided to you. Running the simulation will show you (graphically) the behavior of the animals in their world.

3 Copyright 2010 by Pearson Education 3 Overview of Critters Contd The simulation engine works like Conway’s Game of Life engine. 1. Start off with a pattern of animals in a 2-D grid. 2. Draw the grid. 3. After ‘time tick’, the animals move to new positions according to their individual rules. If more than one animal wants to move to the same place, the order in which they move is random. 4. If they encounter another animal, there is a fight to the death. 5. When all fights are over, go back to 1. The simulation also summarizes the number of each animal remaining.

4 Copyright 2010 by Pearson Education 4 Critters A simulation world with animal objects with behavior: fight animal fighting getColor color to display getMove movement toString letter to display You must implement: Lion Tiger Bear Wolf (Creative) Animal counts Buttons 2-D World

5 Copyright 2010 by Pearson Education 5 Movie of simulation

6 Copyright 2010 by Pearson Education 6 A Critter class public class name implements Critter {... } implements Critter tells the simulator your class is a critter an example of interfaces Must implement all of the methods specified by the interface interface is like a "to do list"

7 Copyright 2010 by Pearson Education 7 Critter Interface

8 Copyright 2010 by Pearson Education 8 Interface Declares methods that classes of that type must implement classes that implement the interface must have the method, but how the method works can be very different A way of ensuring certain behaviors / methods of classes

9 Copyright 2010 by Pearson Education 9 Examples of behavior of Fox Move in a 5 steps x 5 steps square: N, E, S, W, N … Each time tick, the fox takes one step in the appropriate direction. Fight – all fights are rock-paper-scissors Fox alternates between Rock and Paper, regardless of opponent.

10 Copyright 2010 by Pearson Education 10 How the simulator works When you press "Go", the simulator enters a loop: move each animal once ( getMove ), in random order if the animal has moved onto an occupied square, fight ! Key concept: The simulator is in control, NOT your animal. Example: getMove can return only one move at a time. getMove can't use loops to return a sequence of moves. It wouldn't be fair to let one animal make many moves in one turn! Your animal must keep state (as fields, instance variables) so that it can make a single move, and know what moves to make later.

11 Copyright 2010 by Pearson Education 11 Critter exercise: Stone Write a critter class Stone (the dumbest of all critters): MethodBehavior constructor public Stone() fight Always ROCK getColor Always Color.GRAY getMove CENTER toString"S"

12 Copyright 2010 by Pearson Education 12 Ideas for state You must not only have the right state, but update that state properly when relevant actions occur. Counting is helpful: How many total moves has this animal made? How many times has it fought? Remembering recent actions in fields is helpful: Which direction did the animal move last? How many times has it moved that way?

13 Copyright 2010 by Pearson Education 13 Keeping state How can a critter move west until it fights? public Direction getMove() { while (animal has not fought) { return Direction.EAST; } while (animal has not fought a second time) { return Direction.EAST; } } private int moves; // total moves made by this Critter public int getMove() { moves++; if (moves % 4 == 1 || moves % 4 == 2) { return WEST; } else { return EAST; } }

14 Copyright 2010 by Pearson Education 14 Testing critters Focus on one specific critter of one specific type Only spawn 1 of each animal, for debugging Make sure your fields update properly Use println statements to see field values Look at the behavior one step at a time Use "Tick" rather than "Go"

15 Copyright 2010 by Pearson Education 15 Critter exercise: Snake MethodBehavior constructor public Snake() fight SCISSORS getColor black getMove 1 E, 1 S; 2 W, 1 S; 3 E, 1 S; 4 W, 1 S; 5 E,... toString"K"

16 Copyright 2010 by Pearson Education 16 Determining necessary fields Information required to decide what move to make? Direction to go in Length of current cycle Number of moves made in current cycle Remembering things you've done in the past: an int counter? a boolean flag?


Download ppt "Copyright 2010 by Pearson Education 1 Assignment 11: Critters."

Similar presentations


Ads by Google