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;
}
}
}
}