copyright 2010 by pearson education 1 assignment 11: critters

16
Copyright 2010 by Pearson Education 1 Assignment 11: Critters

Upload: nelson-ralf-daniel

Post on 23-Dec-2015

213 views

Category:

Documents


0 download

TRANSCRIPT

Copyright 2010 by Pearson Education1

Assignment 11:Critters

Copyright 2010 by Pearson Education2

Overview of CrittersCritters 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.

Copyright 2010 by Pearson Education3

Overview of Critters ContdThe 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.

Copyright 2010 by Pearson Education4

CrittersA simulation world with animal objects with behavior:

fight animal fightinggetColor color to displaygetMove movementtoString letter to display

You must implement:LionTigerBearWolf (Creative)

Animal counts

Buttons

2-D World

Copyright 2010 by Pearson Education5

Movie of simulation

Copyright 2010 by Pearson Education6

A Critter classpublic class name implements Critter { ...

}

implements Critter tells the simulator your class is a critteran example of interfaces

Must implement all of the methods specified by the interface

interface is like a "to do list"

Copyright 2010 by Pearson Education7

Critter Interface

Copyright 2010 by Pearson Education8

InterfaceDeclares methods that classes of that type

must implementclasses 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

Copyright 2010 by Pearson Education9

Examples of behavior of FoxMove 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-scissorsFox alternates between Rock and Paper,

regardless of opponent.

Copyright 2010 by Pearson Education10

How the simulator worksWhen 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.

Copyright 2010 by Pearson Education11

Critter exercise: StoneWrite a critter class Stone(the dumbest of all critters):

Method Behavior

constructor

public Stone()

fight Always ROCK

getColor Always Color.GRAY

getMove CENTER

toString "S"

Copyright 2010 by Pearson Education12

Ideas for stateYou 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?

Copyright 2010 by Pearson Education13

Keeping stateHow 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; }}

Copyright 2010 by Pearson Education14

Testing crittersFocus on one specific critter of one specific type

Only spawn 1 of each animal, for debugging

Make sure your fields update properlyUse println statements to see field values

Look at the behavior one step at a timeUse "Tick" rather than "Go"

Copyright 2010 by Pearson Education15

Critter exercise: SnakeMethod Behavior

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"

Copyright 2010 by Pearson Education16

Determining necessary fieldsInformation required to decide what move to make?

Direction to go inLength of current cycleNumber of moves made in current cycle

Remembering things you've done in the past:an int counter?a boolean flag?