Reputation: 17268
public static boolean diagonals(char[][] b, int row, int col, int l) {
int counter = 1; // because we start from the current position
char charAtPosition = b[row][col];
int numRows = b.length;
int numCols = b[0].length;
int topleft = 0;
int topright = 0;
int bottomleft = 0;
int bottomright = 0;
for (int i=row-1,j=col-1;i>=0 && j>=0;i--,j--) {
if (b[i][j]==charAtPosition) {
topleft++;
} else {
break;
}
}
for (int i=row-1,j=col+1;i>=0 && j<=numCols;i--,j++) {
if (b[i][j]==charAtPosition) {
topright++;
} else {
break;
}
}
for (int i=row+1,j=col-1;i<=numRows && j>=0;i++,j--) {
if (b[i][j]==charAtPosition) {
bottomleft++;
} else {
break;
}
}
for (int i=row+1,j=col+1;i<=numRows && j<=numCols;i++,j++) {
if (b[i][j]==charAtPosition) {
bottomright++;
} else {
break;
}
}
return topleft + bottomright + 1 >= l || topright + bottomleft + 1 >= l; //in this case l is 5
}
After I was done posting the code above here, I couldn't help but wanted to simplify the code by merging the four pretty much the same loops into one method.
Here's the kind of method I want to have:
public int countSteps(char horizontal, char vertical) {
}
Two parameters horizontal
and vertical
can be either +
or -
to indicate the four directions to walk in. What I want to see if possible at all is i++;
is generalized to i horizontal horizontal;
when horizontal
taking the value of +
.
What I don't want to see is if
or switch
statements, for example:
public int countSteps(char horizontal, char vertical) {
if (horizontal == '+' && vertical == '-') {
for (int i=row-1,j=col+1;i>=0 && j<=numCols;i--,j++) {
if (b[i][j]==charAtPosition) {
topright++;
} else {
break;
}
}
} else if (horizontal == '+' && vertical == '+') {
for (int i=row+1,j=col+1;i>=0 && j<=numCols;i++,j++) {
if (b[i][j]==charAtPosition) {
topright++;
} else {
break;
}
}
} else if () {
} else {
}
}
Since it is as tedious as the original one. Note also that the comparing signs for the loop condition i>=0 && j<=numCols;
for example, >= && <=
have correspondence with the value combination of horizontal
and vertical
.
Sorry for my bad wording, please let me know if anything is not clear.
Upvotes: 0
Views: 83
Reputation: 47739
You can easily convert the loops to something like:
int doit(int i_incr, int j_incr) {
int cornerIncrement = 0;
for (int i=row+i_incr, j=col+j_incr; i>=0 && j>=0; i+=i_incr, j+=j_incr) {
if (b[i][j]==charAtPosition) {
cornerIncrement++;
} else {
break;
}
}
return cornerIncrement;
}
And then repeat 4 times...
int increment = doit(+1, -1); // Or (-1, +1) etc
topLeft += increment; // Or bottomLeft/topRight/bottomRight
Upvotes: 2
Reputation: 32343
So you have these two loops:
for (int i=row-1,j=col-1;i>=0 && j>=0;i--,j--) {
if (b[i][j]==charAtPosition) {
topleft++;
} else {
break;
}
}
for (int i=row-1,j=col+1;i>=0 && j<=numCols;i--,j++) {
if (b[i][j]==charAtPosition) {
topright++;
} else {
break;
}
}
First, turn your counters into an array, i.e. topleft
-> counter[0]
and topright
-> counter[1]
Then, turn the difference between the code into variables, so you have:
for(direction = 0; direction < 2; direction++) {
int offset = direction * 2 - 1; // This is what I mean by doing some math
for(int i=row-1,j=col+offset;i>=0 && -j*offset>=-numCols*direction;i--,j+=offset) {
if (b[i][j]==charAtPosition) {
counter[direction]++;
// etc.
The math can get ugly sometimes, or you can do it in a separate line. Look at my other post on this question for an elegant way to do the math in this particular problem using ? :
syntax.
Upvotes: 1