用箭头退出迷宫,如何修复代码?

问题描述 投票:-1回答:2

我想创建一个模拟箭头退出迷宫的代码。我想离开迷宫我有一个问题。规则是指您可以输入迷宫的框架并退出框架,只要箭头面向框架即可。

这是代码:

public static void main(String[] args) {
    final int NUMBER_OF_COLUMNS = 6;
    final int NUMBER_OF_ROWS    = 5;

    boolean entryFound  = false;
    boolean exitFound   = false;
    boolean work        = true;
    int     entryIndex  = -1;
    int     exitIndex   = -1;
    int     row         = -1;
    int     column      = -1;

    char[][] pathMatrix = { 
                            { 'O', 'V', 'O', '^', '<', '<' }, 
                            { 'O', 'V', 'V', '*', '^', '^' },
                            { '*', 'V', '*', 'O', '*', '^' },
                            { 'O', 'V', 'O', 'O', 'V', '^' },
                            { 'O', '>', '>', '>', '>', '^' },  };

    for(column = 0; column < NUMBER_OF_COLUMNS; column++){
        if(pathMatrix[0][column] == 'V'){
            entryFound = true;
            entryIndex = column;
        }
        if(pathMatrix[NUMBER_OF_ROWS -1][column] == 'V'){
            System.out.println(NUMBER_OF_ROWS -1 + " " + column);
            exitFound = true;
            exitIndex = column;
        }

        if(pathMatrix[0][column] == '^'){
            exitFound = true;
            exitIndex = column;
        }

        if(pathMatrix[0][column] == '>'){
            exitFound = true;
            exitIndex = column;
        }

    }
    System.out.println(exitIndex);


    for(row = 0; row < NUMBER_OF_ROWS; row++){
        for(column = 0; column < NUMBER_OF_COLUMNS; column++){
            System.out.print(pathMatrix[row][column] + " ");
        }
        System.out.print('\n');
    }

    if(entryFound == false || exitFound == false){
        System.out.println("No path has been found in the matrix above");

        return;
    }
    row = 0;
    column = entryIndex;

    do
    {
        System.out.println(row+" "+column);

        if(pathMatrix[row][column] == 'V'){
            row++;
        }

        else if(pathMatrix[row][column] == '>'){
            column++;
        }
        else if(pathMatrix[row][column] == '<'){
            column--;
        }
        else if(pathMatrix[row][column] == '^'){
            row--;
        }
        else {
            work = false;
        }

    }
    while(work == true && (0 < row && row < NUMBER_OF_ROWS) && (0 < column && column < NUMBER_OF_COLUMNS));

    if(row == NUMBER_OF_ROWS && column == exitIndex){
        System.out.println("The path has been found in the maze above");
    }
    else{
        System.out.println("No path has been found in the maze above");
    }
}

这是路线:(从0.4出来,但不起作用)

O V O ^ < < 
O V V * ^ ^ 
* V * O * ^ 
O V O O V ^ 
O > > > > ^ 
0 1
1 1
2 1
3 1
4 1
4 2
4 3
4 4
4 5
3 5
2 5
1 5
No path has been found in the maze above
java maze
2个回答
1
投票

The problem

您的代码中有2个错误:

1 - The condition in your loop says

while(work == true && (0 < row && row < NUMBER_OF_ROWS) && (0 < column && column < NUMBER_OF_COLUMNS));

0 < row是错误的,这意味着你不能在索引为0的第一行上运行。但是你希望能够这样做,所以你需要写0 <= row。所以这样纠正:

while(work && (0 <= row && row < NUMBER_OF_ROWS) && (0 <= column && column < NUMBER_OF_COLUMNS));

2 - The condition to verify if you found the exit

if(row == NUMBER_OF_ROWS && column == exitIndex)

在这里你检查你是否从最底行退出,你要检查的是你是否退出了顶行,即行是-1。所以这样重写:

if(row < 0 && column == exitIndex)

Side note

我认为你会在可读性方面获得很多好处,如果你将代码分解为方法,测试也会容易得多。这是一个例子:

private static final char[][] pathMatrix = {
        { 'O', 'V', 'O', '^', '<', '<' },
        { 'O', 'V', 'V', '*', '^', '^' },
        { '*', 'V', '*', 'O', '*', '^' },
        { 'O', 'V', 'O', 'O', 'V', '^' },
        { 'O', '>', '>', '>', '>', '^' },  };

