Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 11-08-2007, 11:18 PM
Member
 
Join Date: Nov 2007
Posts: 4
rnavarro9 is on a distinguished road
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;
}
}
}
}
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 11-09-2007, 04:08 AM
Senior Member
 
Join Date: Jul 2007
Posts: 1,145
hardwired is on a distinguished road
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; }
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 11-09-2007, 04:40 AM
Member
 
Join Date: Nov 2007
Posts: 4
rnavarro9 is on a distinguished road
// 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!
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 11-09-2007, 06:47 AM
Senior Member
 
Join Date: Jul 2007
Posts: 1,145
hardwired is on a distinguished road
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:
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.
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 11-09-2007, 08:36 AM
Member
 
Join Date: Nov 2007
Posts: 4
rnavarro9 is on a distinguished road
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!
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Java Maze Problem mousey182 New To Java 2 03-28-2008 07:29 PM
Data array to file Java Tip Java Tips 0 01-16-2008 11:42 AM
Maze Help Soda Advanced Java 1 12-22-2007 04:26 AM
array problem wats New To Java 1 12-12-2007 08:08 AM
array problem Albert Advanced Java 2 07-01-2007 02:13 AM


All times are GMT +3. The time now is 09:40 AM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org