Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

2D Boolean Array Vertical, Horizontal, and Diagonal Assignment

Tags:

java

arrays

Honestly I'm probably overthinking this, but if you have a boolean[8][8] array and a value of true within that, how would you make all horizontal, vertical, and diagonal values true as well?

For example, given the [X], how do I make all of the other values X as well:

0 0 X 0 0  X  0 0
0 0 0 X 0  X  0 X
0 0 0 0 X  X  X 0
X X X X X [X] X X
0 0 0 0 X  X  X 0
0 0 0 X 0  X  0 X
0 0 X 0 0  X  0 0
0 X 0 0 0  X  0 0

Right now I am able to do the vertical and horizontal:

for(int i = 0; i < 8; i++){
    for (int l = 0; l < 8; l++){
        if (boolean[i][l]){
            for (int k = 0; k < 8; k++){
                boolean[i][k] = true;
                boolean[k][l] = true;}
            }
        }
   }

1 Answers

First off, in your loop, you need a break away variable once you find the first true value. I'd suggest a boolean isFound as one of the conditions in your for loop. Aside from that, here is how I'd go about it (NOTE: This would be placed right below the vertical/horizontal in your loop):

//positive slope diagonal
if(((i - Math.min(i, l)) + k) < 8 && ((l - Math.min(i, l)) + k) < 8)
    testArray[(i - Math.min(i, l)) + k][(l - Math.min(i, l)) + k] = true;

//negative slope diagonal
if((k) < 8 && ((l + i) - k) >= 0 && ((l + i) - k) < 8)
    testArray[k][(l + i) - k] = true;

In this example, the two diagonals are split up. For the first diagonal, I check to make sure its position is within the bounds of the array (I'll explain how i determine the position in a bit.) Secondly, I determine the starting position's X and Y value of each diagonal, located in the parentheses. Lastly, I find the positions by moving K units in the X and Y direction (step by step) from the starting position to traverse the grid with the diagonal. The same is repeated for the diagnose pointing the other way, but the X value has K subtracted, not added, as as the diagonal points to the opposite direction. Exact logic for the starting position and movement can be best found by toying with the location or drawing my algorithm out.

Eg. enter image description here

Placement (Note that I added in variable to make sure to stop after finding the one true value):

    boolean notFound = true;

    for(int i = 0; i < 8 && notFound; i++){
        for (int l = 0; l < 8 && notFound; l++){
            if (testArray[i][l]){
                for (int k = 0; k < 8; k++){
                    testArray[i][k] = true;
                    testArray[k][l] = true;

                    if(((i - Math.min(i, l)) + k) < 8 && ((l - Math.min(i, l)) + k) < 8)
                        testArray[(i - Math.min(i, l)) + k][(l - Math.min(i, l)) + k] = true;

                    if((k) < 8 && ((l + i) - k) >= 0 && ((l + i) - k) < 8)
                        testArray[k][(l + i) - k] = true;       
                }
                notFound = false;
             }
         }
     }
like image 183
Drone6251 Avatar answered Mar 15 '26 03:03

Drone6251



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!