first commit
This commit is contained in:
396
CS1322/p6/Krystian/Ghost.java
Normal file
396
CS1322/p6/Krystian/Ghost.java
Normal file
@@ -0,0 +1,396 @@
|
||||
/**
|
||||
* CS1322: Programming Assignment #6 - Fall 2002
|
||||
*
|
||||
* <PRE>
|
||||
* IDBox for: Ghost.java
|
||||
*
|
||||
* Revisions: 1.0 Nov. 23, 2002
|
||||
* Created the Ghost class
|
||||
*
|
||||
* </PRE>
|
||||
*
|
||||
* Collaboration statement:
|
||||
* "I worked on the homework assignment alone, using only
|
||||
* course materials."
|
||||
*
|
||||
* @author
|
||||
<A HREF="mailto:gtg142g@mail.gatech.edu">Roland Krystian Alberciak</A>
|
||||
* @version Version 1.0, Nov. 23, 2002
|
||||
*/
|
||||
|
||||
// imports are listed above IDBox
|
||||
|
||||
/**
|
||||
* MODEL.
|
||||
* Ghost. Ghost extends MoveablePiece.
|
||||
* This class will model the Ghost piece which the AI will control.
|
||||
*/
|
||||
|
||||
public class Ghost extends MoveablePiece
|
||||
{
|
||||
/** Debug value for toggling System.out.* output */
|
||||
public static boolean DEBUG = false;
|
||||
public static boolean DEBUG_CODE = false;
|
||||
public static boolean DEBUG_MAIN = false;
|
||||
|
||||
|
||||
/*
|
||||
// =======================================================================
|
||||
// Constructors
|
||||
// =======================================================================
|
||||
*/
|
||||
|
||||
/**
|
||||
* Constructor 1. Specified default-overriding constructor for Ghost.
|
||||
* @param void No parameters.
|
||||
* @return Ghost instance/object.
|
||||
*
|
||||
public Ghost()
|
||||
{
|
||||
|
||||
} //end of Ghost()
|
||||
*/
|
||||
|
||||
/**
|
||||
* Constructor 2. Specified default-overloading constructor for Ghost.
|
||||
* @param x the location along the x-axis.
|
||||
* @param y the location along the y-axis.
|
||||
* @param ghostNum this ghost's number.
|
||||
* @return Ghost instance/object.
|
||||
*/
|
||||
public Ghost( int x, int y, int ghostNum )
|
||||
{
|
||||
super(x,y);
|
||||
|
||||
this.setStartX(x);
|
||||
this.setStartY(y);
|
||||
this.setGhostNum(ghostNum);
|
||||
} //end of Ghost(int, int, int)
|
||||
|
||||
|
||||
|
||||
/*
|
||||
// =======================================================================
|
||||
// Methods which override class Object methods
|
||||
// =======================================================================
|
||||
*/
|
||||
|
||||
|
||||
/**
|
||||
* Equal instance comparison. Compares if two instances are equal.
|
||||
* @param o object-type reference of (hopefully) an Ghost.
|
||||
* @return boolean whether objects are equal.
|
||||
*
|
||||
public boolean equals (Object o)
|
||||
{
|
||||
|
||||
} //end of equals(Object)
|
||||
*/
|
||||
|
||||
/**
|
||||
* toString instance printer. Prints properties of Ghost.
|
||||
* @return System.out.println(Ghost instance) instance print.
|
||||
*/
|
||||
public String toString()
|
||||
{
|
||||
String result="";
|
||||
|
||||
result = "I am Ghost #"+getGhostNum()+
|
||||
" located at (x,y): (" +getX()+", "+getY()+")";
|
||||
|
||||
return result;
|
||||
} //end of toString()
|
||||
|
||||
|
||||
/*
|
||||
// =======================================================================
|
||||
// Class specific methods local to this instance (ex: forced by interface)
|
||||
// =======================================================================
|
||||
*/
|
||||
|
||||
/**
|
||||
* runAway. Modifies this instance's runAwayFlag and runAwayCount.
|
||||
* [1.]: Sets runAwayFlag to true and
|
||||
* [2.]: Initializes runAwayCount to RUN_AWAY_DURATION from P6Constants
|
||||
*/
|
||||
public void runAway()
|
||||
{
|
||||
this.setRunAwayFlag(true);
|
||||
this.setRunAwayCount(P6Constants.RUN_AWAY_DURATION);
|
||||
}
|
||||
|
||||
/**
|
||||
* resetGhost. Modifies this instance's runAwayFlag and startX, startY
|
||||
* This method will be invoked if this ghost is eaten by Pacman.
|
||||
* [1.]: Turns runAwayFlag to false and
|
||||
* [2.]: Returns the ghost to its start coordinates.
|
||||
*/
|
||||
public void resetGhost()
|
||||
{
|
||||
this.setRunAwayFlag(false);
|
||||
setPosition( getStartX(), getStartY() );
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* getMove. Calculate the array of size four containing this ghost's choices
|
||||
of movement as described above. Feel free to use the GhostAI
|
||||
class. Keep in mind however that the getGhostMove method
|
||||
provided assumes the ghost is chasing pacman. You will need to
|
||||
modify the array to have the ghost run away from Pacman if this
|
||||
is the case. If the Ghost is indeed running from Pacman, be sure
|
||||
to deduct a turn from runAwayCount and check to see if
|
||||
runAwayCount is zero, in which case you should have the ghost no
|
||||
longer run from Pacman. This method will not actually set the
|
||||
location of the ghost based on the GhostAI method. It simply
|
||||
returns the array of choices. We do this because this ghost has
|
||||
no clue whether any of its choices will take it into a wall. A
|
||||
class later in the project will process the choices and decide
|
||||
which moves are legal for this ghost. The ghost will then be
|
||||
told which way to move.
|
||||
* @param pacmanX Pacman's x location.
|
||||
* @param pacmanY Pacman's y location.
|
||||
* @return int[] Ghost's choices of movement (as done in GhostAI)
|
||||
*/
|
||||
|
||||
public int[] getMove( int pacmanX, int pacmanY ) // Pacman's x, y
|
||||
{
|
||||
int result[];
|
||||
|
||||
if (getRunAwayFlag() == false) // ghost chasing pacman
|
||||
{
|
||||
result = GhostAI.getGhostMove(
|
||||
getX(), //ghost's X (from GamePiece)
|
||||
getY(), //ghost's Y (from GamePiece)
|
||||
pacmanX, //pacman's X (from parameter)
|
||||
pacmanY //pacman's Y (from parameter)
|
||||
);
|
||||
}
|
||||
|
||||
else // (getRunAwayFlag() == true) // ghost fleeing pacman
|
||||
{
|
||||
result = GhostAI.getGhostMove(
|
||||
getX(), //ghost's X (from GamePiece)
|
||||
getY(), //ghost's Y (from GamePiece)
|
||||
pacmanX, //pacman's X (from parameter)
|
||||
pacmanY //pacman's Y (from parameter)
|
||||
);
|
||||
int temp;
|
||||
int left= 0;
|
||||
int right= result.length-1;
|
||||
while (left < right) //reverse result array (flip across middle)
|
||||
{
|
||||
temp = result[left];
|
||||
result[left]=result[right];
|
||||
result[right]=temp;
|
||||
|
||||
left=left+1;
|
||||
right=right-1;
|
||||
} //endwhile
|
||||
|
||||
// Preferable to a switch statement because of the >0. (case 1,2,3...)
|
||||
if (getRunAwayCount() > 0) //for all integers I such that I>=1
|
||||
this.setRunAwayCount( getRunAwayCount()-1 );
|
||||
else if (getRunAwayCount()==0) //0 is min value & known at this point.
|
||||
this.setRunAwayFlag(false); //get Pacman!
|
||||
else // (getRunAwayCount()<0) //Error condition
|
||||
System.out.println("An error has occurred, getRunAwayCount<0");
|
||||
|
||||
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
// =======================================================================
|
||||
// Methods dealing directly with the data within this instance
|
||||
// (accessors, modifiers, etc)
|
||||
// =======================================================================
|
||||
*/
|
||||
|
||||
/**
|
||||
* Get the value of ghostNum.
|
||||
* @return value of ghostNum.
|
||||
*/
|
||||
public int getGhostNum()
|
||||
{
|
||||
return ghostNum;
|
||||
}
|
||||
|
||||
/**
|
||||
* STUB - MAKE PRIVATE
|
||||
* Set the value of ghostNum.
|
||||
* @param v Value to assign to ghostNum.
|
||||
*/
|
||||
private void setGhostNum(int v)
|
||||
{
|
||||
this.ghostNum = v;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the value of runAwayFlag.
|
||||
* @return value of runAwayFlag.
|
||||
*/
|
||||
public boolean getRunAwayFlag()
|
||||
{
|
||||
return runAwayFlag;
|
||||
}
|
||||
|
||||
/**
|
||||
* STUB - MAKE PRIVATE
|
||||
* Set the value of runAwayFlag.
|
||||
* @param v Value to assign to runAwayFlag.
|
||||
*/
|
||||
private void setRunAwayFlag(boolean v)
|
||||
{
|
||||
this.runAwayFlag = v;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* STUB - REVIEW MAKE PRIVATE
|
||||
* Get the value of runAwayCount.
|
||||
* @return value of runAwayCount.
|
||||
*/
|
||||
public int getRunAwayCount()
|
||||
{
|
||||
return runAwayCount;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* STUB - MAKE PRIVATE
|
||||
* Set the value of runAwayCount.
|
||||
* @param v Value to assign to runAwayCount.
|
||||
*/
|
||||
private void setRunAwayCount(int v)
|
||||
{
|
||||
this.runAwayCount = v;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Get the value of startX.
|
||||
* @return value of startX.
|
||||
*/
|
||||
public int getStartX()
|
||||
{
|
||||
return startX;
|
||||
}
|
||||
|
||||
/**
|
||||
* STUB - MAKE PRIVATE
|
||||
* Set the value of startX.
|
||||
* @param v Value to assign to startX.
|
||||
*/
|
||||
private void setStartX(int v)
|
||||
{
|
||||
this.startX = v;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Get the value of startY.
|
||||
* @return value of startY.
|
||||
*/
|
||||
public int getStartY()
|
||||
{
|
||||
return startY;
|
||||
}
|
||||
|
||||
/**
|
||||
* STUB - MAKE PRIVATE
|
||||
* Set the value of startY.
|
||||
* @param v Value to assign to startY.
|
||||
*/
|
||||
private void setStartY(int v)
|
||||
{
|
||||
this.startY = v;
|
||||
}
|
||||
|
||||
/*
|
||||
// =======================================================================
|
||||
// Protected Variables local to this instance
|
||||
// =======================================================================
|
||||
*/
|
||||
|
||||
|
||||
/*
|
||||
// =======================================================================
|
||||
// Private Variables local to this instance
|
||||
// =======================================================================
|
||||
*/
|
||||
|
||||
/**
|
||||
* ghostNum. The number of the ghost.
|
||||
*/
|
||||
private int ghostNum;
|
||||
|
||||
/**
|
||||
* runAwayFlag. Flag representing whether or not ghost is hunting Pacman.
|
||||
* true if this Ghost is running from Pacman, and
|
||||
* false if the Ghost is chasing Pacman.
|
||||
*/
|
||||
private boolean runAwayFlag;
|
||||
|
||||
/**
|
||||
* runAwayCount. Contains the number of returns remaining
|
||||
* for this ghost to run from Pacman.
|
||||
* (A ghost will run from Pacman for a fixed number of moves
|
||||
* if Pacman eats a special nibble).
|
||||
*/
|
||||
private int runAwayCount;
|
||||
|
||||
/**
|
||||
* startX. Contains the starting x coordinate for this ghost.
|
||||
*/
|
||||
private int startX;
|
||||
|
||||
/**
|
||||
* startY. Contains the starting y coordinate for this ghost.
|
||||
*/
|
||||
private int startY;
|
||||
|
||||
/*
|
||||
// =======================================================================
|
||||
// Main method (overrides this commenting system for readibility reasons)
|
||||
// =======================================================================
|
||||
*/
|
||||
|
||||
/**
|
||||
* Debugging main for class Ghost.
|
||||
* This method will rigorously test my code.
|
||||
*
|
||||
* <br><br>
|
||||
* @param args a String array of command line arguments.
|
||||
*/
|
||||
public static void main(String[] args)
|
||||
{
|
||||
|
||||
if ( (args.length==1) && (args[0].equals("-db") ) )
|
||||
DEBUG_MAIN=true; //else leave false: ignore input.
|
||||
|
||||
if (DEBUG_MAIN)
|
||||
{ // NOTE: This {} encapsulates most of main and is leftmost margin as a flag.
|
||||
// redundant ifs left in, in case the main if(DEBUG) statement was ommitted,
|
||||
// this would then print out neat output, as an example of real running.
|
||||
|
||||
Ghost g1 = new Ghost(4,6,1);
|
||||
Ghost g2 = new Ghost(7,3,2);
|
||||
Ghost g3 = new Ghost(8,5,3);
|
||||
Ghost g4 = new Ghost(4,4,4);
|
||||
|
||||
System.out.println(g1);
|
||||
System.out.println(g2);
|
||||
System.out.println(g3);
|
||||
System.out.println(g4);
|
||||
|
||||
System.out.println("Ghost's class is:" +g1.getClass().getName());
|
||||
|
||||
if (DEBUG) System.out.println("end main(String[] args) call:");
|
||||
} //end of DEBUG if
|
||||
|
||||
} //end of main(String[] args)
|
||||
|
||||
} //end of class Ghost
|
||||
Reference in New Issue
Block a user