The Rabbit HuntThe user interfaceThe program designThe eight classesRabbitHunt IRabbitHunt IIControllerViewModelA note about namesWhy MVC is goodDirectionsThe turn methodOther objectsThe Bush classIsn’t Bush totally useless?Creating and detecting BushesThe Animal classAnimal instance variablesAnimal methods IAnimal methods IIint decideMove( )How the rabbit movesHow the fox movesLooking around...Heading toward the rabbitHaven’t seen a rabbitCan’t move ahead, can’t dodge bushThe assignmentThe EndThe Rabbit HuntAn example Java programThe user interfaceThe program designThe eight classes•RabbitHunt -- just gets things started•Controller -- accepts GUI commands from user•View -- creates the animated display•Model -- coordinates all the actual work–Bush -- just sits there–Animal -- handles basic sight and movement–Fox -- an Animal that tries to catch the rabbit–Rabbit -- an Animal that tries to escape the foxRabbitHunt Ipublic class RabbitHunt { // class variables private static Object[ ][ ] field; private static Model model; private static View view; private static Controller controller; static int numberOfRows; static int numberOfColumns;RabbitHunt II public static void main(String args[]) { numberOfRows = numberOfColumns = 20; field = new Object[numberOfRows][numberOfColumns]; model = new Model(field); view = new View(field); controller = new Controller(model, view); }Controller•Creates the GUI (buttons, scrollbar, “field”•Handles user actions (button presses, moving the scrollbar, resizing the window)•Enables and disables buttons as needed•Alternately--–tells the model to “make a move”–tells the view to display the results•Displays a final message when the hunt endsView•Displays the current state of the hunt, that is, the “field” and the things on it•(That’s all it does)Model•Places the fox, rabbit, and bushes in the field•Alternately gives the rabbit and the fox a chance to move•Decides when the hunt is over (and who won)•Provides several constants and a method for use by the animalsA note about names•I have named the central classes Model, View, and Controller to make the connection with the MVC model obvious•I could have named them anything I wanted•In this program, the Model actually comprises five classes: Model (the “boss” class), Animal and its subclasses Fox and Rabbit, and BushWhy MVC is good•The Controller class sets up lots of GUI stuff and handles it–You haven’t studied GUIs yet•The View class does a lot of work–you can probably figure out how View works•None of this matters to your assignment!–Because the model is independent of the view and the controller, you can totally ignore them–Still, you might learn something from them...Directions•Because Java does not define a “direction” type, Model provides several constants:–N, S, E, W -- the four main compass directions–NE, NW, SE, SW -- the four secondary directions–MIN_DIRECTION, MAX_DIRECTION -- in case you want a for loop that goes through all eight directions (you probably will)–STAY -- a direction meaning “don’t move”The turn method•The Model class provides one direction method that you might find useful:•static int turn(int direction, int amount)•Given a direction and an amount to turn clockwise, turn returns the resultant direction•Examples:–turn(Model.N, 1) returns Model.NE–turn(Model.N, -2) returns Model.WOther objects•Model also provides constants for “things you can see”:–BUSH, RABBIT, FOX -- the obvious things–EDGE -- the edge of the “playing field”•In other classes (such as Rabbit), you can refer to these constants as Model.BUSH, Model.FOX, Model.NW, Model.STAY, etc.The Bush class•We’ll start with the simplest class: Bush•What does a bush have to know?•What must a bush be able to do?•Here’s the complete definition of this class: public class Bush {}•Believe it or not, this is still a useful class!Isn’t Bush totally useless?•(Please note: this is not a reference to the current U.S. president)•With another program design, a Bush might be expected to draw itself–In MVC, it doesn’t even do that--View does•The program can (and does) create bushes•The program can (and does) detect whether a square in the field contains a bushCreating and detecting Bushes•To create a bush: Bush bush = new Bush();–Works because Bush has a default constructor•To test if an object obj is a bush: if (obj instanceof Bush) ...–instanceof is a keyword, used mainly like this•This is all we do with the Bush classThe Animal class•Animal is the superclass of Fox and Rabbit–Hence, Fox and Rabbit have a lot in common–You can get ideas about how to program a Rabbit by studying the Fox class•Animal provides several important methods that can be used directly by any subclassAnimal instance variables public class Animal { private Model model; int row; int column;•The model gives access to several constants•The row and column tell you where you are–You may look at these variables, but you are not allowed to change them–I tried to make it impossible for you to change these variables, but I didn’t succeedAnimal methods I•int look(int direction)–look in the given direction (one of the constants Model.N, Model.NE, etc.) and return what you see (one of Model.BUSH, Model.EDGE, etc.)–Example: if (look(Model.N) == Model.FOX)•int distance(int direction)–returns how many steps it is to the nearest object you see in that direction (if 1, you’re right next to it)–diagonal steps are no longer than other stepsAnimal methods II•boolean canMove(int direction)–tells whether it is possible for you to move in the given direction–false if that move would put you in a bush or off the edge of the board–true if that move would be to an empty space–true if that move would be onto another animal Good for the fox, bad for the rabbitint decideMove( )•The fox and the rabbit each have only one responsibility: to decide where to move next•The decideMove( ) method does this•decideMove( ) returns an integer–It can return one of the eight direction constants–It can also return the constant Model.STAY –If decideMove( ) returns an illegal move, it is treated as Model.STAY•This doesn’t seem like much, but “deciding a move” is what you do in many gamesHow the rabbit
View Full Document