Clipping Edges in a bitboard - java

I am using bitboards to generate attack tables for rooks, bishops, and queens. During my research of magic bitboards, it seems I need to clip the edges of all of my attack tables. I store my bitboards in a long, with square a1 being the LSB and square h8 being the MSB, moving row by row. Here is an example of an attack table for a rook on square c2:
[0, 0, 1, 0, 0, 0, 0, 0]
[0, 0, 1, 0, 0, 0, 0, 0]
[0, 0, 1, 0, 0, 0, 0, 0]
[0, 0, 1, 0, 0, 0, 0, 0]
[0, 0, 1, 0, 0, 0, 0, 0]
[0, 0, 1, 0, 0, 0, 0, 0]
[1, 1, 0, 1, 1, 1, 1, 1]
[0, 0, 1, 0, 0, 0, 0, 0]
My problem is that I need to make all the ones on the edge of the bitboard 0s. At first I tried &ing each attack table with a binary long representing a border clip that looks like this:
[0, 0, 0, 0, 0, 0, 0, 0]
[0, 1, 1, 1, 1, 1, 1, 0]
[0, 1, 1, 1, 1, 1, 1, 0]
[0, 1, 1, 1, 1, 1, 1, 0]
[0, 1, 1, 1, 1, 1, 1, 0]
[0, 1, 1, 1, 1, 1, 1, 0]
[0, 1, 1, 1, 1, 1, 1, 0]
[0, 0, 0, 0, 0, 0, 0, 0]
Now this works for most squares in the center, but for attack tables on the edges it clips it in error. SO for a rook on h8:
Initial attack table:
[1, 1, 1, 1, 1, 1, 1, 0]
[0, 0, 0, 0, 0, 0, 0, 1]
[0, 0, 0, 0, 0, 0, 0, 1]
[0, 0, 0, 0, 0, 0, 0, 1]
[0, 0, 0, 0, 0, 0, 0, 1]
[0, 0, 0, 0, 0, 0, 0, 1]
[0, 0, 0, 0, 0, 0, 0, 1]
[0, 0, 0, 0, 0, 0, 0, 1]
Expected Result:
[0, 1, 1, 1, 1, 1, 1, 0]
[0, 0, 0, 0, 0, 0, 0, 1]
[0, 0, 0, 0, 0, 0, 0, 1]
[0, 0, 0, 0, 0, 0, 0, 1]
[0, 0, 0, 0, 0, 0, 0, 1]
[0, 0, 0, 0, 0, 0, 0, 1]
[0, 0, 0, 0, 0, 0, 0, 1]
[0, 0, 0, 0, 0, 0, 0, 0]
Actual Result:
[0, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 0, 0]
How should i approach this so that it only clips the end of each attack ray? I realize my bit logic is off. Thanks.

Instead of masking out the bits at the border using a constant mask, use a dynamically generated mask.
Example, the rook (♜) is on field F6, so we generate a mask where F1, F8, A6, and H6 are set. Then we use that mask to turn of those bits in your attack pattern.
0 0 0 0 0 1 0 0 8
0 0 0 0 0 0 0 0 7
1 0 0 0 0 ♜ 0 1 6
0 0 0 0 0 0 0 0 5
0 0 0 0 0 0 0 0 4
0 0 0 0 0 0 0 0 3
0 0 0 0 0 0 0 0 2
0 0 0 0 0 1 0 0 1
A B C D E F G H
In this solution I assume that you encode the 64 squares row-wise inside a long. The square A1 is the LSB and the square H8 is the MSB. Therefore above example would be represented by 2305984846213677088L = 0x2000810000000020L = 0b0000010000000000000100000010000000000000000000000000000000000100000L
/**
* #param x 0-based index for the columns A to H (0=A_, 1=B_, ...)
* #param y 0-based index for the rows 1 to 8 (0=_1, 1=_2, ...)
*/
long rookEndpoints(int x, int y) {
long topBtm = 1L << x;
return (0b10000001L << (8 * y)) | (topBtm << (8 * 7)) | topBtm;
}
...
attackPattern &= ~rookEndpoints(x, y)

