我正在尝试编写一个解决迷宫问题的程序。我在程序中没有错误。但是当运行程序时,迷宫不会出现。我得到:
assignment4b.Maze@2a46b75b
Your move (1:north, 2:west, 3:east, 4:south, 5:help):
这是我正在阅读的文件的样子:
20 7 Number of columns and rows
0 18 Exit square (rows and columns start at 0)
6 12 Entrance square
XXXXXXXXXXXXXXXXXX X
X XXXX X
X XXXXX XXXXX XX X
X XXXXX XXXXXXX XX X
X XX XX X
X XXXXXXXXXX XX X
XXXXXXXXXXXX XXXXXXX
这就是我的主要外观:
import java.util.Scanner;
public class Question5
{
public static void main (String[]args)
{
// Construct the maze
Maze m = new Maze("Maze.txt");
System.out.println(m);
while (!m.isOut())
{
// input from the keyboard
int move;
Scanner kb = new Scanner(System.in);
System.out.print("Your move (1:north, 2:west, 3:east, 4:south, 5:help): ");
move = kb.nextInt();
System.out.println();
// Process the player's selection
switch (move)
{
case '1':
m.goNorth();
break;
case '2':
m.goWest();
break;
case '3':
m.goEast();
break;
case '4':
m.goSouth();
break;
case '5':
m.showPath(System.out);
System.out.println();
break;
}
// Display the new maze (unless help was selected)
if (move != 5)
{
System.out.print(m);
System.out.print("\n");
}
}
}
}
这就是我的迷宫课的样子:
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Scanner;
public class Maze implements MazeInterface
{
public static final int CLEAR = 0;
public static final int WALL = 1;
public static final int VISITED = 2;
public static final int PATH = 3;
private int row;
private int col;
// 2D array to represent the maze (on the heap)
private int[][] maze;
// entrance and exit points of the maze
private Location entrance = new Location();
private Location exit = new Location();
// Location of the creature
private Location creature = new Location();
public Maze(String filename) throws IOException
{
FileInputStream fis = new FileInputStream(filename);
Scanner readFile = new Scanner (fis);
if( readFile != null)
{
row = readFile.nextInt();
col= readFile.nextInt();
}
// Maze structure
// Create the maze on the heap: 2D array of row*col ints
maze = new int[row][col];
for(int i=0; i<row; ++i)
{
for(int x = 0; x<col;x++)
{
row = i;
col = x;
}
}
// Read the maze line by line
String mazeLine;
for(int r=0; r<row; ++r)
{
// eof must not have been reached
while(readFile.hasNextLine())
{
mazeLine=readFile.toString();
for(int c=0; c<col; ++c)
{
// mazeline must be made of 'X' and ' ' only
if(mazeLine.charAt(c) ==' ' || mazeLine.charAt(c) =='X');
{
maze[r][c] = ((mazeLine.charAt(c) ==' ')?CLEAR:WALL);
}
}
}
}
// The entrance and exit must not be blocked
if(maze[entrance.r][entrance.c]!=WALL && maze[exit.r][exit.c]!=WALL)
{
// The creature is at the entrance
creature = entrance;
}
}
public boolean goNorth()
{
// Location north of the creature
Location north = creature;
north.r--;
// if north is inside of the maze and the square
// at north is not a wall, we can move the creature
if (insideMaze(north) && square(north) != WALL)
{
creature = north;
return true;
}
else
{
return false;
}
}
public boolean goSouth()
{
// Location south of the creature
Location south = creature;
south.r++;
// if south is inside of the maze and the square
// at south is not a wall, we can move the creature
if (insideMaze(south) && square(south) != WALL)
{
creature = south;
return true;
}
else
{
return false;
}
}
public boolean goWest()
{
// Location west of the creature
Location west = creature;
west.c--;
// if west is inside of the maze and the square
// at west is not a wall, we can move the creature
if (insideMaze(west) && square(west) != WALL)
{
creature = west;
return true;
}
else
{
return false;
}
}
public boolean goEast()
{
// Location east of the creature
Location east = creature;
east.c++;
// if east is inside of the maze and the square
// at east is not a wall, we can move the creature
if (insideMaze(east) && square(east) != WALL)
{
creature = east;
return true;
}
else
{
return false;
}
}
public boolean isOut()
{
return (creature == exit);
}
public void showPath(OutputStream out)
{
// =there is a path
boolean success = false;
// Look for the path recursively
findPath(creature,success);
// print the path (if we found one0
if (success)
{
System.out.println(this);
}
else
{
System.out.println( "There is no solution path");
}
// Remove the VISITED and PATH marks
// (set such squares to CLEAR)
for (int i = 0; i < row; ++i)
{
for (int j = 0; j < col; ++j)
{
if (maze[i][j] == PATH || maze[i][j] == VISITED)
{
maze[i][j] = CLEAR;
}
}
}
}
public <ostream> ostream leftShift(ostream out, Maze m)
{
// X: wall
// C: creature
// : empty square
// .: path leading to the exit
// character on the square
char l;
for (int r = 0; r < m.row; ++r)
{
for (int c = 0; c < m.col; ++c)
{
switch (m.maze[r][c])
{
case CLEAR:
case VISITED:
l = ' ';
break;
case WALL:
l = 'X';
break;
case PATH:
l = '.';
break;
}
// Check for the creature
if (r == m.creature.r && c == m.creature.c)
{
l = 'C';
System.out.println(l);
}
}
}
return out;
}
public void markSquareAsVisited(Location l)
{
maze[l.r][l.c] = VISITED;
}
public void markSquareAsPath(Location l)
{
maze[l.r][l.c] = PATH;
}
public int square(Location l)
{
return maze[l.r][l.c];
}
public boolean insideMaze(Location l)
{
return (l.c >= 0 && l.c < col && l.r >= 0 && l.r < row);
}
public void findPath(Location l, Boolean success)
{
// This square is being visited
// Mark it as such
markSquareAsVisited(l);
// If it is the exit square, we are done
// Mark the square as part of the path
// set success to true
if (l == exit)
{
success = true;
markSquareAsPath(l);
return;
}
// try all possible moves from this square
// north, south, east and west
Location north = l;
north.r--;
Location south = l;
south.r++;
Location east = l;
east.c++;
Location west = l;
west.c--;
// If we haven't found the winning path yet and we can go north
// and we haven't been there already, look for the winning path
// starting from north
if (!success && insideMaze(north) && square(north) != WALL && square(north) != VISITED)
{
findPath(north,success);
}
// If we haven't found the winning path yet and we can go south
// and we haven't been there already, look for the winning path
// starting from south
if (!success && insideMaze(south) && square(south) != WALL && square(south) != VISITED)
{
findPath(south,success);
}
// If we haven't found the winning path yet and we can go east
// and we haven't been there already, look for the winning path
// starting from east
if (!success && insideMaze(east) && square(east) != WALL && square(east) != VISITED)
{
findPath(east,success);
}
// If we haven't found the winning path yet and we can go west
// and we haven't been there already, look for the winning path
// starting from west
if (!success && insideMaze(west) && square(west) != WALL && square(west) != VISITED)
{
findPath(west,success);
}
// If success is true, this location is part of a
// solution path. Mark the square as part of the path.
if (success)
{
markSquareAsPath(l);
}
}
}
这就是我的位置类的样子:
public class Location implements LocationInterface
{
// store a location in the maze
// r and c are the row and column of the location in the maze
public int r;
public int c;
// Comparison of 2 locations
public boolean equalsTo(Location l)
{
return (r == l.r && c == l.c);
}
public boolean notEqualsTo(Location l)
{
return (r != l.r || c != l.c);
}
}
答案 0 :(得分:2)
这导致输出:
Maze m = new Maze("Maze.txt");
System.out.println(m);
如果您想打印其他内容,则需要在toString()
课程上实施Maze
方法。例如,如果你这样实现它:
public String toString() {
return "I'm a maze";
}
而不是说
assignment4b.Maze@2a46b75b
Your move (1:north, 2:west, 3:east, 4:south, 5:help):
会说
I'm a maze
Your move (1:north, 2:west, 3:east, 4:south, 5:help):
答案 1 :(得分:0)
您正在打印对象的引用。由于您的迷宫是一个多行对象,因此您最好在print()
上编写Maze
方法,或者可以覆盖toString()
以提供更有用的字符串表示形式。