private static final int NUMBER_OF_COLUMNS = 6;
private static final int NUMBER_OF_ROWS    = 5;

public static void main(String[] args) {

    printMatrix();

    int[] currentPosition = findEntrance();

    System.out.println("Entrance: " + currentPosition[0] + " " +currentPosition[1] + " " + pathMatrix[currentPosition[0]][currentPosition[1]]);

    while(isInsideMatrix(currentPosition) && isArrow(currentPosition)) {
        System.out.println(currentPosition[0] + " " + currentPosition[1] + " " + pathMatrix[currentPosition[0]][currentPosition[1]]);
        currentPosition = move(currentPosition);
    }

    if(isInsideMatrix(currentPosition)) {
        System.out.println("No path has been found in the maze above");
    } else {
        System.out.println("The path has been found in the maze above");
    }
}

找到迷宫的入口。返回作为{rowIndex,colIndex}形式的int []的位置

private static int[] findEntrance() {
    char c;

    // scan first and last rows
    for(int column = 0; column < NUMBER_OF_COLUMNS; column++) {
        // first row
        c = pathMatrix[0][column];
        if(c == 'V') {
            return new int[] {0, column};
        }
        // last row
        c = pathMatrix[NUMBER_OF_ROWS-1][column];
        if(c == '^') {
            return new int[] {NUMBER_OF_ROWS-1, column};
        }
    }

    // scan first and last columns
    for(int row = 0; row < NUMBER_OF_ROWS; row++) {
        // first column
        c = pathMatrix[row][0];
        if(c == '>') {
            return new int[] {row, 0};
        }
        // last row
        c = pathMatrix[row][NUMBER_OF_COLUMNS-1];
        if(c == '<') {
            return new int[] {row, NUMBER_OF_COLUMNS-1};
        }
    }

    return null;
}

移动光标并返回下一个位置。假设我们目前正站在箭头上

private static int[] move(int[] position) {
    int row = position[0];
    int col = position[1];
    char charAtPosition = pathMatrix[position[0]][position[1]];

    int[] newPosition;
    if(charAtPosition == 'V') {
        newPosition = new int[] {row+1, col};
    } else if(charAtPosition == '^') {
        newPosition = new int[] {row-1, col};
    } else if(charAtPosition == '>') {
        newPosition = new int[] {row, col+1};
    } else if(charAtPosition == '<') {
        newPosition = new int[] {row, col-1};
    } else {
        throw new RuntimeException("Should never come in here.");
    }
    return newPosition;
}

检查给定位置是否有箭头

private static boolean isArrow(int[] position) {
    int row = position[0];
    int col = position[1];
    char charAtPosition = pathMatrix[row][col];
    return charAtPosition == 'V' || charAtPosition == '^' 
        || charAtPosition == '<' || charAtPosition == '>';
}

检查给定位置是否在矩阵内

private static boolean isInsideMatrix(int[] position) {
    int row = position[0];
    int col = position[1];
    return row >= 0 && row < NUMBER_OF_ROWS
        && col >= 0 && col < NUMBER_OF_COLUMNS;
}

打印矩阵

private static void printMatrix() {
    for(int row = 0; row < NUMBER_OF_ROWS; row++){
        for(int column = 0; column < NUMBER_OF_COLUMNS; column++){
            System.out.print(pathMatrix[row][column] + " ");
        }
        System.out.print('\n');
    }
}

这输出:

O V O ^ < <
O V V * ^ ^
* V * O * ^
O V O O V ^
O > > > > ^
Entrance: 0 1 V
0 1 V
1 1 V
2 1 V
3 1 V
4 1 >
4 2 >
4 3 >
4 4 >
4 5 ^
3 5 ^
2 5 ^
1 5 ^
0 5 <
0 4 <
0 3 ^
The path has been found in the maze above

0
投票

就像其他人已经说过的那样,尝试调试器或print-statements来测试你期望变量在执行中的不同点与实际值之间保持的值。在我看来,row==NUMBER_OF_ROWS不是你想要在决赛中测试的。在您的示例中,您需要在第一行退出。

© www.soinside.com 2019 - 2024. All rights reserved.