Class Model

java.lang.Object
  |
  +--Model

public class Model
extends java.lang.Object

Creates and maintains a "field" for the rabbit and fox, and sets up a number of methods to be used by animals. This class, rather than the Animal class, is in charge of letting animals move and look around--this makes it harder for an animal to "cheat" by knowing things it shouldn't know, or making moves it shouldn't be allowed to make.

Version:
October 12, 2001
Author:
David Matuszek

Field Summary
(package private) static int BUSH
          Indicates a bush
(package private) static int E
          Represents the direction EAST
(package private) static int EDGE
          Indicates the edge of the playing field
(package private) static int FOX
          Indicates a fox
(package private)  boolean gameIsOver
          Flag to tell when the game ends
(package private) static int MAX_DIRECTION
          The largest int representing a direction
(package private)  int MAX_NUMBER_OF_STEPS
          The number of turns the rabbit has to survive in order to win
(package private) static int MIN_DIRECTION
          The smallest int representing a direction
(package private) static int N
          Represents the direction NORTH
(package private) static int NE
          Represents the direction NORTHEAST
(package private) static int NW
          Represents the direction NORTHWEST
(package private) static int RABBIT
          Indicates a rabbit
(package private)  boolean rabbitIsAlive
          Flag used to end the game when the fox wins
(package private) static int S
          Represents the direction SOUTH
(package private) static int SE
          Represents the direction SOUTHEAST
(package private) static int STAY
          Represents the direction "right here"
(package private)  int stepsTaken
          The number of turns taken so far in the game
(package private) static int SW
          Represents the direction SOUTHWEST
(package private) static int W
          Represents the direction WEST
 
Constructor Summary
(package private) Model(java.lang.Object[][] field)
          Constructs a model that uses the given field.
 
Method Summary
(package private)  void allowMoves()
          Gives the rabbit a chance to move, then gives the fox a chance to move.
(package private) static int columnChange(int direction)
          Determines how moving in the given direction affects the column number.
(package private)  int distance(int row, int column, int direction)
          Determines the distance to the nearest thing, or to the edge of the field, looking in a given direction.
(package private)  boolean legalLocation(int row, int column)
          Determines whether the given row and column numbers represent a legal location in the field.
(package private)  int look(int row, int column, int direction)
          Determines what can be seen from a given location, looking in a given direction.
(package private)  void populate()
          Puts a rabbit, a fox, and some bushes in the field.
(package private) static int random(int min, int max)
          Utility method to choose a random integer from min to max, inclusive.
(package private)  void reset()
          Sets up a new hunt.
(package private) static int rowChange(int direction)
          Determines how moving in the given direction affects the row number.
(package private) static int turn(int direction, int number)
          Given a direction and a number of times to make 1/8 turn clockwise, return the resultant direction.
 
Methods inherited from class java.lang.Object
, clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

BUSH

static final int BUSH
Indicates a bush

E

static final int E
Represents the direction EAST

EDGE

static final int EDGE
Indicates the edge of the playing field

FOX

static final int FOX
Indicates a fox

gameIsOver

boolean gameIsOver
Flag to tell when the game ends

MAX_DIRECTION

static final int MAX_DIRECTION
The largest int representing a direction

MAX_NUMBER_OF_STEPS

final int MAX_NUMBER_OF_STEPS
The number of turns the rabbit has to survive in order to win

MIN_DIRECTION

static final int MIN_DIRECTION
The smallest int representing a direction

N

static final int N
Represents the direction NORTH

NE

static final int NE
Represents the direction NORTHEAST

NW

static final int NW
Represents the direction NORTHWEST

RABBIT

static final int RABBIT
Indicates a rabbit

rabbitIsAlive

boolean rabbitIsAlive
Flag used to end the game when the fox wins

S

static final int S
Represents the direction SOUTH

SE

static final int SE
Represents the direction SOUTHEAST

STAY

static final int STAY
Represents the direction "right here"

stepsTaken

int stepsTaken
The number of turns taken so far in the game

SW

static final int SW
Represents the direction SOUTHWEST

W

static final int W
Represents the direction WEST
Constructor Detail

Model

Model(java.lang.Object[][] field)
Constructs a model that uses the given field.
Parameters:
field - the field to be used by the model
Method Detail

allowMoves

void allowMoves()
Gives the rabbit a chance to move, then gives the fox a chance to move.

columnChange

static int columnChange(int direction)
Determines how moving in the given direction affects the column number.
Parameters:
direction - the direction in which to move
Returns:
the amount by which the column number will change

distance

int distance(int row,
             int column,
             int direction)
Determines the distance to the nearest thing, or to the edge of the field, looking in a given direction.
Parameters:
row - the row of the object doing the looking
column - the column of the object doing the looking
direction - the direction of the look
Returns:
the distance

legalLocation

boolean legalLocation(int row,
                      int column)
Determines whether the given row and column numbers represent a legal location in the field.
Parameters:
row - the row number
column - the column number

look

int look(int row,
         int column,
         int direction)
Determines what can be seen from a given location, looking in a given direction.
Parameters:
row - the row of the object doing the looking
column - the column of the object doing the looking
direction - the direction of the look
Returns:
the object seen

populate

void populate()
Puts a rabbit, a fox, and some bushes in the field.

random

static int random(int min,
                  int max)
Utility method to choose a random integer from min to max, inclusive.
Parameters:
min - the smallest number to be returned
max - the largest number to be returned
Returns:
a random number N, where min <= N <= max

reset

void reset()
Sets up a new hunt.

rowChange

static int rowChange(int direction)
Determines how moving in the given direction affects the row number.
Parameters:
direction - the direction in which to move
Returns:
the amount by which the row number will change

turn

static int turn(int direction,
                int number)
Given a direction and a number of times to make 1/8 turn clockwise, return the resultant direction.
Parameters:
direction - the initial direction
number - of 45 degree turns clockwise
Returns:
the resultant direction


Generated by BlueJ