Related

Filling an array with all combinations of formula N^R

For a homework question i need to fill an array with all combinations of the formula N^R. The variable R is constant and is 6. The variable N is not constant and let's say it's 2. So 2^6 = 64. Now what i need is an array with all the combinations (in this case 64). I found a website which does exactly what i need and the output in this case should be:
[0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 1],
[0, 0, 0, 0, 1, 0],
[0, 0, 0, 0, 1, 1],
[0, 0, 0, 1, 0, 0],
[0, 0, 0, 1, 0, 1],
[0, 0, 0, 1, 1, 0],
[0, 0, 0, 1, 1, 1],
[0, 0, 1, 0, 0, 0],
[0, 0, 1, 0, 0, 1],
[0, 0, 1, 0, 1, 0],
[0, 0, 1, 0, 1, 1],
[0, 0, 1, 1, 0, 0],
[0, 0, 1, 1, 0, 1],
[0, 0, 1, 1, 1, 0],
[0, 0, 1, 1, 1, 1],
[0, 1, 0, 0, 0, 0],
[0, 1, 0, 0, 0, 1],
[0, 1, 0, 0, 1, 0],
[0, 1, 0, 0, 1, 1],
[0, 1, 0, 1, 0, 0],
[0, 1, 0, 1, 0, 1],
[0, 1, 0, 1, 1, 0],
[0, 1, 0, 1, 1, 1],
[0, 1, 1, 0, 0, 0],
[0, 1, 1, 0, 0, 1],
[0, 1, 1, 0, 1, 0],
[0, 1, 1, 0, 1, 1],
[0, 1, 1, 1, 0, 0],
[0, 1, 1, 1, 0, 1],
[0, 1, 1, 1, 1, 0],
[0, 1, 1, 1, 1, 1],
[1, 0, 0, 0, 0, 0],
[1, 0, 0, 0, 0, 1],
[1, 0, 0, 0, 1, 0],
[1, 0, 0, 0, 1, 1],
[1, 0, 0, 1, 0, 0],
[1, 0, 0, 1, 0, 1],
[1, 0, 0, 1, 1, 0],
[1, 0, 0, 1, 1, 1],
[1, 0, 1, 0, 0, 0],
[1, 0, 1, 0, 0, 1],
[1, 0, 1, 0, 1, 0],
[1, 0, 1, 0, 1, 1],
[1, 0, 1, 1, 0, 0],
[1, 0, 1, 1, 0, 1],
[1, 0, 1, 1, 1, 0],
[1, 0, 1, 1, 1, 1],
[1, 1, 0, 0, 0, 0],
[1, 1, 0, 0, 0, 1],
[1, 1, 0, 0, 1, 0],
[1, 1, 0, 0, 1, 1],
[1, 1, 0, 1, 0, 0],
[1, 1, 0, 1, 0, 1],
[1, 1, 0, 1, 1, 0],
[1, 1, 0, 1, 1, 1],
[1, 1, 1, 0, 0, 0],
[1, 1, 1, 0, 0, 1],
[1, 1, 1, 0, 1, 0],
[1, 1, 1, 0, 1, 1],
[1, 1, 1, 1, 0, 0],
[1, 1, 1, 1, 0, 1],
[1, 1, 1, 1, 1, 0],
[1, 1, 1, 1, 1, 1]
I have tried realising this with for loops, but without succes.
I don't want the full code of an algorithm that makes this possible, but
I want to be helped on my way. Thanks in advance.
I've come up with this solution, which is a bit clumsy but should probably work for your case, the comments should explain everything:
public static void printCombinations(int R, int N) {
// calculate the combinations
String[][] combinations = calculateCombinations(R, N);
// iterate over all
for (int i = 0; i < combinations.length; i++) {
// prints the commas at the end
if (i != 0) {
System.out.println(',');
}
// print to std out
System.out.print(Arrays.toString(combinations[i]));
}
System.out.println();
}
public static String[][] calculateCombinations(int R, int N) {
// calculate our limit
int limit = (int) StrictMath.pow(N, R);
// create the result array
String[][] result = new String[limit][R];
// iterate over all possibilities
for (int i = 0; i < limit; i++) {
// convert to base
String base = Long.toString(i, N);
// holds our temporary value
StringBuilder intermediate = new StringBuilder(R);
// pad the value from the start with zeroes if needed
for (int sub = R - base.length(); sub > 0; sub--) {
intermediate.append('0');
}
// append our number
intermediate.append(base);
// append to result
result[i] = intermediate.toString().split("");
}
// return the result
return result;
}
And can then be called like this to pretty print it out:
printCombinations(6, 2);
Or to get it as a result:
String[][] result = calculateCombinations(6, 2);
Running Demo

