Reputation: 35
I'm trying to understand this particular program but am having trouble understanding what this specific method does.
The method takes the param String playerShip
which is user entered and must be in the form (letter)(number) where the letters can range from a-g and numbers can range from 1-8, considering the ships will be placed on an 8x8 board. So an example of the string would be "A6", "E2" etc..
also private char[][] board = new char[8][8];
and pShip=0;
I know that this method will take the user entered string and try to place it onto the 8x8 board but where is the 49 and 65 coming from?
/**
* Sets a player ship on the board by replacing an 's' into board array
* user-defined coordinates, increments playerShips for each ship added
*
* @param playerShip coordinates of player ship entered by user
*/
public void setPlayerShips(String playerShip) {
board[((int) playerShip.charAt(1)) - 49][((int) playerShip.charAt(0)) - 65] = 's';
pShip++;
}
Upvotes: 0
Views: 107
Reputation: 46
This method is converting the given (letter-number) string coordinates into the corresponding position in the board matrix and setting it with 's' (which seems to be the defined value that indicates that there is part of a ship in that board position).
As others have mentioned, to map the given characters (the first, a letter character and the second, a number character) to values between 0 and 7 to index the board matrix, the programmer subtracted the ASCII value for the first character of the valid range. From A-H the ASCII characters code go from 65 (A) up to 72 (H) so when you pick a letter in that range and subtract 65 (the integer value of A, you obtain a value in the indexable range of the board matrix (0-7). Same thing applies to the second character that ranges from 1-8 and the ASCII codes for these characters range from 49 (for character 1) to 56 (for character 8) so the programmer subtracted 49 to obtain, once more, a number from 0-7 to index the other dimension of the board matrix.
I suggest you google for "ASCII table" so you can take a look at how printable and nonprintable characters of the ASCII coding are mapped to integer numbers as it widely used in all programming languages and a nice thing to understand some of the inner workings of string coding.
Upvotes: 0
Reputation: 16331
The programmer writing the code is simply converting a set of ASCII coordinates to integer offsets for use in the array. Unfortunately, there's nothing in the code that verifies that playerShip.charAt(1)
or 0 will return valid characters.
The reason that this works is that the ASCII code for a capital A is 0x41 or 65. Subtracting 65 from a capital letter results in an integer offset. I leave it to you to work out what 49 is in ASCII.
While at least one comment mentions that the developer chose to use a "magic number", which is almost always unwise, in this case I suspect that he assumed that any developer reading his code would be familiar with this approach to converting characters to integer values.
Upvotes: 1
Reputation: 123
These are ASCII codes: 49 is for 1 and 65 is for letter 'A'. The code is computing the difference between ascii value for each number and upper case letter at that step
Upvotes: 2