在最后一行代码的主要方法上,我有一个目录来运行迷宫.txt从我的桌面



在我的代码的主方法的最后一行,我有一个目录来运行迷宫.txt从我的桌面运行迷宫。我该如何解决此问题,因为如果我将此代码发送给其他人,他们必须打开文件并将目录更改为迷宫的目录.txt他们与我的文件一起下载了该目录。

迷宫.txt

7

7







咕�
import java.io.*;
public class MazeSolver {

private char [][] maze;
private int startX , startY;
private int counter = 0;

public  MazeSolver(String fileName) throws IOException {
maze = fileIterator(fileName);
startX = startX(maze);
startY = startY(maze);
solve(startX,startY);
System.out.println(printMaze());

}
public void solve(int x, int y) {
if (findPath(x,y)) {
maze[x][y] = 'S';
}
}
public boolean findPath(int x , int y){
counter ++;
if (maze[x][y] > 7) {return false;}
if (maze[x][y] == 'G') {return true;}
if (maze[x][y] == 'X' || maze[x][y] == 'O'){return false;}
maze[x][y] ='O';
boolean result;
result = findPath(x , y+1);
if(result){return true;}
result = findPath(x-1 , y);
if(result){return true;}
result = findPath(x , y-1);
if(result){return true;}
result = findPath(x+1 , y);
if(result){return true;}
maze[x][y] = 'O';
return false;

}
public String printMaze() {
String output = "";
for (int x = 0; x < 7; x++) {
for (int y = 0; y < 7; y++) {
output += maze[x][y] + " ";
}
output += "n";
}
return output;
}

private char[][] fileIterator(String fileName) throws IOException {
File file = new File(fileName);
if(!file.exists()){
System.out.println(fileName+ "does not exists");
}
if(!(file.canRead() && file.isFile())){
System.out.println(fileName + "can not be read");
}
BufferedReader read = new BufferedReader(new FileReader(file));
String rea = read.readLine();
String[] split = rea.split(" ");
int row =  Integer.valueOf(split[0]);
int col = Integer.valueOf(split[1]);
String readline;
int num = 0;
char [][] maze = new char[row][col];
while((readline = read.readLine()) != null){
char[] ch = readline.toCharArray();
for(int i = 0;i < ch.length;i++){
maze[i][num] = ch[i];
}
num++;
}
return maze;
}
private static int startX(char[][] charArray){
int startX = 0;
for(int i=0 ; i < charArray.length ; i++){
for(int j=0 ; j < charArray[i].length ; j++){
if(charArray[i][j] == 'S'){
startX = i;
}
}
}
return startX;
}
private static int startY(char[][] charArray){
int startY = 0;
for(int i=0 ; i < charArray.length ; i++){
for(int j=0 ; j < charArray[i].length ; j++){
if(charArray[i][j] == 'S'){
startY = j;
}
}
}
return startY;
}

public static void main(String[] args) throws IOException {
MazeSolver ms = new MazeSolver("C:\Users\mypc\Desktop\maze.txt");
}
}

有很多解决方案,具体取决于您的需求。

如果您希望用户尝试他们可以提供的不同迷宫.txt文件,一个简单的方法是从命令行参数(即从 main 方法的args参数)获取文件的路径。

您可以将主方法主体更改为:

MazeSolver ms = new MazeSolver(args[0]);

当然,需要进行进一步的检查,但这与本练习无关。

然后,用户从终端运行您的程序,如下所示:

java MazeSolver /path/to/their/maze.txt

/path/to/their/maze.txt将由args[0]在您的 main 方法中捕获。

import java.util.Scanner;
import java.io.*;
public class MazeSolver {
private char[][] maze;
private int startX;
private int startY;
private int row;
private int col;
private int endX;
private int endY;
public MazeSolver (String fileName) {
try {
Scanner get = new Scanner(new FileReader(fileName));

row = get.nextInt(); // Integer.parseInt(sChar[0]);
col = get.nextInt(); //col = Integer.parseInt(sChar[2]);        
maze = new char[row][col];      
String mazeString = "";
// Read the entire file and store in a String called mazeString
while(get.hasNextLine()) {
mazeString += get.nextLine();
}
char[] temp = mazeString.toCharArray();
for(int x = 0; x < row * col; x++) {
maze[x/row][x%col] = temp[x];
}
}
catch (IOException e) {
System.out.println("nFile cannot be found. Please try again: n");
System.exit(0);
}
char start = 'S';
for(int i = 0; i < row; i++) {
for(int x = 0; x < col; x++) {
char setStart = maze[i][x];
if(setStart == start) {
startX = i;
startY = x;
}
}
}
char goal = 'G';
for(int i = 0; i < row; i++) {
for(int x = 0; x < col; x++) {
char setEnd = maze[i][x];
if(setEnd == goal) {
endX = i;
endY = x;
}
}
}
if (solveMaze(startX,startY)){
System.out.println("Solution Found");
printMaze();
}
else{
System.out.println("No solution Found");
printMaze();
}
}
public void printMaze() {
for(int r = 0; r < row; r++) {
for(int c = 0; c < col; c++) {
System.out.print(maze[r][c]);
}
System.out.println();
}
}
public boolean solveMaze(int x, int y) {
// Base case
if(x == endX && y == endY) {
maze[x][y] = 'G';
maze[startX][startY]='S';
return true;
}
// Out of bounds
if((x >= 0 && x < row && y >= 0 && y < col && maze[x][y] != 'X' && maze[x][y] != '.') == true) {
maze[x][y] = '.';
// Right
if(solveMaze(x + 1, y)) {
return true;
}
// Left
if(solveMaze(x - 1, y)) {
return true;
}
// Up
if(solveMaze(x, y - 1)) {
return true;
}
// Down
if(solveMaze(x, y + 1)) {
return true;
}
else {
maze[x][y] = '#'; 
return false;
}
}
else {
return false;
}
}

}

相关内容

最新更新