For Loop Map Overwrites Previous Value

Essentially I am making a board game using sockets. Each client connected is stored in a map .
When the user makes a move to a coordinate in the 2D Array i.e. "move 2,3", it should put the connectionID in that position.
Currently my issue is, because I have a for loop; when i use the move command, the connection ID is replaced with the last value in the loop.
public void move(int x, int y) {
for (int value : gs.returnClients().values()) {
storeArray[x][y] = value;
}
}
i.e. If i have 2 clients connected: {62029=1, 62032=2} and my board
[[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 0, 0, 1, 0]
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 2, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]]
Let's say I want to have client 1 move to 0, 3 it should be:
[[0, 0, 0, 1, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 0, 0, 1, 0]
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 2, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]]
But instead I get:
[[0, 0, 0, 2, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 0, 0, 1, 0]
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 2, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]]
Since it's overwriting the last value of the map in the for loop.
How would I make it move dependent on its client "id" from the map?
Edit
In my server i have clients.put(socket.getPort(), connectionID); and methods to return the map and id. I have a class GameService that executes command which is
switch (request.type) {
case MOVE:
int clientID = Integer.parseInt(request.params[0]);
int getX = Integer.parseInt(request.params[1]);
int getY = Integer.parseInt(request.params[2]);
game.move(clientID,getX, getY);
return game.returnBoard();
In a file Request
String[] items = line.trim().split("\\s+");
switch (items[0].toUpperCase()) {
case "MOVE":
return new Request(RequestType.MOVE, items[1], items[2], items[3]);
As far as I understand your code, you need to do your move the following way:
public void move(int client, int x, int y) {
storeArray[x][y] = client;
}
where client is one (!) of the numbers within the gs.returnClients().values()-list. If you need to take it out of that map, you need to deliver the appropriate key for that, e.g.:
public void move(int clientId, int x, int y) {
storeArray[x][y] = gs.returnClients().get(clientId);
}

Convert Binary Array to Char Java

I have this binary array:
int[] bitArray = {
0,1,0,1,0,1,0,0,0,1,1,0,1,0,0,0,0,1,1,0,0,1,0,1,0,0,1,0,0,0,0,0,0,1,1,1,0,0,1,1,
0,1,1,1,0,1,0,1,0,1,1,0,1,1,1,0,0,0,1,0,0,0,0,0,0,1,1,0,1,0,0,1,0,1,1,1,0,0,1,1,
0,0,1,0,0,0,0,0,0,1,1,0,1,0,0,1,0,1,1,0,1,1,1,0,0,0,1,0,0,0,0,0,0,1,1,1,0,1,0,0,
0,1,1,0,1,0,0,0,0,1,1,0,0,1,0,1,0,0,1,0,0,0,0,0,0,1,1,1,0,0,1,1,0,1,1,0,1,0,1,1,
0,1,1,1,1,0,0,1,};
It makes the phrase: The sun is in the sky
How would you convert the int binary array to a char?
Every eight bits make up a char. You could just loop over the bits and accumulate every eight together:
int[] bitArray = {0, 1, 0, 1, 0, 1, 0, 0, 0, 1, 1, 0, 1, 0, 0, 0, 0, 1, 1, 0, 0, 1, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 1, 1, 0, 1, 1, 1, 0, 1, 0, 1, 0, 1, 1, 0, 1, 1, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 1, 0, 1, 0, 0, 1, 0, 1, 1, 1, 0, 0, 1, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 1, 0, 1, 0, 0, 1, 0, 1, 1, 0, 1, 1, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 1, 0, 0, 0, 1, 1, 0, 1, 0, 0, 0, 0, 1, 1, 0, 0, 1, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 1, 1, 0, 1, 1, 0, 1, 0, 1, 1, 0, 1, 1, 1, 1, 0, 0, 1,};
char[] chars = new char[bitArray.length / 8];
for (int i = 0; i < chars.length; ++i) {
int c = 0;
for (int j = i * 8; j < (i + 1) * 8; ++j) {
c = c << 1;
c += bitArray[j];
}
chars[i] = (char)c;
}
String s = new String(chars);
System.out.println(s);
Loop over every byte (8 bits) of the array, create a string of those bits, and then convert those bits to an integer using Integer.parseInt(x,2) and then cast that to a character and added to the result.
public static void main (String[] args) throws java.lang.Exception
{
int[] bitArray = {0,1,0,1,0,1,0,0,0,1,1,0,1,0,0,0,0,1,1,0,0,1,0,1,0,0,1,0,0,0,0,0,0,1,1,1,0,0,1,1,0,1,1,1,0,1,0,1,0,1,1,0,1,1,1,0,0,0,1,0,0,0,0,0,0,1,1,0,1,0,0,1,0,1,1,1,0,0,1,1,0,0,1,0,0,0,0,0,0,1,1,0,1,0,0,1,0,1,1,0,1,1,1,0,0,0,1,0,0,0,0,0,0,1,1,1,0,1,0,0,0,1,1,0,1,0,0,0,0,1,1,0,0,1,0,1,0,0,1,0,0,0,0,0,0,1,1,1,0,0,1,1,0,1,1,0,1,0,1,1,0,1,1,1,1,0,0,1,};
int CHAR_SIZE = 8;
String T = "";
String result ="";
for (int i=0; i<bitArray.length; i+= CHAR_SIZE)
{
for (int j=0; j<CHAR_SIZE; j++)
T += Integer.toString(bitArray[i+j]);
result += Character.toString((char)Integer.parseInt(T, 2));
T="";
}
System.out.println(result);
}
output
The sun is in the sky
ASCII chars are one byte. One byte is eight bits. Separate the array elements in segments of 8 (0-7), (8-15), ...
Store these segments in another array. Use the following constructor to finish.
public String(byte[] bytes,
Charset charset)
Remember to specify utf8 as your charset. This causes the chars to be treated as one-byte ASCII chars.

How to print very long (100000 items) array of integers to stdout

I'm trying to output some very long (100000 items) array of integers to stdout.
public int myPrint(int[] A) {
System.out.println("A " + Arrays.toString(A));
}
It fails because stdout on the machine the code runs cuts the string from some point like (this is whole output, as you can see there is not 100000 items but much less):
A [-1, -1, 0, -1, 1, -1, 0, -1, 1, -1, 0, 0, -1, -1, 0, -1, 0, -1, -1, 0, 1, -1, -1, 1, -1, 1, 0, -1, 1, 1, 0, 1, -1, 0, 0, 1, -1, 0, 1, 0, 1, 0, 0, 1, 0, 1, 0, -1, 1, -1, 1, 1, 0, -1, 0, 0, -1, 0, 0, 0, -1, -1, 0, -1, 0, -1, 0, 1, 1, 0, 1, 1, 0, -1, 0, -1, 1, 0, 1, -1, -1, -1, -1, 0, -1, 0, 0, -1, -1, 0, -1, 0, -1, 0, -1, 1, 0, 1, -1, 0, 1, 0, 0, 0, 1, 1, 0, 1, 1, 0, -1, -1, 1, -1, 0, 0, -1, 0, 1, 1, 0, -1, 1, 0, 0, 1, 0, -1, -1, 1, 0, 1, 1, 1, 1, -1, 1, -1, 0, 1, 0, -1, 0, 0, 0, 1, 0, -1, 0, -1, -1, 0, 1, -1, -1, -1, 1, 1, -1, 0, -1, -1, 0, 0, 1, 1, 0, 1, 0, 1, 1, -1, 1, 0, 0, 0, -1, 0, -1, 0, 1, 1, 0, 0, 1, -1, -1, 1, 0, 1, 0, 0, 1, 0, 1, 1, -1, 1, -1, 0, 0, 1, -1, 0, 0, 1, -1, -1, 1, -1, 1, -1, 0, -1, 0, 1, -1, 0, -1, 0, 0, -1, 1, 0, 1, -1, 1, -1, -1, 0, 1, 0, -1, 1, -1, 1, 1, -1, 1, 0, 1, 1, 1, -1, 0, 1, -1, 0, 1, -1, -1, -1, 0, 0, 0, 0, -1, -1, 0, 1, 1, 1, -1, 1, 0, -1, -1, 0, 0, -1, -1, 1, -1, 1, 0, 1, -1, 0, 1, 0, 0, 0, -1, -1, -1, -1, 1, -1, 1, -1, 0, 0, -1, 1, 1, 1, 0, -1, -1, -1, 1, 1, -1, -1, 0, -1, -1, -1, 0, 0, 0, 0, 1, 0, -1, -1, 1, 1, -1, 0, 1, 0, -1, 0, -1, -1, 1, -1, 1, 1, 0, -1, -1, 1, 1, -1, -1, 0, 1, -1, 1, -1, -1, -1, 0, 1, 0, -1, -1, 0, -1, 1, 0, 1, 1, 1, -1, 0, 1, -1, -1, -1, 0, 1, 1, -1, -1, 1, -1, -1, 1, 1, 0, 1, 1, 1, -1, 0, 1, 0, 0, 1, 1, 0, 0, 1, -1, 0, 1, 0, -1, 1, 0, 0, -1, 0, 1, -1, 0, -1, -1, 0, 1, -1, 1, 1, -1, -1, 1, 1, 1, 1, 1, 0, -1, 1, 0, -1, 1, -1, -1, -1, -1, 0, 0, 0, 0, -1, 0, 1, -1, 1, 0, 1, 0, 0, -1, -1, -1, -1, 1, 1, 1, 1, 0, 1, 1, -1, -1, -1, 1, 1, -1, 1, -1, -1, -1, 1, 1, -1, -1, 1, 0, 0, 0, 0, -1, -1, 0, 1, 0, -1, -1, -1, 1, 0, 0, 0, 1, 1, -1, 0, 0, 1, -1, 0, -1, 0, 1, 0, 0, 0, 1, 1, -1, -1, 0, -1, -1, 1, -1, 0, -1, 1, 1, 1, 1, 0, 0, -1, 0, 1, 0, -1, -1, -1, 1, -1, 1, 0, 0, 0, -1, 0, -1, 0, 0, 0, 1, 1, 1, 0, -1, 1, 1, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, -1, 0, 0, 0, 1, 0, -1, 0, 0, 0, 0, 0, -1, -1, 0, -1, 0, 1, 0, 1, 1, 0, 0, -1, 0, 0, 0, 1, 0, 0, 1, 1, 1, 0, 0, -1, 1, 1, 1, 1, -1, 1, -1, 1, -1, 1, 1, -1, 0, -1, 1, -1, 1, 0, -1, -1, 0, 1, 0, -1, 0, 1, 0, -1
The code is remotely executed and I don't have option to change stdout formatting/buffer size etc. on the machine so it must be done inside the method.
Maybe I should split the string into sub strings?
Do not use Arrays.toString here. But output immediate values.
Maybe with extra flushing, as done with a println.
public int myPrint(int[] A) {
System.out.print("A [");
for (int i = 0; i < A.length; ++i) {
if (i % 25 == 0) {
System.out.println(); // Or flush()
}
if (i != 0) {
System.out.print(", ");
}
System.out.print(A[i];
}
System.out.println("]");
}
int width = 80;
int c = 0;
for(int i: A){
String item = i + " ";
c += item.length();
System.out.print(c);
if(c>width){
System.out.println();
c=0;
}
}
Only prints 80 or so characters to the terminal before adding a new line.

Multidimensional arrays: Java. How does this code work?

I've been following a tutorial about creating a basic tile-based tower defense game in Java and have encountered a piece of code I cannot wrap my brain around and would like some help. (My main question is at the bottom after the code)
At this point we are iterating through a multidimensional array of 0's and 1's that we pass to a constructor which has a method that assigns a grass tile for 0's and stone tile for 1's and then another method to draw them to the screen creating our game screen. Simple enough, right?
Here is the class:
package data;
import static helpers.Artist.*;
public class TileGrid {
public Tile[][] map;
public TileGrid(int[][] newMap){
map = new Tile[20][15];
for(int i = 0; i < map.length; i++){
for(int j = 0; j <map[i].length; j++){
switch(newMap[j][i]){
case 0:
map[i][j] = new Tile(row * 64, col * 64, 64, 64, TileType.GRASS);
break;
case 1:
map[i][j] = new Tile(row * 64, col * 64, 64, 64, TileType.STONE);
break;
case 2:
map[i][j] = new Tile(row * 64, col * 64, 64, 64, TileType.WATER);
break;
}
}
}
}
public void Draw(){
for(int i = 0; i < map.length; i++){
for(int j = 0; j < map[i].length; j++){
Tile t = map[i][j];
DrawQuadTex(t.getTexture(), t.getX(), t.getY(), t.getWidth(), t.getHeight());
}
}
}
}
And here is the array we are passing in:
int[][] map = { //20 tiles wide, 15 tiles high
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0},
{0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0},
{0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 2, 2, 2, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 2, 2, 2, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 1, 0, 0, 0, 0, 0, 0, 2, 2, 2, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 1, 0, 0, 0, 0, 0, 0, 2, 2, 2, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 1, 0, 0, 0, 0, 0, 2, 2, 2, 2, 2, 0, 0, 0, 0, 0, 0, 0},
};
So my main question has to do with the switch statement in the constructor for the TileGrid.
Why do the i and the j get switched newMap[j][i] when checking what it equals? I get that this code works, well, because it does and I understand nested for loops to iterate through a multidimensional array.
But why wouldn't newMap[i][j] work?
As well at the very beginning of that same constructor why do we create an array (of type Tile) with the dimension of map = new Tile[20][15] when we are passing in an array with the dimensions of map[15][20]?
I have tried to figure this out and study this on my own and will continue to do so until I understand it but any help would be soooo appreciated!
You are passing to the TileGrid constructor a 2D array of 15 rows and 20 columns, but inside the constructor you create a 2D array of 20 rows and 15 columns. That's why map[i][j] corresponds with newMap[j][i].
If the input int[][] map was also of 20 rows and 15 columns, you wouldn't have to switch the order of the indices inside the constructor.
This would give an index out of bounds error if it was really switched. However, there is a col variable in the second for loop that is not defined anywhere so it's hard to tell what's going on. This code will not compile.
If it is some code pulled off a a tutorial on some web page, I would just assume that it was a typo and continue on with that in mind. OR better yet, contact the author.

Categories