简体   繁体   中英

Move diagonally in 2d array

I have my maze and can go north east south west but how do i move ne or sw. if possible can you guys give me examples using my code. thanks its a 4x4 array.

the user interface

    //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;

you need to char-parse your input

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;

example north-east:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM