412 lines
10 KiB
Java
412 lines
10 KiB
Java
import java.util.*;
|
|
import java.io.*;
|
|
|
|
/**
|
|
* CS1322: Programming Assignment #6 - Fall 2002
|
|
*
|
|
* <PRE>
|
|
* IDBox for: PacmanFileReader.java
|
|
*
|
|
* Revisions: 1.0 Nov. 23, 2002
|
|
* Created the PacmanFileReader 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
|
|
|
|
public class PacmanFileReader
|
|
{
|
|
/** 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 PacmanFileReader.
|
|
* @param void No parameters.
|
|
* @return PacmanFileReader instance/object.
|
|
*
|
|
public PacmanFileReader()
|
|
{
|
|
|
|
} //end of PacmanFileReader()
|
|
*/
|
|
|
|
/**
|
|
* Constructor 2. Specified default-overloading constructor for PacmanFileReader.
|
|
* @param file_name a String file name of a board file
|
|
* @return PacmanFileReader instance/object.
|
|
*/
|
|
public PacmanFileReader(String file_name)
|
|
{
|
|
ghosts = new Ghost[4]; //0->1, 1->2, 2->3, 3->4
|
|
this.FILE_NAME=file_name;
|
|
pieceArray=new GamePiece[15][15];
|
|
|
|
read_5_lines();
|
|
read_15_lines();
|
|
|
|
} //end of PacmanFileReader(String)
|
|
|
|
|
|
|
|
/*
|
|
// =======================================================================
|
|
// Methods which override class Object methods
|
|
// =======================================================================
|
|
*/
|
|
|
|
|
|
/**
|
|
* Equal instance comparison. Compares if two instances are equal.
|
|
* @param o object-type reference of (hopefully) an PacmanFileReader.
|
|
* @return boolean whether objects are equal.
|
|
*
|
|
public boolean equals (Object o)
|
|
{
|
|
|
|
} //end of equals(Object)
|
|
*/
|
|
|
|
/**
|
|
* toString instance printer. Prints properties of PacmanFileReader.
|
|
* @return System.out.println(PacmanFileReader instance) instance print.
|
|
*/
|
|
public String toString()
|
|
{
|
|
String result="";
|
|
|
|
result= result+pacman.toString()+"\n"; //gets toString of Pacman.
|
|
|
|
for (int i=0; i<ghosts.length; i=i+1)
|
|
result= result+ghosts[i].toString()+"\n"; //gets toString of ghosts.
|
|
|
|
for (int y=0; y<15; y=y+1)
|
|
{
|
|
for (int x=0; x<15; x=x+1)
|
|
result=result+pieceArray[x][y].toString();
|
|
|
|
result= result+"\n";
|
|
}
|
|
|
|
return result;
|
|
} //end of toString()
|
|
|
|
|
|
/*
|
|
// =======================================================================
|
|
// Class specific methods local to this instance (ex: forced by interface)
|
|
// =======================================================================
|
|
*/
|
|
|
|
|
|
/**
|
|
* read_5_lines. Reads the first 5 lines of the given file.
|
|
* Reads each of the first five lines of the board file,
|
|
* gets the x and y start coordinates of objects and
|
|
* instantiates Pacman or each of the four Ghosts
|
|
* (depending on input, being which line is read)
|
|
* @param void No parameters.
|
|
*/
|
|
public void read_5_lines()
|
|
{
|
|
StringTokenizer st;
|
|
String temp_token;
|
|
int startpos[];
|
|
int char_num;
|
|
String line;
|
|
Integer temp_Int;
|
|
|
|
try
|
|
{
|
|
br = new BufferedReader( new FileReader(FILE_NAME) );
|
|
startpos = new int[2];
|
|
char_num=0;
|
|
|
|
line = br.readLine();
|
|
for (int num_line=0;
|
|
( (num_line<5) && (line!=null) && (line.equals("")==false) );
|
|
num_line=num_line+1)
|
|
{
|
|
st = new StringTokenizer(line);
|
|
|
|
while ( st.hasMoreTokens() )
|
|
{
|
|
temp_token = st.nextToken();
|
|
// temp_Int = new Integer(temp_token);
|
|
startpos[char_num]=Integer.parseInt(temp_token);
|
|
|
|
char_num= char_num+1;
|
|
} //endwhile
|
|
char_num=0;
|
|
|
|
if (num_line == 0 )
|
|
pacman = new Pacman(startpos[0], startpos[1]);
|
|
else //if (num_line!=0)
|
|
ghosts[num_line-1] = new Ghost(startpos[0], startpos[1], num_line);
|
|
|
|
line = br.readLine();
|
|
} //endfor
|
|
br.close();
|
|
} //endtry
|
|
|
|
catch( IOException ioe)
|
|
{
|
|
System.out.println("Error making pacman and ghosts.");
|
|
} //endcatch
|
|
|
|
} //end read_5_lines(void)
|
|
|
|
|
|
/**
|
|
* read_15_lines. Reads the board.
|
|
* Instantiates each cell of pieceArray to the appropriate GamePiece subclass
|
|
* passing in the x and y index as the GamePiece coordinates.
|
|
* @param void No parameters.
|
|
*/
|
|
public void read_15_lines()
|
|
{
|
|
StringTokenizer st;
|
|
String temp_token;
|
|
char board_line[];
|
|
int char_num;
|
|
String line;
|
|
Integer temp_Int;
|
|
|
|
try
|
|
{
|
|
br = new BufferedReader( new FileReader(FILE_NAME) );
|
|
board_line = new char[15];
|
|
char_num=0;
|
|
|
|
//ignore first 5 lines
|
|
for (int i=0; i<5; i=i+1)
|
|
line = br.readLine();
|
|
|
|
// 5th line, the real stuff.
|
|
line = br.readLine();
|
|
for (int y_coord=0;
|
|
( (y_coord<15) && (line!=null) && (line.equals("")==false) );
|
|
y_coord=y_coord+1)
|
|
{
|
|
st = new StringTokenizer(line);
|
|
|
|
while ( st.hasMoreTokens() )
|
|
{
|
|
temp_token= st.nextToken();
|
|
// temp_Int = new Integer(temp_token);
|
|
board_line[char_num]= temp_token.charAt(0);
|
|
|
|
char_num= char_num+1;
|
|
} //endwhile
|
|
char_num=0;
|
|
|
|
for (int x_coord=0; x_coord<15; x_coord=x_coord+1)
|
|
{
|
|
if (P6Constants.WALL.charAt(0) == board_line[x_coord])
|
|
{
|
|
pieceArray[x_coord][y_coord]=
|
|
new WallPiece(x_coord, y_coord);
|
|
}
|
|
else if (P6Constants.FLOOR.charAt(0) == board_line[x_coord])
|
|
{
|
|
pieceArray[x_coord][y_coord]=
|
|
new FloorPiece(x_coord, y_coord, false, false);
|
|
}
|
|
else if (P6Constants.NIBBLE.charAt(0) == board_line[x_coord])
|
|
{
|
|
pieceArray[x_coord][y_coord]=
|
|
new FloorPiece(x_coord, y_coord, true, false);
|
|
}
|
|
else if (P6Constants.SPECIAL.charAt(0) == board_line[x_coord])
|
|
{
|
|
pieceArray[x_coord][y_coord]=
|
|
new FloorPiece(x_coord, y_coord, false, true);
|
|
}
|
|
else // error
|
|
System.out.println("Error char in file.");
|
|
|
|
//STUB - use references and dynamic binding.
|
|
|
|
|
|
/*
|
|
switch (board_line[itr_bl])
|
|
{
|
|
case P6Constants.WALL.charAt(0)://'w'
|
|
break;
|
|
case P6Constants.FLOOR.charAt(0)://'f'
|
|
break;
|
|
case P6Constants.NIBBLE.charAt(0)://'n'
|
|
break;
|
|
case P6Constants.SPECIAL.charAt(0)://'s'
|
|
break;
|
|
}
|
|
*/
|
|
} //endfor
|
|
|
|
line = br.readLine();
|
|
} //endfor
|
|
br.close();
|
|
} //endtry
|
|
|
|
catch( IOException ioe)
|
|
{
|
|
System.out.println("Error making the board to play on.");
|
|
} //endcatch
|
|
|
|
} //end read_15_lines(void)
|
|
|
|
/*
|
|
// =======================================================================
|
|
// Methods dealing directly with the data within this instance
|
|
// (accessors, modifiers, etc)
|
|
// =======================================================================
|
|
*/
|
|
|
|
|
|
/**
|
|
* Get the value of pacman.
|
|
* @return value of pacman.
|
|
*/
|
|
public Pacman getPacman()
|
|
{
|
|
return pacman;
|
|
}
|
|
|
|
/**
|
|
* Set the value of pacman.
|
|
* @param v Value to assign to pacman.
|
|
*
|
|
public void setPacman(Pacman v)
|
|
{
|
|
this.pacman = v;
|
|
}
|
|
NOT IMPLEMENTED
|
|
*/
|
|
|
|
/**
|
|
* Get the value of ghosts.
|
|
* @return value of ghosts.
|
|
*/
|
|
public Ghost[] getGhosts()
|
|
{
|
|
return ghosts;
|
|
}
|
|
|
|
/**
|
|
* Set the value of ghosts.
|
|
* @param v Value to assign to ghosts.
|
|
*/
|
|
public void setGhosts(Ghost[] v)
|
|
{
|
|
this.ghosts = v;
|
|
}
|
|
|
|
|
|
/**
|
|
* Get the value of pieceArray.
|
|
* @return value of pieceArray.
|
|
*/
|
|
public GamePiece[][] getPieceArray()
|
|
{
|
|
return pieceArray;
|
|
}
|
|
|
|
/**
|
|
* Set the value of pieceArray.
|
|
* @param v Value to assign to pieceArray.
|
|
*/
|
|
public void setPieceArray(GamePiece[][] v)
|
|
{
|
|
this.pieceArray = v;
|
|
}
|
|
|
|
|
|
/*
|
|
// =======================================================================
|
|
// Protected Variables local to this instance
|
|
// =======================================================================
|
|
*/
|
|
|
|
|
|
/*
|
|
// =======================================================================
|
|
// Private Variables local to this instance
|
|
// =======================================================================
|
|
*/
|
|
|
|
/**
|
|
* pieceArray. A 15x15 array of GamePieces.
|
|
* This array will model the board seen in the GUI.
|
|
*/
|
|
private GamePiece[][] pieceArray;
|
|
|
|
/**
|
|
* br. To read the board file.
|
|
*/
|
|
private BufferedReader br;
|
|
|
|
/**
|
|
* instance variables for pacman and four ghosts
|
|
*/
|
|
private Pacman pacman;
|
|
private Ghost[] ghosts;
|
|
|
|
|
|
// Perhaps to use in the constructor.
|
|
private String FILE_NAME;
|
|
|
|
|
|
/*
|
|
// =======================================================================
|
|
// Main method (overrides this commenting system for readibility reasons)
|
|
// =======================================================================
|
|
*/
|
|
|
|
/**
|
|
* Debugging main for class PacmanFileReader.
|
|
* 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.
|
|
|
|
PacmanFileReader pfr1 = new PacmanFileReader("board_1.txt");
|
|
PacmanFileReader pfr2 = new PacmanFileReader("board_2.txt");
|
|
|
|
System.out.println(pfr1);
|
|
System.out.println(pfr2);
|
|
|
|
if (DEBUG) System.out.println("end main(String[] args) call:");
|
|
} //end of DEBUG if
|
|
|
|
} //end of main(String[] args)
|
|
|
|
} //end of class PacmanFileReader
|