0

迷路とナビゲーションの印刷にエラーがあります。ファイルからスキャンしたときだと思いますが、間違っている可能性があります。

ファイル入力

5 5
P.XX.
.X...
...X.
XXT..
..X..

私が見るもののサンプル

P P P P P 
X X X X X 
. . . . . 
. . . . . 

You may:
1) Move up
2) Move down
3) Move left
4) Move right
0) Quit

私が見るべきもの

P.XX.
.X...
...X.
XXT..
..X..

You may:
1) Move up
2) Move down
3) Move left
4) Move right
0) Quit

コード

import java.util.*;
import java.io.File;
public class MazeGame {

public static void main(String[] args) throws Exception {
Scanner scan = new Scanner(new File("maze.txt"));
Scanner user = new Scanner(System.in);
int rows = scan.nextInt();
int columns = scan.nextInt();
int px = 0;
int py = 0;
String [][] maze = new String[rows][columns];
//String junk = scan.nextLine();

for (int i = 0; i < rows; i++){
    String temp = scan.nextLine();
    String[] arrayPasser = temp.split("");
    for (int j = 0; j < columns; j++){
        maze[i][j] = arrayPasser[i];
    }
}

boolean gotTreasure = false;

while (gotTreasure == false){
    for (int i = 0; i < rows; i++){
        for (int j = 0; j < columns; j++){
            System.out.print(maze[i][j]);
            System.out.print(" ");
    }
        System.out.print("\n");
  }


    System.out.printf("\n");
    System.out.println("You may:");
    System.out.println("1) Move up");
    System.out.println("2) Move down");
    System.out.println("3) Move left");
    System.out.println("4) Move right");
    System.out.println("0) Quit");
    int choice = user.nextInt();
    int i = 0;

    if (choice == 1 && i >= 0 && i < columns){
        for (int k = 0; k < rows; k++){
            for (int l = 0; l < columns; l++){
                if (maze[k][l].equals(maze[px][py]) && maze[px][py-1].equals("X") == false){
                    maze[px][py] = ".";
                    maze[k][l-1] = "P";
                    maze[px][py] = maze[k][l-1];
                }else if (maze[px][py-1] == "X"){
                    System.out.println("Cannot move into a cave-in! Try something else.");
                }else {
                continue;}


                }
            }
        }
    else if (choice == 2 && i >= 0 && i < columns){
        for (int k = 0; k < rows; k++){
            for (int l = 0; l < columns; l++){
                if (maze[k][l].equals(maze[px][py]) && maze[px][py+1].equals("X") == false){
                    maze[px][py] = ".";
                    maze[k][l+1] = "P";
                    maze[px][py] = maze[k][l+1];
                }else if (maze[px][py+1] == "X"){
                    System.out.println("Cannot move into a cave-in! Try something else.");
                }else {
                continue;}

           }
         }
        }
    else if (choice == 3 && i >= 0 && i < columns){
        for (int k = 0; k < rows; k++){
            for (int l = 0; l < columns; l++){
                if (maze[k][l].equals(maze[px][py]) && maze[px-1][py].equals("X") == false){
                    maze[px][py] = ".";
                    maze[k-1][l] = "P";
                    maze[px][py] = maze[k-1][l];
                }else if (maze[px-1][py] == "X"){
                    System.out.println("Cannot move into a cave-in! Try something else.");
                }else {
                continue;}
            }
        }
    }
    else if (choice == 4 && i >= 0 && i < columns){
        for (int k = 0; k < rows; k++){
            for (int l = 0; l < columns; l++){
                if (maze[k][l].equals(maze[px][py]) && maze[px+1][py].equals("X") == false){
                    maze[px][py] = ".";
                    maze[k+1][l] = "P";
                    maze[px][py] = maze[k+1][l];
                }else if (maze[px+1][py] == "X"){
                    System.out.println("Cannot move into a cave-in! Try something else.");
                }else {
                continue;}
            }
        }
    }
    else if (choice == 0){
        System.exit(0);
    }
}

System.out.println("Congratulations, you found the treasure!");

scan.close();
user.close();
    }

}
4

2 に答える 2

0

この行:

maze[i][j] = arrayPasser[i];

次のようにする必要があります。

maze[i][j] = arrayPasser[j];

その理由は、からにiコピーしている間は一定のままであるため、同じ文字の行全体、つまり の行全体を取得することになります。arrayPassermaze(i, i)

于 2013-09-29T01:09:47.327 に答える
0

私は実際に同じ宿題の問題を抱えています。

しかし、2D 配列を文字列にする代わりに、2D char 配列として使用しています。(したがって、いくつかのキーワードを超えて大きな変化はありません)、最初の「パズル」を読み取って印刷する限り、完全に正常に機能します。

// Create maze in 2 dimensional character array.
    char[][] maze = new char[R][C];
    input1.nextLine();

    // Scan maze.
    for (int i = 0; i < R; i++) {
        String row = input1.next();
        for (int j = 0; j < C; j++)
            maze[i][j] = row.charAt(j);
    }
// Print Maze. (in your while loop)
        for (int i = 0; i < R; i++) {
            for (int j = 0; j < C; j++) {
                System.out.print(maze[i][j]);
                System.out.print(" ");
            }
            System.out.print("\n");
        }

それを試してみてください。

于 2013-10-03T00:10:31.920 に答える