public int dirBeginY[]; public int lineBeginX[]; public int lineBeginY[]; public int offsetX[]; public int offsetY[];
public int scoreBlack[] = { 0, 10, 50, 500, 10000 }; public int scoreWhite[] = { 0, 10, 50, 500, 10000 }; private int i0, j0; /**
* 用于存放悔棋路径 */
public LinkedList
Image imageCurcor = new ImageIcon(\ /**
* 鼠标不可用时形状 */
Cursor cursor =
Toolkit.getDefaultToolkit().createCustomCursor(imageCurcor, new Point(16, 16), \ /**
* 是否已经分出胜负的变量 */
boolean end; /**
* 用来实现棋盘的按钮 */
private CellPoint[][] cellsPoint = new CellPoint[15][15];
/**
* 构造函数 */
public Battel() {
this.setIcon(new ImageIcon(\ this.setSize(540, 540); this.setVisible(true);
GridLayout gridLayout = new GridLayout(); gridLayout.setRows(15); gridLayout.setColumns(15); this.setLayout(gridLayout);
for (int row = 0; row < 15; row++)
for (int col = 0; col < 15; col++) {
cellsPoint[row][col] = new
CellPoint(Enums.chessState.NONE,new Point(row, col));
cellsPoint[row][col].addMouseListener(this); this.add(cellsPoint[row][col]); }
dirBeginX = new int[] { 0, 0, 0, 1, 0, 14 }; dirBeginY = new int[] { 0, 0, 0, 0, 0, 1 }; lineBeginX = new int[] { 0, 1, 0, 1, 1, 0 }; lineBeginY = new int[] { 1, 0, 1, 0, 0, 1 }; offsetX = new int[] { 1, 0, 1, 1, -1, -1 }; offsetY = new int[] { 0, 1, 1, 1, 1, 1 }; }
public Battel(CellPoint[][] cellsPoint, LinkedList
this.setIcon(new ImageIcon(\ this.setSize(540, 540); this.setVisible(true);
GridLayout gridLayout = new GridLayout(); gridLayout.setRows(15); gridLayout.setColumns(15); this.setLayout(gridLayout); for(CellPoint cellPoint:path1) path.offerLast(cellPoint);
for (int row = 0; row < 15; row++)
for (int col = 0; col < 15; col++) { // cellsPoint[row][col] = new
CellPoint(Enums.chessState.NONE,new Point(row, col));
this.cellsPoint[row][col]=cellsPoint[row][col]; this.cellsPoint[row][col].addMouseListener(this);
if(cellsPoint[row][col].getState().equals(Enums.chessState.BLACK))
cellsPoint[row][col].setIcon(new ImageIcon(\ else
if(cellsPoint[row][col].getState().equals(Enums.chessState.WHITE)) cellsPoint[row][col].setIcon(new ImageIcon(\
this.add(cellsPoint[row][col]); }
dirBeginX = new int[] { 0, 0, 0, 1, 0, 14 };
dirBeginY = new int[] { 0, 0, 0, 0, 0, 1 }; lineBeginX = new int[] { 0, 1, 0, 1, 1, 0 }; lineBeginY = new int[] { 1, 0, 1, 0, 0, 1 }; offsetX = new int[] { 1, 0, 1, 1, -1, -1 }; offsetY = new int[] { 0, 1, 1, 1, 1, 1 }; } /**
* 电脑选择一个点下棋 */
private void chosePoint() { resetScore(); evaluate(); getMax(); } /**
* 重新设置各点的分值 */
private void resetScore() { // 重设分值 for (int row = 0; row < 15; row++)
for (int col = 0; col < 15; col++) {
if (getCellPoint(new Point(row, col)).getState() == Enums.chessState.NONE) {
getCellPoint(new Point(row, col)).setScore(0); } } } /**
* 判断并为各点打分 */
private void evaluate() {
for (int k = 0; k < 6; k++) {
int lineX = this.dirBeginX[k]; int lineY = this.dirBeginY[k];
line: for (; valid(lineX, lineY); lineX += lineBeginX[k], lineY += lineBeginY[k]) { int x = lineX; int y = lineY;
point: for (; valid(x, y); x += this.offsetX[k], y += this.offsetY[k]) {
int m = 0;
Enums.chessState state = Enums.chessState.NONE; for (int l = 0; l < 5; l++) {
int xx = x + l * this.offsetX[k]; int yy = y + l * this.offsetY[k];
if (!valid(xx, yy)) { continue line; }
if (state == Enums.chessState.NONE) { if (getCellPoint(new Point(xx, yy)).getState() != Enums.chessState.NONE) { m = 1;
state = getCellPoint(new Point(xx, yy)).getState();
} } else { if (getCellPoint(new Point(xx, yy)).getState() == Enums.chessState.NONE)
continue;
if (state != getCellPoint(new Point(xx, yy)).getState()) {
continue point; } m++; } }
// score
if (m == 0)// 当前点分数为零 continue; if (m == 5) { end = true;
if (state == Enums.chessState.BLACK) { // 玩家赢
JOptionPane.showMessageDialog(this, \恭喜你获得胜利\
\分出胜负!\JOptionPane.WARNING_MESSAGE);
// 重新布置棋盘
for (int row = 0; row < 15; row++)
for (int col = 0; col < 15; col++) { getCellPoint(new Point(row, col))
.setState(Enums.chessState.BLACK); } return; } else {
JOptionPane.showMessageDialog(this, \你输掉了游戏\分出胜负!\ JOptionPane.WARNING_MESSAGE);
for (int row = 0; row < 15; row++)
for (int col = 0; col < 15; col++) { getCellPoint(new Point(row, col))
.setState(Enums.chessState.BLACK); } return; } }
int sc = state == Enums.chessState.BLACK ? this.scoreBlack[m]
: this.scoreWhite[m]; for (int l = 0; l < 5; l++) {
int xx = x + l * this.offsetX[k]; int yy = y + l * this.offsetY[k];
if (getCellPoint(new Point(xx, yy)).getState() == Enums.chessState.NONE) {
getCellPoint(new Point(xx, yy)).setScore( getCellPoint(new Point(xx, yy)).getScore()
+ sc); } } } } } }
private void getMax() { float max = 0; i0 = j0 = -1;
for (int row = 0; row < 15; row++) { for (int col = 0; col < 15; col++) {
if (getCellPoint(new Point(row, col)).getState() == Enums.chessState.NONE) { if (max < getCellPoint(new Point(row, col)).getScore()) {
max = getCellPoint(new Point(row, col)).getScore();
i0 = row; j0 = col; } } }