JB1000
JB1000

Reputation: 1

User Input on a 2D Array (Java)

I am trying to implement the knights tour problem on a 3x4 grid display. My grid is displayed as follows:

   A B C D
1: 1 - - -
2: - - - -
3: - - - -

The user starts on A1, which is [0][0] on the 2D array. How would I associate my user input with other positions on the grid. For example, if the user was to type A2 (I'm aware this isn't a valid knight move) - how would I let the program know this is position [0][1] on the array (I think)?

Any help would be much appreciated.

Upvotes: 0

Views: 133

Answers (1)

Matteo Di Napoli
Matteo Di Napoli

Reputation: 577

Let's say you have index i, j for 2DArray[i][j]: then you can use i = rank - 1, and j = file - 65 exploiting char autocasting to int and ASCII int values to obtain the correct 2D array position.

Upvotes: 1

Related Questions