簡體   English   中英

Java 迷宮程序邏輯錯誤

[英]Java Maze Program Logical Errors

我正在嘗試構建一個迷宮程序,它允許您選擇一個起點(迷宮內應該是零),它會告訴您是否有通往出口的路線(在 maze.txt 文件中的迷宮中用 E 表示) )。 它只能通過水平或垂直方向的零路徑到達出口。 如果您嘗試從“1”開始,它會返回您無法從此處開始。 如果您從沒有通往 'E' 或出口的路線的 '0' 開始,它會返回“幫助我被困”。 如果它發現從水平或垂直跟隨“0”到 E 的路徑,則應將該“路徑”轉換為加號以顯示路徑。 從用戶輸入的起點在迷宮中表示為“S”。

(迷宮.txt文件)

E0001110000000100100
11100011101110001111
11111000101000111000
00001110101100010010
01011000101111000110
00001110000110011110
11011100110110111000
00011110110111111101
01011011110110100001
01000000000110110111
11011011010010000000
01010010011000101011
01111000101110101110
00001110000111011001
01101011101101000011
11000110100111011010
01110000100100110011
11010111110110000000
01110100011000111110
00011001000011100010

我提供的代碼中的問題:

  1. 當我選擇一個明顯的起點,通向迷宮中由“E”表示的出口時,它返回“幫助我被困”(第 0 行,第 2 列),這是不正確的

  2. 問題 1 的 B/c,我不知道我標記路徑的功能是否正常工作 b/c 我無法“找到路徑”。

編碼

//Class 1
public class mazeGame{

   public void solveMaze(char [][] maze, int row1, int column1) {

       if(findpath(maze, row1, column1)) {

           print(maze, row1, column1);

           System.out.println("I am finally free!!");

       }//end of if

       else {

           print(maze,row1,column1);

           System.out.println(" Help me! I am trapped");



       }//end of else

   }//end of solveMaze method







   public boolean findpath(char [][] maze, int a, int b)

   {

       if(out(maze,a,b))

       {

           return false;

       }//end of if



       if(closed(maze,a,b) || marked(maze,a,b)) {

           return false;

       }//end of if



       if(exit(maze,a,b)) {

           return true;

       }//end of if



       mark(maze,a,b);



       if(findpath(maze,a,b-1)) {

           return true;

       }//end of if.... Goes left



       if(findpath(maze, a, b+1))//goes right

       {

           return true;

       }//end of if



       if(findpath(maze,a-1, b)) {



           return true;

       }//end of if... goes up 1... ex: row 5-1 is now row 4(which is above row 5



       if(findpath(maze,a+1, b)) {



           return true;

       }//end of if... goes down 1



       unmark(maze,a,b);//unmarks path

       return false;



   }//end of findpath









   public boolean closed(char [][] maze, int a, int b) {

       boolean c = false;

       if(maze [a][b] == '1')

       {

           c = true;

       }//end of if

       return c;

   }//end of closed method. checks if path is invalid





   public boolean exit(char[][] maze, int a, int b) {



       boolean c = false;

       if(maze [a][b]=='E')

       {

           c = true;

       }//end of if

       return c;

   }//end of exit method... checks if exit has been reached



   public boolean marked(char [][] maze, int a , int b) {

       boolean c = false;

       if(maze[a][b]=='+')

       {

           c = true;

       }//end of if

       return c;



   }//end of marked... checks if path is marked



   public boolean open(char [][] maze, int a, int b) {

       boolean c= false;

       if(maze [a][b]=='0')

       {

           c = true;

       }//end of if

       return c;



   }//end of open method...checks if path is valid



   public boolean out(char [][] maze, int a, int b) {

       boolean c = false;

       if(a >= maze.length || b>= maze[0].length || a<0 || b>0) {

           c = true;

       }//end of if

       return c;

   }//end of out... checks if path is out of range



   public char mark(char [][] maze, int a, int b) {

       return maze[a][b]='+';

   }//end of mark... marks path









   public void unmark(char[][]maze, int a, int b) {

       maze[a][b] = 'x';

   }//end of unmark












   public void print(char [][] maze, int row1, int column1) {

       maze[row1][column1]='S';

       print(maze);

   }//end of print method...prints maze, and users starting point



