import java.lang.*;
class Reversi
{
static class Mass
{
private Mass
(String str
) { this.
str = str
; } @Override
public String toString
() { return str
; }
public static final Mass NONE = new Mass("-");
public static final Mass BLACK = new Mass("X");
public static final Mass WHITE = new Mass("O");
public static Mass reverse(Mass m)
{
if (m == BLACK) return WHITE;
if (m == WHITE) return BLACK;
return m;
}
}
static final int BOARD_WIDTH = 8;
static final int BOARD_HEIGHT = 8;
static Mass turn;
static final Mass[][] board = new Mass[BOARD_HEIGHT + 2][BOARD_WIDTH + 2];
static void initBoard()
{
for (Mass[] line : board)
{
for (int i = 0; i < line.length; ++i)
{
line[i] = Mass.NONE;
}
}
board[4][4] = board[5][5] = Mass.BLACK;
board[4][5] = board[5][4] = Mass.WHITE;
turn = Mass.BLACK;
}
static void printBoard(boolean drawputable)
{
for (int i = 0; i < BOARD_WIDTH; ++i)
{
System.
out.
print((char)(i
+ 'A')); }
for (int h = 1; h <= BOARD_HEIGHT; ++h)
{
for (int w = 1; w <= BOARD_WIDTH; ++w)
{
if (drawputable && (putable(h, w, turn)))
{
}
else
{
System.
out.
print(" " + board
[h
][w
]); }
}
}
}
// require: called by putable() or put()
static boolean reverse
(int x, int y, int dx, int dy, Mass m, boolean checkonly)
{
int tx = x + dx;
int ty = y + dy;
Mass rv = Mass.reverse(m);
while (board[ty][tx] == rv)
{
tx += dx;
ty += dy;
}
if (board[ty][tx] != m) return false;
if ((tx == (x + dx)) && (ty == (y + dy))) return false;
if (checkonly) return true;
tx -= dx;
ty -= dy;
while (board[ty][tx] == rv)
{
board[ty][tx] = m;
tx -= dx;
ty -= dy;
}
return true;
}
static final boolean CHECK_ONLY = true;
static boolean putable(int x, int y, Mass m)
{
if ((x < 1) || (y < 1) || (x > BOARD_WIDTH) || (y > BOARD_HEIGHT))
{
return false;
}
if ((m != Mass.BLACK) && (m != Mass.WHITE)) return false;
if (board[y][x] != Mass.NONE) return false;
for (int dy = -1; dy <= 1; ++dy)
{
for (int dx = -1; dx <= 1; ++dx)
{
if (reverse(x, y, dx, dy, m, CHECK_ONLY))
{
return true;
}
}
}
return false;
}
static final boolean REVERSE = false;
static boolean put(int x, int y, Mass m)
{
if ((x < 1) || (y < 1) || (x > BOARD_WIDTH) || (y > BOARD_HEIGHT))
{
return false;
}
if ((m != Mass.BLACK) && (m != Mass.WHITE)) return false;
if (board[y][x] != Mass.NONE) return false;
board[y][x] = m;
for (int dy = -1; dy <= 1; ++dy)
{
for (int dx = -1; dx <= 1; ++dx)
{
reverse(x, y, dx, dy, m, REVERSE);
}
}
return true;
}
static void countup()
{
int b = 0;
int w = 0;
for (Mass[] line : board)
{
for (Mass mass : line)
{
if (mass == Mass.WHITE) ++w;
else if (mass == Mass.BLACK) ++b;
}
}
System.
out.
printf("B%2d - W%2d ", b, w
); if (b
> w
) System.
out.
println("Black won"); else if (b
< w
) System.
out.
println("White won"); else System.
out.
println("Drew"); }
{
initBoard();
// printBoard(true);
// System.out.println("-----------------");
Mass pass = null;
boolean f = true;
int n = 0;
while (f && (n < 65))
{
if (pass == turn)
{
pass = null;
}
f = false;
++n;
for (int y = 1; (!f) && (y <= BOARD_HEIGHT); ++y)
{
for (int x = 1; (!f) && (x <= BOARD_WIDTH); ++x)
{
if (putable(x, y, turn))
{
f = put(x, y, turn);
turn = Mass.reverse(turn);
}
}
}
if (!f)
{
if (pass != null)
{
break;
}
else
{
pass = turn;
turn = Mass.reverse(turn);
}
}
}
printBoard(false);
System.
out.
println("-----------------"); countup();
}
}