## How to convert a chess co-ordinate into row, column for 2D array

how to assign values to two dimensional array in java

how to fill a 2d array java

initialize 2d array java

simple two-dimensional array program in java

two dimensional array java for loop

chess piece movement algorithm java

two dimensional array in c++

**Closed**. This question needs to be more focused. It is not currently accepting answers.

Since a chess board is well defined, another approach would be to use an enum. For example:

public static void main(String[] args) { ChessPosition cp = ChessPosition.valueOf("A1"); System.out.println(cp); cp = ChessPosition.valueOf("H8"); System.out.println(cp); } public enum ChessPosition { A1(0, 0), // ... H8(7, 7); private final int row; private final int column; private ChessPosition(int row, int column) { this.row = row; this.column = column; } public int getRow() { return row; } public int getColumn() { return column; } public String toString() { return name() + " row=" + getRow() + ", column=" + getColumn(); } }

**Getting the Number of Rows and Columns,** How do you find the number of rows and columns in a 2d array? Create a Chessboard with 2D Arrays Recently, I decided to write a chess program to experiment with AI, inspired by the ongoing World Chess Cup tournament held in Tbilsi, Georgia. I thought I could take some time today to share the foundation for my game – the chessboard.

The below code shows you how to do the transformation you need:

String str = "g3"; System.out.println(str.charAt(0) - 'a'); System.out.println(str.charAt(1) - '1');

will print

6 2

so
`str.charAt(0) - 'a'`

to transform the letter
`str.charAt(1) - '1'`

to transform the number

**Print Two Dimensional Array in Java,** . Some key things to notice about this code are: total is declared to be a double so that the result will be a double. [row,col] = ind2sub(sz,ind) returns the arrays row and col containing the equivalent row and column subscripts corresponding to the linear indices ind for a matrix of size sz. Here sz is a vector with two elements, where sz(1) specifies the number of rows and sz(2) specifies the number of columns.

First, consider that character code points are arranged alphabetically. Since characters in Java are represented as unsigned integers, you could subtract the code point of `'a'`

from another character to see how far it is from `'a'`

: `'a'-'a' = 0`

, `'b'-'a' = 1`

, `'c'-'a' = 2`

, and so on. Assuming that the first character of a two-character string is a lowercase letter in the a..h range, you can get your first "coordinate" like this:

int hPos = coord.charAt(0)-'a';

You can do the same thing for the digit:

int vPos = coord.charAt(1)-'1';

In addition, Java supplies a way to extract a digit from a numeric codepoint. Since `a..h`

are considered digits in base-18, you could also use this approach:

int hPos = Character.digit(coord.charAt(0), 18) - 10; int vPos = Character.digit(coord.charAt(1), 10) - 1;

**How to declare and Initialize two dimensional Array in Java with ,** of length 2. You can find the number of rows using numbers. As well as knowing the amount of rows & columns, such as 9 x 9. How do I convert an index for any point on the array into an x and y coordinate as if it were a 9x9 grid? For example, when provided with rows & columns 9x9 and index 10 should yield x: 2, y: 2 and 80 should yield x: 9, y: 9

**How to find the length of a 2D array in Python,** How do you find the length of a 2d array? Convert xy Coordinates to Matrix. Follow 500 views (last 30 days) John on 26 May 2013. (4,9) whose z value is 1 is shown as 1 in column 4 row 9 of the matrix.

**2d array chessboard java,** Access to a certain element that we say is in row 3 and column 5 would be: This is a 2-Dimensional Chessboard Continually ask the user for coordinates Always with 2D arrays and making a chess board. each "square" can change color, Create a 2d array of appropriate size. Use a for loop to loop over your 1d array. Inside that for loop, you'll need to figure out where each value in the 1d array should go in the 2d array. Try using the mod function against your counter variable to "wrap around" the indices of the 2d array. I'm being intentionally vague, seeing as this is

**Java ELearning Kit For Dummies,** Creating Multidimensional Arrays The arrays that you saw earlier in this chapter all have a single indicated by the fact that tables use both rows and columns. I was trying to do something similar. I found that when numpy created a structured array from an existing 2D array (using np.core.records.fromarrays), it considered each column (instead of each row) in the 2-D array as a record. So you have to transpose it. This behavior of numpy does not seem very intuitive, but perhaps there is a good reason

##### Comments

- Why not give it a try? See [askl], but coding questions should have a minimal reproducible example. Chess coordinates are always column-row pairs, and they will always be a single character for each, for a maximum of two characters. A naive approach would be to split the coords into a column and a row, and use that as indices to a 2D array (after transforming the column and row into actual indices -- how you do that is up to you, but an easy approach is to use the row as-is, and turn the column into an index via a simple A -> 1, B -> 2 transform. Be careful with 0- vs. 1- based indices!
- very useful! thank you so much!
- But how will this work for all the letters?(thanks for your response btw!)
- @MarinaCalder the method
`charAt(0)`

returns the char at the`0`

th index of the string, but when combined in an arithmetic operation like`charAt(0)-'a'`

returns the ASCII code of the char. So if the the char is`'a'`

then`charAt(0)-'a'->'a'-'a'->0`

, if the the char is`'b'`

then`charAt(0)-'a'->'b'-'a'->1`

,..., if the the char is`'h'`

then`charAt(0)-'a'->'h'-'a'->7`

. So it works for all the letters. - I cannot thank you enough for this explanation!
- I’m trying to convert any chess co ordinate from a user input