   public void print(char [][] maze) {

       System.out.printf("%-4s", "");//DOUBLE CHECK THIS

       for(int i= 0; i< maze[0].length;i++)

       {

           System.out.printf("%-4d", i);



       }//end of for loop

       System.out.println();



       for(int i = 0; i< maze.length; i++)

       {

           System.out.printf("%-4d",i);

           for(int j = 0; j< maze[0].length; j++)

           {

               System.out.printf("%-4c", maze[i][j]);

           }//end for loop

           System.out.println();

       }//end of for loop  

   }//end of print method...prints maze







}//end of mazeGame class

求求你笑一笑

難讀的代碼,但很容易解決問題

在您的public boolean out您檢查 b>0 而不是 b<0

有了這個,一切正常:)


此外:

類名應以大寫字母開頭

我也做了一些代碼格式化的東西。 如果你想看...

迷宮游戲

//Class 1
public class MazeGame {

    public void solveMaze(char [][] maze, int row1, int column1) {
        if(findpath(maze, row1, column1)) {
            print(maze, row1, column1);
            System.out.println("I am finally free!!");
        } else {
            print(maze,row1,column1);
            System.out.println(" Help me! I am trapped");
        }
    }

    public boolean findpath(char [][] maze, int a, int b) {
        // check current field
        // check if is walkable
        if(out(maze,a,b) || closed(maze,a,b) || marked(maze,a,b)) return false;
        // found exit?
        if(exit(maze,a,b)) return true;
        mark(maze,a,b);

        // go further (recursion)
        if(findpath(maze,a,b-1))  return true;
        if(findpath(maze, a, b+1))return true;
        if(findpath(maze,a-1, b)) return true;
        if(findpath(maze,a+1, b)) return true;

        //unmark this position
        unmark(maze,a,b);
        return false;
    }

    public boolean closed(char [][] maze, int a, int b) {
        return maze [a][b] == '1';
    }

    public boolean exit(char[][] maze, int a, int b) {
        return maze [a][b]=='E';
    }

    public boolean marked(char [][] maze, int a , int b) {
        return maze[a][b]=='+';
    }

    public boolean open(char [][] maze, int a, int b) {
        return maze[a][b]=='0';
    }

    public boolean out(char [][] maze, int a, int b) {
        return (a >= maze.length || b>= maze[0].length || a<0 || b<0);
    }

    public void mark(char [][] maze, int a, int b) {
        maze[a][b]='+';
    }

    public void unmark(char[][]maze, int a, int b) {
        maze[a][b] = 'x';
    }

    public void print(char [][] maze, int row1, int column1) {
        maze[row1][column1]='S';
        print(maze);
    }

    public void print(char [][] maze) {
        System.out.printf("%-4s", "");
        for(int i= 0; i< maze[0].length;i++)
            System.out.printf("%-4d", i);
        System.out.println();

        for(int i = 0; i< maze.length; i++) {
            System.out.printf("%-4d",i);
            for(int j = 0; j< maze[0].length; j++) {
                System.out.printf("%-4c", maze[i][j]);
            }
            System.out.println();
        }
    }
}

司機

import java.util.*;
import java.io.File;
import java.io.FileNotFoundException;

public class Driver {

    public static void main(String[] args) {
        int rows = 20;
        int columns = 20;
        char [][] maze = new char [rows][columns];//maze size
        Scanner scan = new Scanner(System.in);

        try {
            File maze_file = new File("src/main/maze.txt");
            System.out.println(maze_file.getAbsolutePath());
            Scanner s = new Scanner(maze_file);
            while(s.hasNext()) {
                for(int row = 0; row < maze.length;row++) {
                    String line = s.nextLine();
                    for (int col = 0; col < 20; col++) {
                        maze[row][col] = line.charAt(col);
                    }
                }
            }
            s.close();
        } catch(FileNotFoundException e) {
            System.out.println("ERROR. FILE NOT FOUND");
        }

        MazeGame d = new MazeGame();
        d.print(maze);

        System.out.println("You will now enter the row and column # that you wish to start at.");
        System.out.println("Enter which row you would like to start at(Row # 0-19) ");
        int row1 = scan.nextInt();//fetchs row number from user
        System.out.println("Enter which column you would like to start at (Column# 0-19) ");
        int column1 = scan.nextInt();//fetches column number from user

        if(row1 >= maze.length || column1 >= maze[0].length)
            System.out.println("The number entered is invalid");
        else if(maze[row1][column1]=='E')
            System.out.println(" Can not start at exit");
        else if(maze[row1][column1]=='1')
            System.out.println("You can not start at a value containing 1, only 0");
        else
            d.solveMaze(maze, row1, column1);

    }

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM