我的目标是用一 st子verse笑一个马奇,但我无法走得很远。
我有2D系列的<代码>Room物体,我总是从第1位开始。 我认为,我所建立的一切都是正确的。 然而,每当我试图查阅我阵列中储存的数据时,我就不断收到<条码>。
任何将给我指明正确方向的援助都会受到赞赏。
我的房间:
import java.awt.Point;
public class Room {
private Room up;
private Room down;
private Room left;
private Room right;
private char value;
private boolean blocked;
private boolean visited = false;
private Point p;
public void setCord(int row, int column) {
p = new Point(row, column);
}
public void setUp(Room [][] r, int row, int column) {
up = r[row][column];
}
public void setDown(Room[][] r, int row, int column) {
down = r[row][column];
}
public void setRight(Room[][] r, int row, int column) {
right = r[row][column];
}
public void setLeft(Room[][] r, int row, int column) {
left = r[row][column];
}
public void setValue(char c) {
value = c;
}
public void setVisited(boolean b) {
visited = b;
}
public void setBlocked(boolean b) {
blocked = b;
}
public Point getCord() {
return p;
}
public Room getUp() {
return up;
}
public Room getDown() {
return down;
}
public Room getRight() {
return right;
}
public Room getLeft() {
return left;
}
public char getValue() {
return value;
}
public boolean getVisited() {
return visited;
}
public boolean getBlocked() {
return blocked;
}
}
我的马齐格人:
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.PrintWriter;
import java.util.*;
import javax.swing.JOptionPane;
public class Maze {
String inFile, // Name of file to be used as input
outFile, // Name of file to output completed maze to
line; // Current line being read by scanner
char [][] mazeContent;
Room [][] rooms;// Holds the values that create maze
Room [] theStack;
Room current = new Room();
ArrayList<Room> al;
int rows, columns;
int tos = 0;
char [][] mazeC;
public static void main(String []args) throws Exception {
Maze m = new Maze();
}
public Maze() throws FileNotFoundException {
// Prompts user for the name of the file they wish to use as the input file.
inFile = JOptionPane.showInputDialog(null, "Please enter the name of the file you wish to read, including " +
"the file path:");
//if(inFile.equals("")) inFile = "C:JavaJavaFilesmaze1.txt;
// Prompts user to enter the name they wish to save the file under.
outFile = JOptionPane.showInputDialog(null, "Please enter the filename you wish to save the data to:");
// Creates a scanner object to read in the input file.
Scanner readFile = new Scanner(new FileReader(inFile));
PrintWriter output = new PrintWriter(outFile);
rows = readFile.nextInt();
columns = readFile.nextInt();
readFile.nextLine();
theStack = new Room[1000];
mazeContent = new char [rows][columns];
rooms = new Room [rows][columns];
theStack = new Room[1000];
for(int i = 0; i < rows; i++) {
line = readFile.nextLine();
for(int j = 0; j< line.length(); j++) {
mazeContent[i][j] = line.charAt(j);
}
}
createRooms();
findPath();
}
private void findPath() {
Room start = rooms[1][1];
push(start);
while(!isEmpty()) {
current = pop();
//System.out.println("The value is " + current.getValue());
if(current.getValue() == $ ) {
System.out.println("Success");
}
else if(current.getBlocked() != true && current.getVisited() != true) {
current.setVisited(true);
push(current.getRight());
push(current.getLeft());
push(current.getUp());
push(current.getDown());
}
}
}
public void createRooms() {
for(int i = 1; i < rows - 1; i++) {
for(int j = 1; j < columns -1; j++) {
Room r = new Room();
r.setCord(i,j);
r.setValue(mazeContent[i][j]);
r.setUp(rooms, i-1, j);
r.setDown(rooms, i+1, j);
r.setRight(rooms, i, j+1);
r.setLeft(rooms, i, j-1);
if(mazeContent[i][j] == * )
r.setBlocked(true);
else
r.setBlocked(false);
rooms[i][j] = r;
}
}
}
private Room pop() {
return theStack[--tos];
}
private boolean isEmpty() {
// TODO Auto-generated method stub
return tos == 0;
}
private void push(Room item) {
if (isFull()) {
System.out.println("The stack is full!");
}
else
theStack[tos++] = item;
}
private boolean isFull() {
return tos == theStack.length-1;
}
}