I'm trying to write an Othello game.
I wrote some code to check if a square is a legal move:
// checks if black can move somewhere
static boolean isLegalMove(Board board, int squareNumber) {
int startX = squareNumber % 8, startY = squareNumber / 8;
if (board.boardColors[startX][startY] != 'e') return false;
char[] string = new char[80];
for (int dy = -1; dy <= 1; dy++)
for (int dx = -1; dx <= 1; dx++) {
// keep going if both velocities are zero
if (dy == 0 && dx == 0) continue;
string[0] = 0;
//////////////////////
for (int ctr = 1; ctr < 8; ctr++) {
int x = startX + ctr * dx;
int y = startY + ctr * dy;
if (x >= 0 && y >= 0 && x < 8 && y < 8) string[ctr - 1] = board.boardColors[x][y];
else string[ctr - 1] = 0;
}
///////////////////////
if (canMove(string)) return true;
}
return false;
}
Anyone know how I can make this code better?
Thank you.