I've been trying to implement simple program that returns valid move of Reversi / Othello game. Unfortunately, it doesn't work and I cannot really see why. It returns [2,2] which is certainly not valid move.
//myColor, opponentColor, Reversi move etc. are already defined.
I would be glad if you pointed out flaw in my system.
@Override
public ReversiMove makeNextMove(int[][] board) {
for (int y = 0; y < 8; y++) {
for (int x = 0; x < 8; x++) {
if (board[y][x] != myColor && board[y][x] != opponentColor) {
int nextX = x + 1;
while (nextX < 7 && board[y][nextX] == this.opponentColor) {
if (board[y][nextX + 1] == this.myColor) {
return new ReversiMove(y,x);
}
nextX++;
}
nextX = x - 1;
while (nextX > 0 && board[y][nextX] == this.opponentColor) {
if (board[y][nextX - 1] == this.myColor) {
return new ReversiMove(y,x);
}
nextX--;
}
int nextY = y + 1;
while (nextY < 7 && board[nextY][x] == this.opponentColor) {
if (board[nextY + 1][x] == this.myColor) {
return new ReversiMove(y,x);
}
nextY++;
}
nextY = y - 1;
while (nextY > 0 && board[nextY][x] == this.opponentColor) {
if (board[nextY - 1][x] == this.myColor) {
return new ReversiMove(y,x);
}
nextY--;
}
nextX = x + 1;
nextY = y + 1;
while (nextX < 7 && nextY < 7 && board[nextY][nextX] == this.opponentColor) {
if (board[nextY + 1][nextX + 1] == this.myColor) {
return new ReversiMove(y,x);
}
nextX++;
nextY++;
}
nextX = x - 1;
nextY = y - 1;
while (nextX > 0 && nextY > 0 && board[nextY][nextX] == this.opponentColor) {
if (board[nextY - 1][nextX - 1] == this.myColor) {
return new ReversiMove(y,x);
}
nextX--;
nextY--;
}
nextX = x + 1;
nextY = y - 1;
while (nextX < 7 && nextY > 0 && board[nextY][nextX] == this.opponentColor) {
if (board[nextY - 1][nextX + 1] == this.myColor) {
return new ReversiMove(y,x);
}
nextX++;
nextY--;
}
nextX = x - 1;
nextY = y + 1;
while (nextX > 0 && nextY < 7 && board[nextY][nextX] == this.opponentColor) {
if (board[nextY + 1][nextX - 1] == this.myColor) {
return new ReversiMove(y,x);
}
nextX--;
nextY++;
}
}
}
}
return new ReversiMove(-1, -1);
}
}
board[x][y] structure...
sometime refer as:
board[y][nextX] instead of board[nextX][y]