Results 1 to 5 of 5
  1. #1
    rnavarro9 is offline Member
    Join Date
    Nov 2007
    Posts
    7
    Rep Power
    0

    Default maze problem (file to a 2d array)

    I have a 5x5.txt file, which includes the map (layout) of my maze. I've read a couple of posts on here which refer to the BufferedReader method.


    5x5.txt

    5 5
    4 4 3 3

    14 12 5 4 6
    10 9 4 3 10
    9 5 2 13 2
    14 14 10 12 2
    9 1 3 12 11

    I'm not sure how to load the first line of this .txt file, call setUp(), load the rest of the file.

    I've created an old FileReaderClass which read files from a .txt, but was never confident about storing lines into an array....so far I have this. Can you plz help?

    import java.awt.*;
    import java.awt.event.*;
    import java.io.BufferedReader;
    import java.io.FileNotFoundException;
    import java.io.FileReader;
    import java.io.IOException;
    import java.util.ArrayList;
    import java.util.StringTokenizer;
    import javax.swing.JButton;
    import javax.swing.JPanel;

    /**
    * This class stores the maze and provides functions construct a maze given a
    * file (through Mazepanel(String filename)) as well as solving the maze.
    *
    * The maze is physically stored in MazeCell[][] cells, and the MazePanel is
    * also aware of the indices into cells at which the start and finish can be
    * found (as well as the dimensions of the 2-D array.
    */
    public class MazePanel extends JPanel {

    private JButton startButton;

    public static final int buttonSizeX = 100;

    public static final int buttonSizeY = 40;

    // This member is in charge of storing the graphical representation
    // of the maze as a whole, as well as the individual MazeCells which
    // are to be drawn
    private JPanel grid;

    // This 2-D array of MazeCell is instantiated in the setUp() function,
    // and initialized in the MazePanel(String filename) function
    private MazeCell[][] cells;

    public static int width; // The dimensions of the cells array

    public static int height; // (in number of cells)

    // Boolean reprsenting whether or not a new thread should be kicked off
    // (don't worry about this data for your assignment)
    private boolean running = true;

    private int startXIdx; // pair of indices which represents where the

    private int startYIdx; // start location can be found

    private int finishXIdx; // same with the finish location

    private int finishYIdx;

    // Leave the line below in your code. You won't need to know what it is for
    // this course.
    private static final long serialVersionUID = 1L;

    /**
    * This constructor takes a filename where a maze description can be found,
    * then creates the maze objects that the file corresponds to.
    *
    * @param filename
    * The filename of the maze description
    * @throws FileNotFoundException
    * @throws IOException
    * @throws MazeFileFormatException
    */

    /*This Section of code will load the first line of the file, set the height and width; and load the rest of the file
    * using it as the "map" for the maze. Values will be split using the split method and stored in arrays.
    */
    public MazePanel(String filename) throws FileNotFoundException,
    IOException, MazeFileFormatException {

    BufferedReader br = new BufferedReader(new FileReader(filename));
    int[][] mazeLine = new int[0][];
    // Take first line and split it

    String line = br.readLine();
    line = br.readLine();
    String[] tokens = line.split("\\s");

    //Add first line into array

    int firstLine = mazeLine.length;






    }

    /**
    * This function creates all the swing components that draw the
    * grid of cells. It also instantiates the cells themselves.
    *
    * Precondition: constructor has set up dimensions of the grid
    */
    private void setUp() {
    startButton = new JButton("Start");
    grid = new JPanel();
    cells = new MazeCell[height][width];
    running = true; // controls loop in run method

    setBackground(new Color(255, 255, 200));
    setLayout(null);
    setSize(width * MazeCell.CELL_W, 50 + height * MazeCell.CELL_H);
    setLocation(0, 0);

    // start button
    startButton.setSize(buttonSizeX, buttonSizeY);
    startButton.setLocation(10, 10);
    startButton.setVisible(true);
    startButton.setFont(new Font("Ariel", Font.BOLD, 24));
    startButton.addActionListener(new StartListener());

    // grid
    grid.setSize(width * MazeCell.CELL_W, height * MazeCell.CELL_H);
    grid.setLocation(10, 70);
    grid.setVisible(true);
    grid.setLayout(new GridLayout(height, width));

    for (int i = 0; i < height; i++)
    for (int j = 0; j < width; j++) {
    cells[i][j] = new MazeCell();
    grid.add(cells[i][j]);
    }

    add(startButton);
    add(grid);
    }


    public void paintComponent(Graphics g) {
    super.paintComponent(g);
    }

    /**
    * This function is responsible for making each cell aware of its neighbors.
    * We do this by examining type information on each of the cells, as well as
    * bounds checking on the address of the neighbor.
    */
    private void addNeighbors() {
    // Write this function
    }

    /**
    * This function is responsible for "solving" the maze. Solving
    * the maze represents finding a path from the start MazeCell to the
    * finish MazeCell. We do this using recursion, and at each step, we
    * call setVisited(true) and setUsed(true) so that the draw code can
    * represent our partial solution. If no path can be found, solve should
    * return false.
    * @param start The cell from whence the recursive search originates
    * @return
    */
    public boolean solve(MazeCell start) {

    //your recursive solution goes here!
    return true;
    }

    private void clearMaze() {
    for (int i = 0; i < height; ++i)
    for (int j = 0; j < width; ++j) {
    cells[i][j].setVisited(false);
    }
    }

    /**
    * Inner class responsible for responding to clicks of the start button.
    */
    public class StartListener implements ActionListener, Runnable {
    public void actionPerformed(ActionEvent arg0) {
    clearMaze();
    running = true;
    Thread runSearch = new Thread(this);
    runSearch.start();
    }

    public void run() {
    while (running) {
    try {
    Thread.sleep(50);
    } catch (Exception e) {
    }
    solve(cells[startYIdx][startXIdx]);
    running = false;
    }
    }
    }
    }

  2. #2
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    Java Code:
        public MP(String filename) throws FileNotFoundException,
                                          IOException {//, MazeFileFormatException {
            BufferedReader br = new BufferedReader(new FileReader(filename));
            int[][] mazeLine = new int[0][];
            // Take first line and split it
            String line = br.readLine();
            String[] tokens = line.split("\\s");
            width  = Integer.parseInt(tokens[0]);
            height = Integer.parseInt(tokens[1]);
            setUp();
            // line 2: what is it for?  // 4 4 3 3
            // read but don't know what to do with it
            line = br.readLine();
            // read the "blank" line
            line = br.readLine();
            // use mazeLine to read in the grid data from the file
            mazeLine = new int[height][width];
            int row = 0;
            while((line = br.readLine()) != null) {
                tokens = line.split("\\s");
                for(int j = 0; j < tokens.length; j++) {
                    mazeLine[row][j] = Integer.parseInt(tokens[j]);
                }
                row++;
            }
            br.close();
            //System.out.printf("mazeLine = %s%n", Arrays.deepToString(mazeLine));
            //Add first line into array
    //        int firstLine = mazeLine.length;
        }

  3. #3
    rnavarro9 is offline Member
    Join Date
    Nov 2007
    Posts
    7
    Rep Power
    0

    Default

    // line 2: what is it for? // 4 4 3 3
    // read but don't know what to do with it


    Thanks a lot by the way!

    This would be the general layout of the maze:

    x y
    sx sy ex ey

    a b c d e
    f g h i j
    ...
    o p q r s

    =============
    Where ex and ey are the end locations & sx and sy are the starting positions in the maze.

    NOW:

    By all means, I'm not in this website to have someone just do my homework for me. I really want to understand whats going on.


    Why do you :
    (I know that this takes the lines in the file and splits them up in to tokens)
    (But, why do you do it twice? Once in the beginning of the code; and the second in the while loop? )

    //first instance
    String[] tokens = line.split("\\s");

    //second instance
    tokens = line.split("\\s");

    Thanks!

  4. #4
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    why do you do it twice? Once in the beginning of the code; and the second in the while loop?
    Consider the text file we are reading:
    Java Code:
    5 5
    4 4 3 3
    
    14 12 5 4 6
    10 9 4 3 10
    9 5 2 13 2
    14 14 10 12 2
    9 1 3 12 11
    The first br.readLine reads the first line which has two tokens which we split and assigned to the variables width and height.
    I skipped the next two lines with readLine statements.
    The loop is going to read the last five lines in the file. Reading the first line returns the string "14 12 5 4 6". We need to split this to get the 5 values to parse into ints and then to assign to the next line in the mazeLine array. Each trip through the loop reads another line of tokens.

  5. #5
    rnavarro9 is offline Member
    Join Date
    Nov 2007
    Posts
    7
    Rep Power
    0

    Cool thanks!

    I got it. I also made some modifications to the code you sent. I had to .parseInt
    for the second line which you didn't know what it was. The code is working pretty well. Except I'm having trouble adding the array into the Maze. I'll keep working on it, let me know if you care to know what I come up with!

Similar Threads

  1. Java Maze Problem
    By mousey182 in forum New To Java
    Replies: 2
    Last Post: 03-28-2008, 06:29 PM
  2. Data array to file
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 01-16-2008, 10:42 AM
  3. Maze Help
    By Soda in forum Advanced Java
    Replies: 1
    Last Post: 12-22-2007, 03:26 AM
  4. array problem
    By wats in forum New To Java
    Replies: 1
    Last Post: 12-12-2007, 07:08 AM
  5. array problem
    By Albert in forum Advanced Java
    Replies: 2
    Last Post: 07-01-2007, 01:13 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
  •