techplant
techplant

Reputation: 21

How to move an object around in a 2D Square

for (int i = 0; i < 5; i++){
  for (int j = 0; j < 5; j++){
    System.out.print(".");
  } 
  System.out.println();
}

To create by 5*5 square:

. . . . .
. . . . .
. . . . .
. . . . .
. . . . .

I would like to add a pointer onto the board, where I can receive input and traverse the pointer around the board. For example:

A . . . . 
. . . . .
. . . . .
. . . . .
. . . . .

Entering an input such as down would move my character downwards

. . . . . 
A . . . .
. . . . .
. . . . .
. . . . .

Right

. . . . . 
. A . . .
. . . . .
. . . . .
. . . . .

I understand I could use a Scanner scanner = new Scanner(System.in) to receive the input, and implement a switch to detect user input for 'up', 'down', 'left' & 'right'. However how would I go about implenting this movement for pointer A on the 5*5 square in java?

Upvotes: 2

Views: 96

Answers (1)

TheHappyBee
TheHappyBee

Reputation: 303

To answer your question, a Array. We will each "." inside the array and the such pointer A. Such that it is initialized as:

String[][] square = 
            {{"A",".",".",".","."},
            {".",".",".",".","."},
            {".",".",".",".","."},
            {".",".",".",".","."},
            {".",".",".",".","."}};

now, we make a new method move(String direction, String[][] square), in which we make "A" move according to a set of rules:

Left - 0 y, -1 x
Right - 0 y, +1 x
Up - -1 y, 0 x
Down- +1 y, 0 x 

To make:

public String[][] move(String direction, String[][] square) {
         if (direction.equals("left")) {
             try {
                 outerloop:
                for (int i = 0; i < 5; i++){
                    for (int j = 0; j < 5; j++){
                       if (square[i][j].equals("A")) {
                           String temp = square[i][j];
                           square[i][j] = square[i][j-1];
                           square[i][j-1] = temp;
                           break outerloop;
                           
                       }
                    }
                }
             } catch(Exception e) {
                 System.out.println("sorry you have reached the end of the square");
                 return square;
             }
         }
         else if (direction.equals("right")) {
             try {
                 outerloop:
                for (int i = 0; i < 5; i++){
                    for (int j = 0; j < 5; j++){
                       if (square[i][j].equals("A")) {
                           String temp = square[i][j];
                           square[i][j] = square[i][j+1];
                           square[i][j+1] = temp;
                           break outerloop;
                       }
                    }
                }
             } catch(Exception e) {
                 System.out.println("sorry you have reached the end of the square");
                 return square;
             }
         }
         else if (direction.equals("down")) {
             try {
                 outerloop:
                for (int i = 0; i < 5; i++){
                    for (int j = 0; j < 5; j++){
                       if (square[i][j].equals("A")) {
                           String temp = square[i][j];
                           square[i][j] = square[i+1][j];
                           square[i+1][j] = temp;
                           break outerloop;
                       }
                    }
                }
             } catch(Exception e) {
                 System.out.println("sorry you have reached the end of the square");
                 return square;
             }
         } else {
             try {
                 outerloop:
                for (int i = 0; i < 5; i++){
                    for (int j = 0; j < 5; j++){
                       if (square[i][j].equals("A")) {
                           String temp = square[i][j];
                           square[i][j] = square[i-1][j];
                           square[i-1][j] = temp;
                           break outerloop;
                       }
                    }
                }
             } catch(Exception e) {
                 System.out.println("sorry you have reached the end of the square");
                 return square;
             }
         }
         return square;
    }

Finally, initialize it during your loop and get square. Then use Arrays.deepToString() to print out the output.

Or to print in your format: get each item and print it out using the loop your mentioned above:

for (int i = 0; i < 5; i++){
  for (int j = 0; j < 5; j++){
    System.out.print(board[i][j] + "\n");
  } 
}

Note: remember to replace square with the new square returned in the method!

Upvotes: 2

Related Questions