"Vega" design

Interface Occupant

All known implementing classes:
Robot, Beeper


public interface Occupant

Contains all the methods and fields necessary to give total autonomy to the occupant of a maze position to decide to move to a next position in the maze based on the information it gets from the square object representing its current position in the maze. Keeps track of its position (the object representing the occupied maze position). It provides methods to decide and make effective a move from one position to the next, an to give information as to the deltaX and deltaY spaces the occupant will have to move to get to the chosen new position.

void setPoint (int x, int y) Sets the Point object indicating where in the Square an occupant should be drawn
Method Summary
Square chooseNewPosition()
Returns the best choice of a Square - position in the maze - to move to. This method uses private methods to check that the move is legal and in line with the object's goal in the game as set out by private requirements and restrictions for moving a particular in the maze.
void changePosition(Square newHost)
This method is called to move to another position in the maze. The move is made effective by setting the occupant's position field to the square representing the new position.Accordingly, the square object representing this new position in the maze will adjust its occupant field to point to it.
Square getPosition ()
This method returns the Square object this Occupant occupies.

Class BeeperHunt


public class BeeperHunt extends Frame implements Observer, ActionListener

This class is used to start and control the development of the BeeperHunt game. It acts as the GUI which has a button to to start and stop the game and a panel representing the maze where the game takes place. This class launches the Robot's search for beepers, updates the game board, checks the life clock of the beepers, and replaces them accordingly in response to Robot moves of which it is an Observer.

Constructor
BeeperHunt ( )
Creates a Maze object, a Robot object and 2 beeper objects initializing the corresponding variables.
Method Summary
static void main (String [ ] args)
Creates a new BeeperHunt object and calls the buildGame() method.
void buildGame ()
Sets up the GUI. Adds the Start and Quit buttons and the Maze to the GUI.
void ActionPerformed (ActionEvent e)
If the start button is the source of the event then the Robot and Beepers are placed in the maze at a random position, and the Robot's startSearch method is called. If the source of the event is the Quit button then the program exits.
void update (Observable robot, Object obj)
This method is activated every time the Robot moves and calls the setChanged and NotifyObserver methods. This method will check if the lifeTime of the Beeper objects is up. It checks the Robot's numberOfSteps, calculates the life lived so far by a Beeper, and compares it wuth its lifeTime. If its life is up, it will remove the beeper, create a new one and place it in the maze
void changeBeeper (Beeper beeper)
Does all the necessary changes to remove a beeper from the game and place a new one
Field Summary
Beeper beeper1
This variable holds the reference to one of the two beeper objects
Beeper beeper2
This variable holds the reference to one of the two beeper objects
Maze maze
This is the cariable that holds a reference to the maze

Class Maze


public class Maze extends Panel implements Observer

This class creates the Maze where the Beeper hunt will take place. It first creates an array of squares with all walls present and then it goes on to shape the maze by visiting each square and erasing one of its walls. The array of squares that has been modified to represent a maze is then painted when the maze is created and each time the state of the Robots and Beepers change.

Constructor
Maze ()
Creates an array of Square objects representing each position in a maze. It not only fills the array with Squares but modifies each Square object to be aware of each of its neighbors by updating the corresponding fields
Method Summary
void buildMaze ()
Modifies the created array to shape it as a multiple path maze made up of 4 X 4 arrangement of standard mazes. It uses private methods to erase walls and create the "standard" submazes and connect them
int getSpeed ()
int getRows ()
int getColumns ()
Point place(Square s)
It acceses the square's row and column fields and returns a Point object corresponding to the x and y coordinates in the maze drawing where the piece should paint itself. To calculate the point's coordinates it uses private fields and methods
void paint (Graphics g)
Paints the maze and all the pieces in it. It uses some data structure to keep track of which pieces are present and calls their paint method accordingly
void update (Observable piece, Object nothing)
Each time a piece changes its state in the maze this method is activated so the board can repaint itself accordingly

Class Piece


public abstract class Piece extends Observable

This class represents the general functionality of a piece. It provides all the methods and fields necessary to place, move and remove a piece into and from the Square object representing a maze position.

