248 lines
5.3 KiB
Java
248 lines
5.3 KiB
Java
/**
|
|
* <PRE>
|
|
* CFileReader.java
|
|
*
|
|
* Revisions: 1.0 Nov. 24, 2002
|
|
* Created the CFileReader class
|
|
*
|
|
* </PRE>
|
|
*
|
|
* @author <A HREF="mailto:gtg184g@mail.gatech.edu">Jose Manuel Caban</A>
|
|
* @version Version 1.0, Nov. 24, 2002
|
|
*/
|
|
|
|
import java.io.*;
|
|
import java.util.*;
|
|
|
|
public class CFileReader implements Constants{
|
|
|
|
BufferedReader brReader;
|
|
|
|
/**
|
|
*@return an array with the first 4 slots holding pacman and the ghost
|
|
*start locations, slot 5 holding the Background grid, slot 6 holding the
|
|
*Node Grid
|
|
*/
|
|
public Object[] readMap(String mapFile){
|
|
Object[] array = new Object[6];
|
|
StringTokenizer st;
|
|
|
|
/////////////////////
|
|
//Read Start Points//
|
|
/////////////////////
|
|
|
|
try{
|
|
brReader = new BufferedReader(new FileReader(mapFile));
|
|
}catch(FileNotFoundException fnfe){
|
|
System.err.println("File Not Found Error - CFileReader");
|
|
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);
|
|
}
|
|
|
|
array[i] = new CCoord(Integer.parseInt(st.nextToken()),
|
|
Integer.parseInt(st.nextToken()),1);
|
|
}
|
|
/*End Read Start Points*/
|
|
/////////////////////////
|
|
|
|
String strTemp = "";
|
|
CTile[][] cBgGrid;
|
|
|
|
try{
|
|
strTemp = brReader.readLine();
|
|
}catch(IOException ioe){
|
|
System.err.println(ioe);
|
|
System.exit(0);
|
|
}
|
|
|
|
////////////////////////////////////////////////////
|
|
/*Check whether its standard file or enhanced grid*/
|
|
|
|
switch(strTemp.charAt(0)){
|
|
case 'w': case 'n': case 's': case 'f':
|
|
cBgGrid = new CTile[15][15];
|
|
break;
|
|
|
|
case '1':case '2':case '3':case '4': case '5': case '6': case '7':
|
|
case '8':case '9':case '0':
|
|
|
|
st = new StringTokenizer(strTemp);
|
|
cBgGrid = new CTile[Integer.parseInt(st.nextToken())]
|
|
[Integer.parseInt(st.nextToken())];
|
|
|
|
try{
|
|
strTemp = brReader.readLine();
|
|
}catch(IOException ioe){
|
|
System.err.println(ioe);
|
|
System.exit(0);
|
|
}
|
|
|
|
break;
|
|
default:
|
|
cBgGrid = new CTile[0][0];
|
|
break;
|
|
}
|
|
|
|
/*End Check file type*/
|
|
///////////////////////
|
|
|
|
///////////////
|
|
/*Read in map*/
|
|
st = new StringTokenizer("");
|
|
|
|
for(int i=0; i<cBgGrid.length; i++){
|
|
|
|
try{
|
|
if(i==0){
|
|
st = new StringTokenizer(strTemp);
|
|
}else{
|
|
st = new StringTokenizer(brReader.readLine());
|
|
}
|
|
}catch(IOException ioe){
|
|
System.err.println(ioe);
|
|
System.exit(0);
|
|
}
|
|
|
|
for(int z=0; z<cBgGrid[i].length; z++){
|
|
|
|
switch(st.nextToken().charAt(0))
|
|
{
|
|
case 'f':
|
|
cBgGrid[i][z] = new CTile(i,z,1);
|
|
break;
|
|
case 'n':
|
|
cBgGrid[i][z] = new CTile(i,z,1);
|
|
cBgGrid[i][z].setItems(true,false);
|
|
break;
|
|
case 's':
|
|
cBgGrid[i][z] = new CTile(i,z,1);
|
|
cBgGrid[i][z].setItems(false,true);
|
|
break;
|
|
default:
|
|
case 'w':
|
|
cBgGrid[i][z] = new CTile(i,z,2);
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
/*End map read*/
|
|
////////////////
|
|
array[5] = cBgGrid;
|
|
|
|
//////////////
|
|
//Init Nodes//
|
|
//////////////
|
|
|
|
CNode[][] cNode= new CNode[cBgGrid.length][cBgGrid[0].length];
|
|
|
|
for(int i=0; i<cNode.length; i++){
|
|
for(int z=0; z<cNode[i].length; z++){
|
|
boolean up=false, down=false, left=false, right=false;
|
|
|
|
//check up
|
|
try{
|
|
up = cBgGrid[i-1][z].getZ()==1?true:false;
|
|
}
|
|
catch(IndexOutOfBoundsException iobe){
|
|
up = true;
|
|
}
|
|
catch(Exception e){
|
|
up = false;
|
|
}
|
|
|
|
//check down
|
|
try{
|
|
down = cBgGrid[i+1][z].getZ()==1?true:false;
|
|
}
|
|
catch(IndexOutOfBoundsException iobe){
|
|
down = true;
|
|
}
|
|
catch(Exception e){
|
|
down = false;
|
|
}
|
|
|
|
//check left
|
|
try{
|
|
left = cBgGrid[i][z-1].getZ()==1?true:false;
|
|
}
|
|
catch(IndexOutOfBoundsException iobe){
|
|
left = true;
|
|
}
|
|
catch(Exception e){
|
|
left = false;
|
|
}
|
|
|
|
//check right
|
|
try{
|
|
right = cBgGrid[i][z+1].getZ()==1?true:false;
|
|
}
|
|
catch(IndexOutOfBoundsException iobe){
|
|
right = true;
|
|
}
|
|
catch(Exception e){
|
|
right = false;
|
|
}
|
|
|
|
if(cBgGrid[i][z] != null && cBgGrid[i][z].getZ()==1){
|
|
cNode[i][z] = new CNode(up,down,left,right,i,z,1);
|
|
}
|
|
}
|
|
}
|
|
|
|
array[6] = cNode;
|
|
try{
|
|
brReader.close();
|
|
}catch(IOException ioe){
|
|
System.err.println(ioe);
|
|
System.exit(0);
|
|
}
|
|
|
|
return array;
|
|
}
|
|
|
|
public static void main(String args[]){
|
|
CFileReader bob = new CFileReader();
|
|
Object[] steve = bob.readMap("board_1.txt");
|
|
for(int i=0; i<5; i++){
|
|
System.out.println(steve[i]);
|
|
}
|
|
|
|
System.out.println();
|
|
|
|
CTile[][] crystal = (CTile[][])steve[5];
|
|
for(int i=0; i<crystal.length; i++){
|
|
for(int z=0; z<crystal.length; z++){
|
|
System.out.println(crystal[i][z]);
|
|
}
|
|
}
|
|
|
|
CNode[][] julia = (CNode[][])steve[6];
|
|
for(int i=0; i<julia.length; i++){
|
|
for(int z=0; z<julia.length; z++){
|
|
try{
|
|
if(julia[i][z] == null){}
|
|
else{
|
|
System.out.println(julia[i][z]);}
|
|
}
|
|
catch(Exception e){
|
|
//System.out.print("null");
|
|
}
|
|
}
|
|
System.out.println("/*NextLine*/");
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}// end of class CFileReader
|