com.brettonw.game.Referee Maven / Gradle / Ivy
The newest version!
package com.brettonw.game;
import java.util.ArrayList;
import java.util.List;
public class Referee {
public static List getAvailableMoves (Board board) throws InvalidMoveException {
byte[] test = board.getBoard ();
ArrayList moves = new ArrayList<> (Board.SIZE);
for (int y = 0; y < Board.DIMENSION; ++y) {
for (int x = 0; x < Board.DIMENSION; x++) {
Move move = new Move (x, y);
int offset = move.getOffset ();
if (test[offset] == Player.EMPTY) {
moves.add (move);
}
}
}
return moves;
}
private static final byte[][] wins = {
{1, 1, 1, 0, 0, 0, 0, 0, 0, 3},
{0, 0, 0, 1, 1, 1, 0, 0, 0, 3},
{0, 0, 0, 0, 0, 0, 1, 1, 1, 3},
{1, 0, 0, 1, 0, 0, 1, 0, 0, 3},
{0, 1, 0, 0, 1, 0, 0, 1, 0, 3},
{0, 0, 1, 0, 0, 1, 0, 0, 1, 3},
{1, 0, 0, 0, 1, 0, 0, 0, 1, 3},
{0, 0, 1, 0, 1, 0, 1, 0, 0, 3},
{2, 2, 2, 0, 0, 0, 0, 0, 0, 6},
{0, 0, 0, 2, 2, 2, 0, 0, 0, 6},
{0, 0, 0, 0, 0, 0, 2, 2, 2, 6},
{2, 0, 0, 2, 0, 0, 2, 0, 0, 6},
{0, 2, 0, 0, 2, 0, 0, 2, 0, 6},
{0, 0, 2, 0, 0, 2, 0, 0, 2, 6},
{2, 0, 0, 0, 2, 0, 0, 0, 2, 6},
{0, 0, 2, 0, 2, 0, 2, 0, 0, 6}
};
public static byte[][] getWins () {
return wins;
}
public static int checkWinner (Board board) {
byte[] test = board.getBoard ();
for (byte[] win : wins) {
int maskedSum = 0;
for (int i = 0; i < Board.SIZE; ++i) {
maskedSum += test[i] & win[i];
}
if (maskedSum == win[Board.SIZE]) {
return win[Board.SIZE] / 3;
}
}
return Player.EMPTY;
}
}