Contents

Character coordinates (with.Java)

   Apr 29, 2024     3 min read

This is an article about the “character coordinates (with.Java)” problem.

As I solve coding test problems, I look back on the problems I solved and look into different solution methods to get to know myself.

Let’s look at the problem first.

problem

I’m playing a stupid RPG game.

There are up, down, left, and right direction keys in the game, and pressing each key moves one space up, down, left, or right.

For example, if you press up in [0,0], the character’s coordinates will be [0, 1], if you press down, [0, -1], if you press left, [-1, 0], if you press right, the coordinates will be [0, 1]. 1, 0].

The array keyinput of the direction keys entered by the mage and the size of the map board are given as parameters.

When the character always starts at [0,0], please complete the solution function so that it returns the character’s coordinates [x, y] after all key inputs are completed.

[0, 0] is located in the exact center of the board.

For example, if the horizontal size of the board is 9, the character can move up to [-4, 0] to the left and up to [4, 0] to the right.

Restrictions

  • The board is given in the form of [horizontal size, vertical size].
  • The horizontal and vertical sizes of the board are odd.
  • Directional key input outside the size of the board is ignored.
  • 0 ≀ length of keyinput ≀ 50
  • 1 ≀ board[0] ≀ 99
  • 1 ≀ board[1] ≀ 99
  • Keyinput is always up, down, left, and right.

Input/Output Example

keyinputboardresult
[“left”, “right”, “up”, “right”, “right”][11, 11][2, 1]
[“down”, “down”, “down”, “down”, “down”][7, 9][0, -4]

My solution to the problem

class Solution {
     public int[] solution(String[] keyinput, int[] board) {
         int[] answer = new int[2];
         int maxX = board[0] / 2;
         int maxY = board[1] / 2;

         for (String direction: keyinput) {
             switch (direction) {
                 case "up":
                     if (answer[1] < maxY) {
                         answer[1]++;
                     }
                     break;
                 case "down":
                     if (answer[1] > -maxY) {
                         answer[1]--;
                     }
                     break;
                 case "left":
                     if (answer[0] > -maxX) {
                         answer[0]--;
                     }
                     break;
                 case "right":
                     if (answer[0] < maxX) {
                         answer[0]++;
                     }
                     break;
                 default:
                     break;
             }
         }

         return answer;
     }
}

Solution explanation

  • Coordinate movement: Iterates through the given direction array and performs coordinate movement in each direction.
  • Coordinate movement limit: Set the maximum range of movement based on the center of the given board. Limit coordinate movement by setting conditions to prevent movement out of range.
  • Return result: Returns the final moved coordinates as a result.

Code Advantages

  • Flexible direction input processing: Processing for each direction was implemented using a switch statement so that coordinates can be moved flexibly according to a given direction.
  • Coordinate movement limit setting: Coordinate movement is limited by setting the maximum range of movement based on the center of the board.

Code Disadvantages

  • Only considers limitations in the range of movement: Since the board size changes or various movement constraints are not considered, there may be a possibility of errors in certain situations.