package smh.chess;
import java.io.BufferedReader;
import java.io.InputStreamReader;
public class OthelloChess
{
public OthelloChess()
{
chessBoard = new int[OthelloChess.SIZE][OthelloChess.SIZE];
chessBoard[3][3] = OthelloChess.WHITE;
chessBoard[4][4] = OthelloChess.WHITE;
chessBoard[3][4] = OthelloChess.BLACK;
chessBoard[4][3] = OthelloChess.BLACK;
}
public static void main(String[] args)
{
OthelloChess game = new OthelloChess();
while (!game.printBoard())
{
int x, y;
do
{
System.out.println("input x,y:");
String command = "";
try
{
command = (new BufferedReader(new InputStreamReader(
System.in))).readLine();
x = Integer.valueOf(command.split(",")[0]);
y = Integer.valueOf(command.split(",")[1]);
}
catch (Exception e)
{
x = -1;
y = -1;
}
} while (!game.play(x, y, OthelloChess.BLACK));
}
}
public int[][] getChessBoard()
{
return this.chessBoard;
}
public boolean play(int x, int y, int player)
{
if (!(x >= 0 && x < OthelloChess.SIZE && y >= 0
&& y < OthelloChess.SIZE && chessBoard[x][y] == 0))
{
System.out.println("You can't put the chess there!");
return false;
}
if (!canReverseOpposite(chessBoard, x, y, player))
{
System.out
.println("You must find a place to reverse at least one opposite's chess!");
return false;
}
chessBoard[x][y] = player;
for (int d = 0; d < 8; d++)
{
go(chessBoard, x, y, d, player, true);
}
System.out.println("The mark of the best position is "
+ findBestPosition(chessBoard, getOpposite(player), 0));
if (!canReverseOpposite(chessBoard, bestx, besty, getOpposite(player)))
{
System.out
.println("Surprising,the pc can't find a place to reverse your chess!");
System.out.println("Your turn again.");
}
else
{
chessBoard[bestx][besty] = getOpposite(player);
for (int d = 0; d < 8; d++)
{
go(chessBoard, bestx, besty, d, getOpposite(player), true);
}
System.out.println("pc: x=" + bestx + " y=" + besty);
}
return true;
}
private boolean canReverseOpposite(int[][] board, int x, int y, int player)
{
int mark = 0;
for (int d = 0; d < 8; d++)
{
mark += go(chessBoard, x, y, d, player, false);
}
if (mark > 0)
{
return true;
}
else
{
return false;
}
}
private int getOpposite(int player)
{
if (player == OthelloChess.BLACK)
{
return OthelloChess.WHITE;
}
else if (player == OthelloChess.WHITE)
{
return OthelloChess.BLACK;
}
else
{
// empty
return 0;
}
}
public boolean printBoard()
{
boolean end = true;
int counter = 0;
char ch;
System.out.println();
System.out.print(' ');
for (int i = 0; i < OthelloChess.SIZE; i++)
{
System.out.print(i);
}
System.out.println();
for (int i = 0; i < OthelloChess.SIZE; i++)
{
System.out.print(i);
for (int j = 0; j < OthelloChess.SIZE; j++)
{
if (chessBoard[i][j] == OthelloChess.BLACK)
{
ch = '*';
counter++;
}
else if (chessBoard[i][j] == OthelloChess.WHITE)
{
ch = '@';
}
else
{
ch = '_';
end = false;
}
System.out.print(ch);
}
System.out.println();
}
if (end)
{
String win = "";
int opposite = OthelloChess.SIZE * OthelloChess.SIZE
- counter;
if (counter > opposite)
{
win = "* win!";
}
else if (counter < opposite)
{
win = "@ win!";
}
else
{
win = "draw!";
}
System.out.println(win + " *:" + counter + " @:" + opposite);
}
return end;
}
private int findBestPosition(int[][] board, int player, int level)
{
int max = Integer.MIN_VALUE;
int betterx = 0;
int bettery = 0;
for (int i = 0; i < OthelloChess.SIZE; i++)
{
for (int j = 0; j < OthelloChess.SIZE; j++)
{
// must be a empty position
if (board[i][j] != 0)
{
continue;
}
// find the advantage in this position(i,j)
int advantage = 0;
for (int d = 0; d < 8; d++)
{
advantage += go(board, i, j, d, player, false);
}
if (advantage > 0)
{
advantage += this.getMarks(i, j);
betterx = i;
bettery = j;
int disadvantage = 0;
//find the best position of the opposite
if (level < OthelloChess.THINK_LEVEL)
{
board[betterx][bettery] = player;
disadvantage = findBestPosition(board,
getOpposite(player), level + 1);
board[betterx][bettery] = 0;
}
if (advantage - disadvantage > max)
{
max = advantage - disadvantage;
if (level == 0)
{
bestx = betterx;
besty = bettery;
}
}
}
}
}
return max;
}
private void initDirection(int direction)
{
int[][] directions = { { 1, 0 }, { 1, 1 }, { 0, 1 }, { -1, 1 },
{ -1, 0 }, { -1, -1 }, { 0, -1 }, { 1, -1 } };
if (direction < 0 || direction >= 8)
{
xplus = 0;
yplus = 0;
}
else
{
xplus = directions[direction][0];
yplus = directions[direction][1];
}
}
private int go(int[][] board, int x, int y, int direction, int player,
boolean turn)
{
int oldx = x, oldy = y;
initDirection(direction);
int oppsite = getOpposite(player);
int mark = 0, step = 0;
do
{
x += xplus;
y += yplus;
boolean isOpposite = (x >= 0) && (x < OthelloChess.SIZE)
&& (y >= 0) && (y < OthelloChess.SIZE)
&& (board[x][y] == oppsite);
if (!isOpposite)
{
break;
}
step++;
// the edge position will get high marks.
mark += getMarks(x, y);
} while (true)