-
Maze
Hi to everyone who could read my post.
Advance thank you for your kind response if any...
I am a computer Science student and fun of using java..
Then there it goes our professor want us to create a maze with two dimensional array..
My problem as of now is how to store the text file value on my array?
I need to store those value in text file...
Could anyone help me...
And lastly could any one knows how to create a maze program.
With start and Goal...
Actually it is a maze traverse project...
--------------------------------------------------------------------------------------------
this is my code... all i want as of now is to store the text file value to an array of two dimension. the txt file value is the char[] [] maze. i stored it in a text file and want to pass the text file content into array because my succeeding code are referring to that array char [][] maze.
--------------------------------------------------------------------------------------------
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.awt.Point;
import java.util.*;
import javax.swing.*;
public class FirstMazeJava extends JFrame {
private ArrayList pA=new ArrayList();
String tempStr = null;
char[][] maze= {{'*', '*', '.', '*', '*'},
{'*', '*', '.', '*', '*'},
{'*', '*', '.', '.', '*'},
{'*', '*', '*', '.', '*'},
{'*', '*', '*', '.', '*'}};;
public void setMaze(char[][] maze) {
this.maze=maze;
}
public char[][] getMaze() {
return this.maze;
}
public void addPoint(Point p) {
pA.add(p);
}
public ArrayList retrievePoint() {
return pA;
}
public Point getStartPoint() {
int maxRow = maze.length;
int maxCol=maze[0].length;
for (int r=0;r<maxRow;r++) {
if (maze[r][maxCol-1]=='.') {
return new Point(r,maxCol-1);
}
}
for (int c=0;c<maxCol;c++) {
if (maze[maxRow-1][c]=='.') {
return new Point(maxRow-1,c);
}
}
return new Point(0,0);
}
public Point getEndPoint() {
int maxRow = maze.length;
int maxCol=maze[0].length;
for (int c=0;c<maxCol;c++) {
if (maze[0][c]=='.') {
return new Point(0,c);
}
}
for (int r=0;r<maxRow;r++) {
if (maze[r][0]=='.') {
return new Point(r,0);
}
}
return new Point(0,0);
}
String displayPoint(Point p) {
return "("+p.x+","+p.y+")";
}
public boolean findEndPoint(Point sp, Point ep, Point pp) {
int maxRow = maze.length;
int maxCol=maze[0].length;
if (sp.x>maxRow-1) return false;
if (sp.y>maxCol-1) return false;
if(maze[sp.x][sp.y]=='*') return false;
if (sp.equals(ep)) {
System.out.println("End Point : ("+ep.x+","+ep.y+")");
return true;
}
Point left = new Point(sp.x, sp.y-1);
Point right = new Point(sp.x, sp.y+1);
Point down = new Point(sp.x+1, sp.y);
Point up = new Point(sp.x-1, sp.y);
boolean b = false;
if(!left.equals(pp)) b = b || findEndPoint(left, ep, sp);
if(!right.equals(pp)) b = b || findEndPoint(right, ep, sp);
if(!down.equals(pp)) b = b || findEndPoint(down, ep, sp);
if(!up.equals(pp)) b = b || findEndPoint(up, ep, sp);
if (b) {
addPoint(sp);
System.out.println("Path: (" + sp.x + "," + sp.y + ")");
}
return b;
}
public Point[] findEndPoint() {
System.out.println("\nStart Point : ("+this.getStartPoint().x+","+this.getStartPoint() .y+")");
findEndPoint(this.getStartPoint(),this.getEndPoint (),this.getStartPoint());
return (Point[])retrievePoint().toArray(new Point[0]);
}
public void animateMazeMovement(Point[] pA) {
//animate maze movement
}
//main executable body
public static void main(String[] s) {
try {
FileReader file = new FileReader("E:\\regie.txt");
BufferedReader buff = new BufferedReader(file);
boolean eof = false;
while (!eof) {
String line = buff.readLine();
if (line == null)
eof = true;
else
System.out.println(line);
}
buff.close();
}catch (IOException e) {
System.out.println("Error -- " +e.toString());
FirstMazeJava fmj = new FirstMazeJava();
System.out.println("Maze:\n\n" + fmj.getMaze());
Point[] pL = fmj.findEndPoint();
System.out.println(Arrays.asList(pL));
}
}
}
sorry for the structure 'cause i'm a bit confused if we could finish it by wednesday...
hope that i could get your response as early as tommorrow.
-
for storing/reading to/from a text file you need to look at BufferedReader, BufferedWriter, FileWriter, FileReader, and possibly Delimeter(found within the String api..I think)