Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/399.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在java中从文本文件执行多个迷宫_Java_Arrays - Fatal编程技术网

在java中从文本文件执行多个迷宫

在java中从文本文件执行多个迷宫,java,arrays,Java,Arrays,你好,我是stackoverflow和Java新手。我已经在这个项目上工作了很长一段时间(大约一周),我需要创建一个迷宫解算器,在每个文本字段的[0,0]到[9,9]之间运行(idk,如果我使用正确的术语)。每个迷宫由一条空线隔开。我有正确显示迷宫的程序,但现在问题出现了,我无法让它解决每个迷宫,只能解决文本文件中的最后一个迷宫,即使我更改迷宫的顺序,它也只能解决最后一个迷宫。该程序运行迷宫集合中的最后一个迷宫,但我无法让它从第一个迷宫开始并连续执行每个迷宫,以下是我编写的代码: import

你好,我是stackoverflow和Java新手。我已经在这个项目上工作了很长一段时间(大约一周),我需要创建一个迷宫解算器,在每个文本字段的[0,0]到[9,9]之间运行(idk,如果我使用正确的术语)。每个迷宫由一条空线隔开。我有正确显示迷宫的程序,但现在问题出现了,我无法让它解决每个迷宫,只能解决文本文件中的最后一个迷宫,即使我更改迷宫的顺序,它也只能解决最后一个迷宫。该程序运行迷宫集合中的最后一个迷宫,但我无法让它从第一个迷宫开始并连续执行每个迷宫,以下是我编写的代码:

import java.util.ArrayList;
import java.util.Scanner;
import java.io.File;

public class Maze
{
private static char[][] maze;

private static ArrayList<String> mazegenerator;

public static void initializeMaze(String fileName)
{
        try
          {
    mazegenerator = new ArrayList<>();
    int numcols = 0;
            int r = 0;

        Scanner file = new Scanner(new File(fileName));
        while(file.hasNextLine())
        {
            String nextLine = file.nextLine(); // sets the String nextLine a value
                                mazegenerator.add(nextLine); // inserts the string into the array
            if (nextLine.length() > numcols)
                numcols = nextLine.length(); //sets numcols to the length of the Line
                            if(nextLine.length() == 0)
                                clearthismaze(); // clears the input from array
        } // loop to collect the cols and set the row size

    int numrows = mazegenerator.size();
    maze = new char[numrows][numcols];
    for (int i ; r < numrows; r ++)
    {
        String row = mazegenerator.get(r);
        for (int c = 0; c < numcols; c++)
        {
            if(row.length() >= c)
                maze[r][c]= row.charAt(c);
        }
    }
            if(r == 10 && numrows == 10 && numcols == 10)
                System.out.println();
                printMaze();
                solveMaze(0,0);
                System.out.println();
                printMaze();
                numrows = 0;
                numcols = 0;
          }
    catch(Exception e){System.out.println(fileName + " has an issue");} 


} // this method generates the maze and sets it up which is later printed

public static void printMaze()
{
    for (char[] row: maze)
    {
        for (char c: row)
            System.out.print(c);
        System.out.println();
    }
    System.out.println();

} //method to print maze accordingly

public static void main (String[] args)
{       
            System.out.println("This program prints the original maze before solving and then prints the maze solution, \n"
                    + "solving from the upper left to the lower right. X's represent routes taken, 2's represent the true path, \n"
                    + "if no 2's are present then that means the maze is unsolvable \n");
    initializeMaze("multiplemazes.txt");
} // main method , driver method

public static boolean solveMaze(int r, int c)
{
    boolean finished = false;
    if(PossibleToMove(r,c)){
         maze[r][c] = 'X'; // coordinate has been visited mark it so you don't return to it                                                  
         if(c == maze[0].length - 1 && r == maze.length-1)// base case: did I reach destination?
         finished = true;  // maze is solved   
    else{              

        if(!finished)
            finished = solveMaze(r,c-2); // look left
        if(!finished)
            finished = solveMaze(r-1,c); // look up
        if(!finished)
            finished = solveMaze(r,c+2); // look right
        if(!finished)
            finished = solveMaze(r+1,c); // look down
    }
        if(finished) // part of the true path
            maze[r][c] = '2';
        if(!finished){
            System.out.println(">>>" + " Backtracking from " + "( " + r + ", " + c/2 + ")");               
        }
    }
    // if I know that I reached a dead end
    // this can't be part of the solution
    return finished;         
} //method solveMaze

private static boolean PossibleToMove(int r, int c){
boolean canmove  = false;

if(r >=0 && r < maze.length && c >=0 && c < maze[0].length)
   if(maze[r][c] == '0')
       canmove = true;

return canmove;
}  // method PossibleToMove
public static void clearthismaze(){
mazegenerator.clear();
}
} // class Maze

任何数量的帮助都是有用的,是的,这是一个家庭作业,我想我只是陷入了一个循环问题,或者我的clearthismaze()方法出了问题。我的意图是打印出数组,求解数组,清除数组,跳过空行,然后移动到下一个数组。如果有人能指出这个问题,我将非常感激,谢谢。

如果你得到一条空行,你正在清除
mazegenerator
。因此,如果你开始解迷宫,只有最后一个可用

您可以将类型更改为
列表mazegenerator
。因此
mazegenerator.get(0)
保存第一个迷宫,
mazegenerator.get(0).get(0)
保存第一个迷宫的第一行。 您还需要更改文件读取循环:

List<String> singleMaze = new ArrayList<>();
while(file.hasNextLine()) {
    // ...
    if(nextLine.length() == 0) {
        mazegenerator.add(singleMaze);
        singleMaze = new ArrayList<>();
    }
}
List singleMaze=new ArrayList();
while(file.hasNextLine()){
// ...
if(nextLine.length()=0){
mazegenerator.add(单迷宫);
singleMaze=newarraylist();
}
}

为什么您认为需要执行两个步骤才能获得左/右迷宫?我跳过了整数之间的空白,因为我不知道如何将两个整数之间的空格解析为字符数组。在这段时间内,这是一种快速修复方法。在它计算穿过的空间之前。谢谢你帮助我理解这个问题。非常感谢。我无法显示最终的迷宫,我可以通过更改mazegenerator.get().get(r)中的数字来循环显示前3个迷宫,但是如何获得mazegenerator.get(3)(最终的一个)?即使我有一百个迷宫,我也只能得到99个,但不能得到第100个,我的阅读有什么问题吗?将你的解题代码放入循环
for(List singleMaze:mazegenerator)
。这将遍历所有迷宫(不管是3、4还是100)
List<String> singleMaze = new ArrayList<>();
while(file.hasNextLine()) {
    // ...
    if(nextLine.length() == 0) {
        mazegenerator.add(singleMaze);
        singleMaze = new ArrayList<>();
    }
}