Constructor
Piece (Board board)
Constructs a piece. It sets the Board object as an observer.
Method Summary
Point place (Square s)
Makes the move effective in the maze by returning the point object with the x an y coordinates inside the Square where it will draw itself.
void remove ()
Removes a piece from the maze. It sets its Square object's Occupant reference to null and its reference to the Square object to null.
Point moveTo (Square s)
It calculates the absoluteMove and number of steps and moves the piece acordingly updating observers with each step moved. Makes the move effective in the maze by returning the point object with the x an y coordinates inside the Square where it will draw itself.

Class Robot


public class Robot extends Piece implements Occupant

This class adds to the general functionality of a piece the particular characteristics of a Robot piece. It does this by giving the implementation to the Occupant interface that best fits a Robot object and adding some private functionality. In this sense it has fields and implements methods for an efficient beepers search, and move from Square to Square. void setPoint ()
Method Summary
Square chooseNewPosition ()
Makes the best choice of a Square to move to. It makes the deciison based on its knowledge of its "host" Square's wall data and on the information about beepers at sight it gathers.
Square changePosition (Square s)
Same implementation as described in the Interface definition. It calls its moveTo method to set its Point field
Square getPosition ()
void search ()
This method implements the search algorithmm for finding beepers and moving through the maze. It combines its private search algorithm with all the Occupant and Piece methods to move the piece.
void paint (Graphics g)
Draws a Robot inside in the coordinates corresponding to the middle of the Square object its occupies.
Same as in interface definition

Class Beeper


public class Beeper extends Piece implements Occupant

Represents a Beeper. It adapts the general Piece and Occupant implementation to its functionality as a Piece which basically is placed and then removed. Its private fields keep information about its previous locations and its lifetime

void setPoint ()
Constructor
Beeper (int stepsSoFar)
Creates a new Beeper with the number of steps (time) elapsed so far. This variable will then be used to compare it with the future count of Robot steps to calculate death.
Method Summary
Square chooseNewPosition ()
Chooses a new position to place a Beeper after checking that this position has not been used before and is not occupied
Square changePosition (Square s)
It calls the its remove and place methods. The new Point field is set as a result. It uses private methods to keep track of its previous locations
Square getPosition ()
void paint (Graphics g)
Draws a Beeper inside in the coordinates corresponding to the middle of the Square object its occupies.
Same as in interface definition
int getLifeTime ()

Class Square


public class Square

Represents a Square. Has private fields to represent its walls and methods to keep track of its occupants and to check whether it is occupied.

Constructor
Square (int seqNum, int colPos, int rowPos)
Sets its walls to true (all present) and initializes private fields which indicate whether itr has been visited, initilizes data structures that store neighbors, its reference to an occupant, its sequence number in the array and its row and column positions in the array
Method Summary
addNeighbor (Square neighbor)
Adds a neighbor to its set of neighbors
boolean equals (Object o)
Equality test is based on sequence number
int hashCode ()
The hash Code is a number calculated based on the column and row positions
boolean isNeighbor (Square s)
Returns true if s is an adjacent square in the maze, false otherwise
Set getNeighbors ()
Returns all the adjacent squares in the maze
int modClass ()
Returns the modulo 5 class of the square. This is used by the maze to find out where in the maze is each square relative to its neighbor (up, down, left or right). I they are in the same modulo 5 class they are verticaly adjacent otherwise they are horizontally adjacent
int getSeqNum ()
int getCol ()
int getRow ()
Piece getOccupant ()
void setOccupant (Occupant o)
void removeOccupant()
Sets the reference to an occupant to null
boolean hasOccupant()
Returns true if it ahs an occupant. False otherwise
boolean isVisited()
Returns true if it has been visited, false otherwise
void setVisited(boolean is)
Sets to true the isVisited flag
void addOpenNeighbor(Square n)
Pots n in the set of neighbors with open walls

Class Point


public class Point

Represents a Point in the drawing panel where the maze is drawn.

Constructor
Point (int x , int y)
Constructs a point with the x and y positions
Method Summary
int getX ()
int getY ()

Class Radar


public class Radar

Represents a Radar which looks for beepers at sight.

Constructor
Radar (Square S)
Constructs a radar object with s as its reference point to search from
Method Summary
Square getTarget ()
returns a Square object representing the position where a beeper has been seen