Vertical, horizontal and diagonal assignment of a two-dimensional boolean matrix
To be honest, I'm probably thinking too much about this, but if you have an array boolean[8][8]
and a true value inside that, how would you make all the horizontal, vertical and diagonal values true?
For example, given [X], how do I make all the other X values:
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
Now I can do 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;}
}
}
}
source to share
First of all, in your loop, you need a detach variable as soon as you find the first true value. I would suggest boolean isFound as one of the conditions in your for loop. Other than that, here's how I would do it (NOTE: this will 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. For the first diagonal, I check that its position is within the array (I'll explain how I define position a bit). Secondly, I define the starting X position and Y value of each diagonal in parentheses. Finally, I find positions by moving the K-links in the X and Y directions (step by step) from the original position to cross the grid with the diagonal. The same is repeated for the diagnosis pointing in a different way, but the X value is K, subtracted rather than added, since the diagonal points to the opposite direction. The exact logic for starting position and movement can best be found by playing with the location or plotting my algorithm.
Placing (note that I added to the variable to make sure I stopped after finding 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;
}
}
}
source to share
Here's a complete working example that solves this problem. See the comments in the code for more details:
public class Boolean_Array
{
private static final int SIZE = 8;
public static void main(String[] args)
{
// create the boolean array
boolean [][] boolArr = new boolean [SIZE][SIZE];
// 1. Set the row, col of the true
int row = 3;
int col = 5;
// 2. Make the vertical, horizontal and diagonals true
for (int i = 0; i < SIZE; i++)
{
// Do the vertical and horizontal
boolArr[row][i] = true;
boolArr[i][col] = true;
// Do the diagonals
setDiagonol(boolArr, row - i, col - i, i); // up and to the left
setDiagonol(boolArr, row - i, col + i, i); // up and to the right
setDiagonol(boolArr, row + i, col - i, i); // down and to the left
setDiagonol(boolArr, row + i, col + i, i); // down and to the right
}
print(boolArr);
}
private static void setDiagonol (boolean [][] boolArr, int row, int col, int i)
{
try
{
boolArr[row][col] = true;
}
catch (java.lang.ArrayIndexOutOfBoundsException aioobe)
{
// catching for convenience so we don't have to check the bounds
}
}
private static void print (boolean [][] boolArr)
{
for (int i = 0; i < SIZE; i++)
{
for (int j = 0; j < SIZE; j++)
{
System.out.print(boolArr[i][j] == true ? "X " : "0 ");
}
System.out.println();
}
}
}
Output
0 X 0 0 0 X 0 0
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
source to share
Main class
public static void main(String[] args) {
printGrid(3, 2);
System.out.println();
printGrid(5, 4);
System.out.println();
printGrid(7, 0);
System.out.println();
}
public static void printGrid(int x, int y) {
boolean[][] grid = new boolean[8][8];
for (int i = 0; i < grid.length; i++) {
for (int l = 0; l < grid[0].length; l++) {
// horizontal and vertical
if (x == i || y == l) {
grid[i][l] = true;
}
// diagonals
if (Math.abs(x - i) == Math.abs(y - l)) {
grid[i][l] = true;
}
System.out.print(String.format("%-2s", grid[i][l] == true ? "X" : "O"));
}
System.out.println();
}
}
Output
X O X O X O O O
O X X X O O O O
X X X X X X X X
O X X X O O O O
X O X O X O O O
O O X O O X O O
O O X O O O X O
O O O O X O O O
X O O O X O O O
O X O O X O O X
O O X O X O X O
O O O X X X O O
X X X X X X X X
O O O X X X O O
O O X O X O X O
X O O O O O O X
X O O O O O X O
X O O O O X O O
X O O O X O O O
X O O X O O O O
X O X O O O O O
X X O O O O O O
X X X X X X X X
source to share