将true行从文件添加到ArrayList但忽略false行

时间:2013-10-16 15:27:04

标签: java arraylist

我正在创建一个穿过迷宫的程序,并选择正确的路径。有多个级别和房间。我能够读取文件并让程序通过迷宫,但问题是程序正在打印文件中每个房间的解决方案,它应该只打印正确的路径。我正在尝试创建一个循环,其中房间是真的,可以退出到ArrayList并忽略除了入口之外没有出路的房间。以下是文件中房间的示例:

1 0 0 0 0 0 0 1 
0 0 0 0 0 1 0 0 
0 0 0 0 0 0 0 0 
0 0 0 1 0 0 0 1 
0 0 0 1 0 0 0 0 
1 0 0 1 0 1 0 0 
0 0 0 1 0 0 0 0 
1 0 0 1 0 0 0 1

这是我的代码,我已经采用了我认为应该对底部的切换评论工作的方式:

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


public class Maze {
static boolean [][][] maze;
static int dimensions;
static boolean theEnd = false;
static ArrayList<Integer> path = new ArrayList<Integer>();

public static void main(String[] args) throws IOException {


    String fileName = "";
    Scanner input = new Scanner(System.in);

    System.out.print("What's the name of the file: ");
    fileName = input.nextLine();
    Scanner inputFile = new Scanner(new File(fileName + ".txt"));

    String firstLine = inputFile.nextLine();
    String [] pieces = firstLine.split(" ");
    dimensions = pieces.length;

    maze = new boolean[dimensions][dimensions][dimensions];
    for(int floor = 0; floor < dimensions; floor++){
        for(int row = 0; row < dimensions; row++){
            for(int column = 0; column < dimensions; column++){
                int place = Integer.parseInt(pieces[column]);
                if(place == 1){
                    maze[floor][row][column] = true;
                }
                else{
                    maze[floor][row][column]= false;
                }
                //System.out.print(place);
            }
            //System.out.println();
            String nextLine = inputFile.nextLine();
            if(!nextLine.isEmpty()){
                pieces = nextLine.split(" ");
            }
            else if (floor != dimensions - 1){
                pieces = inputFile.nextLine().split(" ");
            }
        }
    }

    traverse(0,0,0);
    inputFile.close();
    input.close();
    //System.out.println(theEnd);

}

private static void traverse(int floor, int row, int column) {
    maze[floor][row][column]= false;
    if(floor == dimensions - 1 && row == dimensions - 1 && column == dimensions - 1){
        theEnd = true;
    }

    if(row != 0 && maze[floor][row - 1][column]){
        traverse (floor, row - 1, column);
    }

    if(row != dimensions -1 && maze[floor][row + 1][column]){
        traverse (floor, row + 1, column);
    }

    if(column != 0 && maze[floor][row][column - 1]){
        traverse (floor, row, column - 1);
    }

    if(column != dimensions -1 && maze[floor][row][column + 1]){
        traverse (floor, row, column + 1);
    }

    if(floor != 0 && maze[floor - 1][row][column]){
        traverse (floor - 1, row, column);
    }

    if(floor != dimensions - 1 && maze[floor + 1][row][column]){
        traverse (floor + 1, row, column);
    }

    do{
        System.out.println("(" + floor + ", " + row + ", " + column + ")");
    }while(maze.equals(true));

    // if (the room is true and can be exited){
        // then add the room to the ArrayList
    //}
    // else {
        // the room should be skipped and not added to the ArrayList
    //}
}

}

如何创建if else语句以添加可以退出到ArrayList的房间?谢谢你的帮助!

0 个答案:

没有答案