I have an exercise. An int[8][8] chess grid. I have to find out if white can take a black. The input is: (lowercaps = black, upper = white)
tc.drf.t
ppp.pppp
...p...c
.....f..
..C..P..
..P.D.P.
PP.....P
T.F.RFCT
For the qeen/towers, I used a loop to check in each direction (top/bottom/left/right) and now have 4 simple loop functions that looks more or less the same. I would like to have only one function but can't manage to find how. Any idea?
static boolean attackRowRt(String[] board, int y, int fromX){
for(int x=fromX+1; x<=7; x++){
char attacked = board[y].charAt(x);
if(attacked == '.') continue;
return Character.isLowerCase(attacked);
}
return false;
}
static boolean attackRowLt(String[] board, int y, int fromX){
for(int x=fromX-1; x>=0; x--){
char attacked = board[y].charAt(x);
if(attacked == '.') continue;
return Character.isLowerCase(attacked);
}
return false;
}
static boolean attackColBtm(String[] board, int x, int fromY){
for(int y=fromY+1; y<=7; y++){
char attacked = board[y].charAt(x);
if(attacked == '.') continue;
return Character.isLowerCase(attacked);
}
return false;
}
static boolean attackColTop(String[] board, int x, int fromY){
for(int y=fromY-1; y>=0; y--){
char attacked = board[y].charAt(x);
if(attacked == '.') continue;
return Character.isLowerCase(attacked);
}
return false;
}