Files
GTSchoolShit/CS1322/p6/PacmanFileReader.java
2025-06-07 01:59:34 -04:00

219 lines
4.7 KiB
Java

/**
* <PRE>
* PacmanFileReader.java
*
* Revisions: 1.0 Nov. 24, 2002
* Created the PacmanFileReader class
* 1.1 Nov. 25, 2002
* Compiled, Finished, Done
*
* </PRE>
*
* Collaboration statement:
* I worked on this assignment with Roland Krystian Alberciak
*
* @author
*<A HREF="mailto:gtg142g@mail.gatech.edu">Roland Krystian Alberciak</A>
*@author <A HREF="mailto:gtg184g@mail.gatech.edu">Jose Manuel Caban</A>
* @version Version 1.1, Nov. 25, 2002
*/
import java.util.*;
import java.io.*;
public class PacmanFileReader {
private GamePiece[][] pieceArray;
private BufferedReader brReader;
/**
*Hold Pacman
*/
private Pacman cPacman;
/**
*Hold Ghosts
*/
private Ghost cGhost1;
private Ghost cGhost2;
private Ghost cGhost3;
private Ghost cGhost4;
/**
*Constructor
*@param inFile, the name of the file to read
*/
public PacmanFileReader(String inFile){
readMap(inFile);
}
/**
*Take in the file and create a grid
*@param mapFile, the file to be read
*/
private void readMap(String mapFile){
StringTokenizer st;
/////////////////////
//Read Start Points//
/////////////////////
try{
brReader = new BufferedReader(new FileReader(mapFile));
}catch(FileNotFoundException fnfe){
System.err.println("File Not Found Error - FileReader");
System.exit(0);
}
for(int i=0; i<5; i++){
st = new StringTokenizer("b");
try{
st = new StringTokenizer(brReader.readLine());
}catch(IOException ioe){
System.err.println(ioe);
System.exit(0);
}
switch(i){
case 0:
cPacman = new Pacman(Integer.parseInt(st.nextToken()),
Integer.parseInt(st.nextToken()));
break;
case 1:
cGhost1 = new Ghost(Integer.parseInt(st.nextToken()),
Integer.parseInt(st.nextToken()),1);
break;
case 2:
cGhost2 = new Ghost(Integer.parseInt(st.nextToken()),
Integer.parseInt(st.nextToken()),2);
break;
case 3:
cGhost3 = new Ghost(Integer.parseInt(st.nextToken()),
Integer.parseInt(st.nextToken()),3);
break;
case 4:
cGhost4 = new Ghost(Integer.parseInt(st.nextToken()),
Integer.parseInt(st.nextToken()),4);
break;
}
}
/*End Read Start Points*/
/////////////////////////
String strTemp = "";
pieceArray = new GamePiece[15][15];
//////////////
/*Read in map*/
st = new StringTokenizer("");
for(int i=0; i<pieceArray.length; i++){
try{
st = new StringTokenizer(brReader.readLine());
}catch(IOException ioe){
System.err.println(ioe);
System.exit(0);
}
for(int z=0; z<pieceArray[i].length; z++){
switch(st.nextToken().charAt(0))
{
case 'f':
pieceArray[z][i] = new FloorPiece(z,i,false,false);
break;
case 'n':
pieceArray[z][i] = new FloorPiece(z,i,true,false);
break;
case 's':
pieceArray[z][i] = new FloorPiece(z,i,false,true);
break;
default:
pieceArray[z][i] = new WallPiece(z,i);
break;
case 'w':
pieceArray[z][i] = new WallPiece(z,i);
break;
}
}
}
try{brReader.close();}catch(IOException ioe){}
}//end readMap()
/**
*@return pieceArray
*/
public GamePiece[][] getPieceArray(){
return pieceArray;
}
/**
*@return, pacman
*/
public Pacman getPacman(){
return cPacman;
}
/**
*@return ghost1
*/
public Ghost getGhost1(){
return cGhost1;
}
/**
*@return ghost2
*/
public Ghost getGhost2(){
return cGhost2;
}
/**
*@return ghost3
*/
public Ghost getGhost3(){
return cGhost3;
}
/**
*@return ghost4
*/
public Ghost getGhost4(){
return cGhost4;
}
/**
*@return ghosts[]
*/
public Ghost[] getGhosts(){
return (new Ghost[] {cGhost1,cGhost2,cGhost3,cGhost4});
}
/**
* 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) {
PacmanFileReader pmfr = new PacmanFileReader("board_1.txt");
for(int i=0; i<15; i++){
for(int z=0; z<15; z++){
System.out.println(pmfr.pieceArray[i][z]);
}
System.out.println("/*Next line*/");
}
}// end of main(String[] args)
}// end of class PacmanFileReader