class ReversiStones { private int count; private byte turn; private byte current[][]; private boolean next[][] = new boolean[10][10]; public int count() { return count; } public byte turn() { return turn; } public boolean put(int x, int y) { if (next[x][y]) { current[x][y] = turn; count++; for (int i = 1; current[x - i][y - i] != 0; i++) { if (current[x - i][y - i] == turn) { while (i-- != 1) current[x - i][y - i] = turn; break; } } for (int i = 1; current[x - i][y] != 0; i++) { if (current[x - i][y] == turn) { while (i-- != 1) current[x - i][y] = turn; break; } } for (int i = 1; current[x - i][y + i] != 0; i++) { if (current[x - i][y + i] == turn) { while (i-- != 1) current[x - i][y + i] = turn; break; } } for (int i = 1; current[x][y - i] != 0; i++) { if (current[x][y - i] == turn) { while (i-- != 1) current[x][y - i] = turn; break; } } for (int i = 1; current[x][y + i] != 0; i++) { if (current[x][y + i] == turn) { while (i-- != 1) current[x][y + i] = turn; break; } } for (int i = 1; current[x + i][y - i] != 0; i++) { if (current[x + i][y - i] == turn) { while (i-- != 1) current[x + i][y - i] = turn; break; } } for (int i = 1; current[x + i][y] != 0; i++) { if (current[x + i][y] == turn) { while (i-- != 1) current[x + i][y] = turn; break; } } for (int i = 1; current[x + i][y + i] != 0; i++) { if (current[x + i][y + i] == turn) { while (i-- != 1) current[x + i][y + i] = turn; break; } } turn *= -1; if (!init()) { turn *= -1; if (!init()) return false; } } return true; } public boolean test(int x, int y) { return next[x][y]; } public boolean test(int x, int y, byte turn) { if (this.turn == turn) return next[x][y]; if (x != 0 && x != 9 && y != 0 && y != 9 && current[x][y] == 0) { for (int i = 1; current[x - i][y - i] != 0; i++) { if (current[x - i][y - i] == turn) { if (i != 1) return true; break; } } for (int i = 1; current[x - i][y] != 0; i++) { if (current[x - i][y] == turn) { if (i != 1) return true; break; } } for (int i = 1; current[x - i][y + i] != 0; i++) { if (current[x - i][y + i] == turn) { if (i != 1) return true; break; } } for (int i = 1; current[x][y - i] != 0; i++) { if (current[x][y - i] == turn) { if (i != 1) return true; break; } } for (int i = 1; current[x][y + i] != 0; i++) { if (current[x][y + i] == turn) { if (i != 1) return true; break; } } for (int i = 1; current[x + i][y - i] != 0; i++) { if (current[x + i][y - i] == turn) { if (i != 1) return true; break; } } for (int i = 1; current[x + i][y] != 0; i++) { if (current[x + i][y] == turn) { if (i != 1) return true; break; } } for (int i = 1; current[x + i][y + i] != 0; i++) { if (current[x + i][y + i] == turn) { if (i != 1) return true; break; } } } return false; } private boolean init() { boolean next = false; turn *= -1; for (int x = 1; x < 9; x++) { for (int y = 1; y < 9; y++) { if (this.next[x][y] = test(x, y, (byte)(turn * -1))) next = true; } } turn *= -1; return next; } public int get(int x, int y) { return current[x][y]; } public byte[][] get() { byte current[][] = new byte[10][10]; for (int x = 1; x < 9; x++) { for (int y = 1; y < 9; y++) { current[x][y] = this.current[x][y]; } } return current; } ReversiStones() { turn = 1; current = new byte[10][10]; current[4][4] = 1; current[4][5] = -1; current[5][4] = -1; current[5][5] = 1; init(); } ReversiStones(ReversiStones rs) { turn = rs.turn(); current = rs.get(); for (int x = 0; x < 10; x++) { for (int y = 0; y < 10; y++) { next[x][y] = rs.test(x, y); } } } public String toString() { String map = ""; for (int y = 0; y < 10; y++) { for (int x = 0; x < 10; x++) { map += current[x][y] != 0 ? current[x][y] == 1 ? "O" : "X" : next[x][y] ? turn == 1 ? "o" : "x" : " "; } map += "\n"; } return map; } }