1 / 16

Assignment 11: Critters

Assignment 11: Critters. 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.

zeph-chaney
Download Presentation

Assignment 11: Critters

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Assignment 11:Critters

  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. Overview of Critters Contd • The simulation engine works like Conway’s Game of Life engine. • Start off with a pattern of animals in a 2-D grid. • Draw the grid. • 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. • If they encounter another animal, there is a fight to the death. • When all fights are over, go back to 1. • The simulation also summarizes the number of each animal remaining.

  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 2-D World Buttons

  5. Movie of simulation

  6. A Critter class public class nameimplements 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. Critter Interface

  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. 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. 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. Critter exercise: Stone • Write a critter class Stone(the dumbest of all critters):

  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. 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. 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. Critter exercise: Snake

  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?

More Related