?? board.java
字號(hào):
import java.awt.Color;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.image.BufferedImage;
import java.io.File;
import javax.imageio.ImageIO;
/**
* @author Bengi Mizrahi
*
* To change this generated comment edit the template variable "typecomment":
* Window>Preferences>Java>Templates.
* To enable and disable the creation of type comments go to
* Window>Preferences>Java>Code Generation.
*/
public class Board {
static final int EMPTY = -1;
static final int WHITE = 0;
static final int BLACK = 1;
short liberties[][] = {{ 3, 5, 5, 5, 5, 5, 5, 3 },
{ 5, 8, 8, 8, 8, 8, 8, 5 },
{ 5, 8, 7, 6, 6, 7, 8, 5 },
{ 5, 8, 6, 5, 5, 6, 8, 5 },
{ 5, 8, 6, 5, 5, 6, 8, 5 },
{ 5, 8, 7, 6, 6, 7, 8, 5 },
{ 5, 8, 8, 8, 8, 8, 8, 5 },
{ 3, 5, 5, 5, 5, 5, 5, 3 }};
int[][] boardData;
int numOfPieces[];
int potential [];
public Player player [];
int playingSide;
public Board (){};
public Board(int beginnerSide, Player black, Player white) {
boardData = new int[8][8];
for (int i = 0; i < 8; i++)
for (int j = 0; j < 8; j++)
boardData[i][j] = EMPTY;
boardData[3][3] = boardData[4][4] = WHITE;
boardData[3][4] = boardData[4][3] = BLACK;
numOfPieces = new int[2];
numOfPieces[WHITE] = 2;
numOfPieces[BLACK] = 2;
potential = new int[2];
potential[WHITE] = 6;
potential[BLACK] = 6;
player = new Player [2];
player [WHITE] = white;
player [BLACK] = black;
playingSide = beginnerSide;
}
public Board myClone () {
Board b = new Board ();
b.liberties = new short [8][8];
b.boardData = new int [8][8];
for (int i = 0; i < 8; i++) {
for (int j = 0; j < 8; j++) {
b.liberties [i][j] = liberties [i][j];
b.boardData [i][j] = boardData [i][j];
}
}
b.numOfPieces = new int [2];
b.numOfPieces [Board.WHITE] = numOfPieces [Board.WHITE];
b.numOfPieces [Board.BLACK] = numOfPieces [Board.BLACK];
b.potential = new int [2];
b.potential [Board.WHITE] = potential [Board.WHITE];
b.potential [Board.BLACK] = potential [Board.BLACK];
b.player = new Player [2];
b.player [0] = player [0];
b.player [1] = player [1];
b.playingSide = playingSide;
return b;
}
public int getCell(int i, int j) {
return boardData[i][j];
}
public int getNumOfPieces(int side) {
return numOfPieces[side];
}
public int getPotential (int side) {
return potential [side];
}
public int evaluatePieceSuperiority (int side) {
return numOfPieces [side] -
numOfPieces [opponent (side)];
}
public int evaluatePotentialMobility (int side) {
return potential [side] - potential [opponent (side)];
}
public int evaluateCorners (int side) {
int WORTH_OF_A_CORNER = 20;
int w = 0;
int b = 0;
int [][] map = {
{0, 0},
{0, 7},
{7, 0},
{7, 7}
};
for (int i = 0; i < 4; i++) {
if (boardData [map [i][0]][map [i][1]] == Board.BLACK)
w++;
else if (boardData [map [i][0]][map [i][1]] == Board.WHITE)
b++;
}
if (side == BLACK) return WORTH_OF_A_CORNER * (b - w);
else return WORTH_OF_A_CORNER * (w - b);
}
public int evaluate (int side) {
return evaluateCorners (side) +
evaluateMobility (side) +
evaluatePotentialMobility (side);
}
public int evaluateMobility (int side) {
int w = 0;
int b = 0;
for (int i = 0; i < 8; i++) {
for (int j = 0; j < 8; j++) {
if (islegalMove (i, j, Board.WHITE)) {
w++;
}
if (islegalMove (i, j, Board.BLACK)) {
b++;
}
}
}
if (side == BLACK) return b - w;
else return w - b;
}
public static int opponent(int side) {
if (side == WHITE)
return BLACK;
else
return WHITE;
}
public void changeTurn() {
playingSide = opponent(playingSide);
}
public int getNumOfEmptyCells () {
return 64 - (numOfPieces [WHITE] + numOfPieces [BLACK]);
}
public boolean islegalMove(int row, int column, int who) {
int x, y;
boolean legal;
int step;
legal = false;
if (boardData[row][column] == EMPTY) // must be empty
{
for (int direction_x = -1; direction_x < 2; direction_x++)
for (int direction_y = -1; direction_y < 2; direction_y++) {
step = 0;
do {
step++;
x = column + step * direction_x;
y = row + step * direction_y;
} while (
(x >= 0)
&& (x < 8)
&& (y >= 0)
&& (y < 8)
&& (boardData[y][x] == opponent(who)));
if ((x >= 0)
&& (x < 8)
&& (y >= 0)
&& (y < 8)
&& (step > 1)
&& (boardData[y][x] == who)) {
legal = true;
}
}
}
return legal;
}
public void performMove(int row, int column ) {
int x, y;
boolean legal;
int step;
if (islegalMove(row, column, playingSide)){
boardData [row][column] = playingSide;
numOfPieces[playingSide]++;
potential[opponent(playingSide)] = potential[opponent(playingSide)] + liberties[row][column];
for (int f = -1; f <= 1; f++)
for (int g = -1; g <= 1; g++)
if ((f != 0 || g != 0) && (row + f)>=0 && (row + f)<8
&& (column + g)>=0 && (column + g)<8 ) {
if(liberties[row + f][column + g]>0)
liberties[row + f][column + g]--;
}
for (int direction_x = -1; direction_x < 2; direction_x++) {
for (int direction_y = -1; direction_y < 2; direction_y++) {
step = 0;
do {
step++;
x = column + step * direction_x;
y = row + step * direction_y;
} while (
(x >= 0) && (x < 8) && (y >= 0) && (y < 8) && (boardData[y][x] == opponent(playingSide)));
if ((x >= 0) && (x < 8) && (y >= 0) && (y < 8) && (step > 1) && (boardData[y][x] == playingSide)) {
for (int a = 1; a < step; a++) {
if (boardData[row + direction_y * a][column + direction_x * a] == opponent(playingSide)) {
numOfPieces[opponent(playingSide)]--;
numOfPieces[playingSide]++;
if (liberties[row + direction_y * a][column + direction_x * a] > 0) {
potential[opponent(playingSide)] = potential[opponent(playingSide)] + liberties[row + direction_y * a][column + direction_x * a];
potential[playingSide] = potential[playingSide] - liberties[row + direction_y * a][column + direction_x * a];
}
} else {
numOfPieces[playingSide]++;
}
boardData[row + direction_y * a][column + direction_x * a] = playingSide;
}
}
}
}
changeTurn();
}
}
public String toString () {
String s = "\n";
for (int i = 0; i < 8; i++) {
for (int j = 0; j < 8; j++) {
switch (boardData [i][j]) {
case Board.BLACK: s += "B"; break;
case Board.WHITE: s += "W"; break;
case Board.EMPTY: s += "-"; break;
}
}
s += "\n";
}
return s;
}
}
?? 快捷鍵說(shuō)明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -