public class Kyokumen 
{
	static final int NONE = 0;
	static final int BLACK = 1;
	static final int WHITE = 2;
	static final int OUT = 3;
	static final int[] DX = {0, 1, 1, 1, 0, -1, -1, -1};
	static final int[] DY = {-1, -1, 0, 1, 1, 1, 0, -1};

	private int[][] box = new int[10][10];
	private int nextTurn;

	public Kyokumen() {
		init();
	}

	public void init() {
		for (int x = 1; x <= 8; x++) {
			for (int y = 1; y <= 8; y++) {
				box[x][y] = NONE;
			}
		}
		for (int i = 0; i < 10; i++) {
			box[0][i] = OUT;
			box[9][i] = OUT;
			box[i][0] = OUT;
			box[i][9] = OUT;
		}
		box[4][4] = WHITE;
		box[4][5] = BLACK;
		box[5][4] = BLACK;
		box[5][5] = WHITE;
		nextTurn = BLACK;
	}

	int getState(int x, int y) {
		return box[x+1][y+1];
	}

	boolean isPosibleToPlace(int x, int y, int color) {
		x++;
		y++;
		if (box[x][y] == NONE) {
			for (int i = 0; i < 8; i++) {
				int wx = x+DX[i];
				int wy = y+DY[i];
				if (box[wx][wy] == otherSide(color)) {
					do {
						wx += DX[i];
						wy += DY[i];
					} while (box[wx][wy] == otherSide(color));
					if (box[wx][wy] == color) {
						return true;
					}
				}
			}
		}
		return false;
	}

	boolean isPosibleToPlace(int x, int y) {
		return isPosibleToPlace(x, y, nextTurn);
	}

	boolean isPosibleToPlace(int color) {
		for (int x = 1; x <= 8; x++) {
			for (int y = 1; y <= 8; y++) {
				if (isPosibleToPlace(x, y, color)) {
					return true;
				}
			}
		}
		return false;
	}

	boolean isPosibleToPlace() {
		return isPosibleToPlace(nextTurn);
	}

	boolean isEnd() {
		return (!isPosibleToPlace(BLACK) && !isPosibleToPlace(WHITE));
	}

	int otherSide(int color) {
		if (color == BLACK) return WHITE;
		else if (color == WHITE) return BLACK;
		return NONE;
	}

	boolean[][] getReversible(int x, int y, int color) {
		x++;
		y++;
		boolean[][] reversible = new boolean[8][8];
		for (int i = 0; i < 8; i++) {
			for (int j = 0; j < 8; j++) {
				reversible[i][j] = false;
			}
		}
		if (box[x][y] == NONE) {
			for (int i = 0; i < 8; i++) {
				int wx = x+DX[i];
				int wy = y+DY[i];
				if (box[wx][wy] == otherSide(color)) {
					do {
						wx += DX[i];
						wy += DY[i];
					} while (box[wx][wy] == otherSide(color));
					if (box[wx][wy] == color) {
						wx -= DX[i];
						wy -= DY[i];
						while (box[wx][wy] == otherSide(color)) {
							reversible[wx-1][wy-1] = true;
							wx -= DX[i];
							wy -= DY[i];
						}
					}
				}
			}
		}
		return reversible;
	}

	boolean[][] getReversible(int x, int y) {
		return getReversible(x, y, nextTurn);
	}

	void placeStone(int x, int y) {
		x++;
		y++;
		box[x][y] = nextTurn;
		for (int i = 0; i < 8; i++) {
			int wx = x+DX[i];
			int wy = y+DY[i];
			if (box[wx][wy] == otherSide(nextTurn)) {
				do {
					wx += DX[i];
					wy += DY[i];
				} while (box[wx][wy] == otherSide(nextTurn));
				if (box[wx][wy] == nextTurn) {
					wx -= DX[i];
					wy -= DY[i];
					while (box[wx][wy] == otherSide(nextTurn)) {
						box[wx][wy] = nextTurn;
						wx -= DX[i];
						wy -= DY[i];
					}
				}
			}
		}
		if (isPosibleToPlace(otherSide(nextTurn))) {
			nextTurn = otherSide(nextTurn);
		}
	}

	int countStone(int color) {
		int n = 0;
		for (int x = 1; x <= 8; x++) {
			for (int y = 1; y <= 8; y++) {
				if (box[x][y] == color) n++;
			}
		}
		return n;
	}
}

