Results 1 to 5 of 5
- 11-08-2007, 10:18 PM #1
Member
- Join Date
- Nov 2007
- Posts
- 7
- Rep Power
- 0
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;
}
}
}
}
- 11-09-2007, 03:08 AM #2
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; }
- 11-09-2007, 03:40 AM #3
Member
- Join Date
- Nov 2007
- Posts
- 7
- Rep Power
- 0
// 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!
- 11-09-2007, 05:47 AM #4
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:
The first br.readLine reads the first line which has two tokens which we split and assigned to the variables width and height.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
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.
- 11-09-2007, 07:36 AM #5
Member
- Join Date
- Nov 2007
- Posts
- 7
- Rep Power
- 0
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
-
Java Maze Problem
By mousey182 in forum New To JavaReplies: 2Last Post: 03-28-2008, 06:29 PM -
Data array to file
By Java Tip in forum Java TipReplies: 0Last Post: 01-16-2008, 10:42 AM -
Maze Help
By Soda in forum Advanced JavaReplies: 1Last Post: 12-22-2007, 03:26 AM -
array problem
By wats in forum New To JavaReplies: 1Last Post: 12-12-2007, 07:08 AM -
array problem
By Albert in forum Advanced JavaReplies: 2Last Post: 07-01-2007, 01:13 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks