Java迷宫不会打印

时间:2018-02-28 17:44:15

标签: java maze

我必须为java作业制作一个迷宫,我能够完成其中的大部分工作。我收到了包含所有方法的代码大纲。有人能帮我吗?我的问题是迷宫不会打印出来,我无法弄清楚原因。

package maze;

public class Maze {

private char direction; 
private int r;  // x position of the mouse
private int c;  //y position of the mouse
private boolean exitFound = false;


public Maze(int[][] arrMaze) {
    this.r = arrMaze.length - 1;
    this.c = 0;
}

//Prints out the maze without solution
public void displayMaze(int[][] arrMaze) 
{   
    //display the maze putting blank spaces where there are 1's in the array and putting
    //another symbol where there are 0's to show the maze without the solution
    for(int i=0; i<arrMaze.length; i++){
        System.out.println(" ");
        for(int j=0; j<arrMaze[i].length; j++){
            if(arrMaze[i][j] == 0){
                System.out.print("#");
            } if(arrMaze[i][j] == 1) {
                System.out.print(" ");
            } if(arrMaze[i][j] == 2){
                System.out.print("@");
            } if(arrMaze[i][j] == 3){
                System.out.println("~");
            }
        }
    }

}

//displays the Maze with the path taken
public void displayPath(int[][] arrMaze)  
{  
    //show the user how far the mouse has gone since the start.
    //The path the mouse has gone will be filled in but the path ahead will not.
    for (int i = 0; i < arrMaze.length; i++) {
        System.out.println(" ");
        for (int j = 0; j < arrMaze[i].length; j++) {
            if (arrMaze[i][j] == 3) {
                System.out.print("@");
            } else if (arrMaze[i][j] == 2) {
                System.out.print("~");
            } else if (arrMaze[i][j] == 0) {
                System.out.print("#");
            } else {

            }
        }
    }


}


public boolean takeStep(int[][] newMaze) {
    // moveNorth(newMaze)
    for (int i = 0; i < newMaze.length; i++) {
        System.out.println(" ");
        for (int j = 0; j < newMaze[i].length; j++) {
            if (newMaze[r][c] == 3) {
                moveNorth(newMaze);
                System.out.print("~");
            } else if (newMaze[r][c] == 2) {

                System.out.print("@");
            } else {

            }
        }
    }

    return isAnExit(newMaze);
}


public void moveNorth(int[][] arrMaze) {
    //complete the code here
    /*method will check for a 0 or a 1 in the position above the current position 
     * and then if not a 0 will change the current position to the row above it, but in the same column.
     */

    if (arrMaze[r][c - 1] != 0) {
        arrMaze[r][c - 1] = 3;
        arrMaze[r][c + 1] = 2;
    } else {
        moveSouth(arrMaze);
    }
    displayPath(arrMaze);
}

public void moveSouth(int[][] arrMaze) 
{ 
    //method will check for a 0 or a 1 in the position below the current position and then if not a 0
    //it will change the current position to the row below it, but in the same column.

    if (arrMaze[r][c + 1] != 0) {
        arrMaze[r][c + 1] = 3;
        arrMaze[r][c + 1] = 2;
    } else {
        moveNorth(arrMaze);
    }
    displayPath(arrMaze);
}


public void moveEast(int[][] arrMaze) {
    //method will check for a 0 or a 1 in the position to the right of  the current position and then if 
    //not a 0 will change the current position to the column to the right but the same row.

   if (arrMaze[r + 1][c] != 0) {
        arrMaze[r + 1][c] = 3;
        arrMaze[r - 1][c] = 2;
    } else {
        moveWest(arrMaze);
    }
    displayPath(arrMaze);
}




public void moveWest(int[][] arrMaze) {
    //method will check for a 0 or a 1 in the position to the left of  the current position and then if
    //not a 0 will change the current position to the column to the left but the same row.

    if (arrMaze[r - 1][c] != 0) {
        arrMaze[r - 1][c] = 3;
        arrMaze[r + 1][c] = 2;
    } else {

    }
    displayPath(arrMaze);
}




private boolean isAnExit(int[][] arrMaze) {
    //method will return true if the user arrives into the last column of the array because there is only one 
    //location in the last column that is a 1, so if the user reaches the array[i].length then that means that it found an exit.

    if (arrMaze[r][c] > arrMaze.length) {
        exitFound = true;
    } else {
        exitFound = false;
    }
    return exitFound;

}

//finds the path without stopping at every step
//method will show the complete path from start to finish of the maze and the suggested route to the end.
public void findExit(int[][] arrMaze) {
if (arrMaze[r][c] > arrMaze.length) {
        for (int i = 0; i < arrMaze.length; i++) {
            takeStep(arrMaze);
        }
    }
   }
}

这是测试代码。我提供了测试代码,但我还没有改变它。

package maze;

import java.util.Scanner;

public class TestMaze 
{
public static void main(String[] args)
{

  int[][] mazeArray = {
         {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
         {0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,0},
         {0,0,0,0,0,0,0,0,0,0,1,1,1,1,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0},
         {0,0,0,1,1,1,1,0,0,0,1,0,0,0,0,0,0,1,0,0,0,0,0,1,0,0,0,0,0,0,0,1,1,1,1,1},
         {0,0,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0},
         {0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0},
         {0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,0},
         {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,0,0,0,0,0,0,0,0,1,1,1,1,1,1,0,0,0},
         {0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,1,0,0,0,0,0},
         {0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,0,0,0,0},
         {0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,0,0,0,0},
         {0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0},
         {0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,0,0,0,0,0,0},
         {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0},
         {0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0},
         {1,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0},
         {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}};

  Maze myMaze = new Maze(mazeArray);      
  boolean keepAsking = true;
  Scanner scan = new Scanner(System.in);
  String input = "";
  myMaze.displayPath(mazeArray);
  System.out.println("Maze");

  do {
     System.out.println("T = Take a step | S = Show path | Q = Quit");
     System.out.print("Enter command: ");
     input = scan.nextLine();
     input.trim();
     input.toLowerCase();
     if(input.equals("t")) {

        keepAsking = !myMaze.takeStep(mazeArray);
        System.out.println("Which direction would you like to go? N, S, E, W?");
        String direction = scan.nextLine();
        if(direction.equalsIgnoreCase("n"))
           myMaze.moveNorth(mazeArray);
        if(direction.equalsIgnoreCase("s"))
           myMaze.moveSouth(mazeArray);
        if(direction.equalsIgnoreCase("e"))
           myMaze.moveEast(mazeArray);
        if(direction.equalsIgnoreCase("w"))
           myMaze.moveWest(mazeArray);

     } 
     else if(input.equals("s")) {
        myMaze.findExit(mazeArray);
        keepAsking = false;
     } 
     else if(input.equals("q")) {
        keepAsking = false;
     } 
     else {
        System.out.println("ERR: Invalid input");
     }
  } while(keepAsking);
  System.out.println("Quitting program...");
  scan.close();
  }
}

2 个答案:

答案 0 :(得分:0)

您需要致电displayMaze()(在displayPath()中)进行打印。

目前,您没有调用该方法,这意味着您的代码永远不会打印迷宫,因为它没有被指示。

此外,您在哪里为rc分配值?我认为您打算在i方法(j而不是displayPath())中使用[i][j][r][c]

答案 1 :(得分:0)

我想你在某个地方犯了一个错误,因为你的rc值在任何地方都未定义。尝试添加代码来初始化和更新这些值,然后你应该看到你的迷宫打印。