在2d阵列中对角移动

时间:2013-11-15 09:58:33

标签: java arrays

我有迷宫,可以往东南偏西,但我怎么移动ne或sw。 如果可能,你们可以使用我的代码给我示例。感谢它的4x4阵列。

用户界面

    //Begin user dialog
    System.out.println("Welcome");
    input ="";
    while(!input.equals("quit"))
    {
       System.out.println(map.rooms[row][col].name);
       System.out.print(">");
       input = scan.nextLine().toLowerCase();

        switch (input) {
            case "n":
                if(map.rooms[row][col].isValidExit("n"))
                    row--;
                else
                    System.out.println("You cant go that way");
                break;
            case "s":
                 if(map.rooms[row][col].isValidExit("s"))
                    row++;
                else
                    System.out.println("You cant go that way");
                 break;
            case "w":
                if(map.rooms[row][col].isValidExit("w"))
                    col--;
                else
                    System.out.println("You cant go that way");
                break;
            case "e":
                if(map.rooms[row][col].isValidExit("e"))
                    col++;
                else
                    System.out.println("You cant go that way");
                break;

2 个答案:

答案 0 :(得分:1)

你需要对你的输入进行解析

while(!input.equals("quit"))
{
   System.out.println(map.rooms[row][col].name);
   System.out.print(">");
   input = scan.nextLine().toLowerCase();
   char[] inputArray = input.toCharArray();

   for(char c : inputArray){

    switch (input) {
        case "n":
            if(map.rooms[row][col].isValidExit("n"))
                row--;
            else
                System.out.println("You cant go that way");
            break;
        case "s":
             if(map.rooms[row][col].isValidExit("s"))
                row++;
            else
                System.out.println("You cant go that way");
             break;
        case "w":
            if(map.rooms[row][col].isValidExit("w"))
                col--;
            else
                System.out.println("You cant go that way");
            break;
        case "e":
            if(map.rooms[row][col].isValidExit("e"))
                col++;
            else
                System.out.println("You cant go that way");
            break;

答案 1 :(得分:0)

示例东北:

    case "ne":
        if(map.rooms[row][col].isValidExit("ne")) {
            col++;
            row--;
        }
        else {
            System.out.println("You cant go that way");
        }
        break;