Results 1 to 2 of 2

Thread: Maze

  1. #1
    soriano's Avatar
    soriano is offline Member
    Join Date
    Dec 2008
    Posts
    1
    Rep Power
    0

    Wink 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.
    Last edited by soriano; 12-15-2008 at 01:28 PM.

  2. #2
    xcallmejudasx's Avatar
    xcallmejudasx is offline Senior Member
    Join Date
    Oct 2008
    Location
    Houston, TX & Flint, MI
    Posts
    609
    Rep Power
    6

    Default

    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)

Similar Threads

  1. Maze reader
    By bix in forum New To Java
    Replies: 1
    Last Post: 11-20-2008, 06:06 PM
  2. 3D Maze
    By EternalSolitude in forum New To Java
    Replies: 5
    Last Post: 11-14-2008, 12:51 AM
  3. Help me with create a 2D Maze in Java
    By hnam31190 in forum New To Java
    Replies: 4
    Last Post: 11-02-2008, 01:37 AM
  4. Java Maze Problem
    By mousey182 in forum New To Java
    Replies: 2
    Last Post: 03-28-2008, 06:29 PM
  5. Maze Help
    By Soda in forum Advanced Java
    Replies: 1
    Last Post: 12-22-2007, 03:26 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •