package fivaGame;
import java.awt.GridLayout;
import java.awt.Image;
import java.awt.Toolkit;
import javax.swing.JLabel;
import javax.swing.ImageIcon;
import javax.swing.JOptionPane;
import java.awt.Point;
import java.awt.event.MouseListener;
import java.awt.event.MouseEvent;
import java.awt.Cursor;
import java.util.LinkedList;
import fivaGame.soundset.Sound;
import fivaGame.soundset.SoundSet;
/**
* 棋盘类,继承自JLabel,实现MouseListener接口,是本游戏的核心部分,负责事件处理 判断
*
* @author B.Lee
* @version 2008.05.10
*/
public class Battel extends JLabel implements MouseListener {
public int dirBeginX[];
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<ChessPoint> path = new LinkedList<ChessPoint>();
Image imageCurcor = new ImageIcon("icon/cannot.gif").getImage();
/**
* 鼠标不可用时形状
*/
Cursor cursor = Toolkit.getDefaultToolkit().createCustomCursor(imageCurcor,
new Point(16, 16), "cursor");
/**
* 是否已经分出胜负的变量
*/
boolean end;
/**
* 用来实现棋盘的按钮
*/
private ChessPoint[][] chPoint = new ChessPoint[15][15];
/**
* 构造函数
*/
public Battel() {
this.setIcon(new ImageIcon("icon/back.gif"));
this.setSize(540, 540);
this.setVisible(true);
GridLayout gridLayout = new GridLayout();
gridLayout.setRows(15);
gridLayout.setColumns(15);
gridLayout.setHgap(0);
gridLayout.setVgap(0);
this.setLayout(gridLayout);
for (int row = 0; row < 15; row++)
for (int col = 0; col < 15; col++) {
chPoint[row][col] = new ChessPoint(Enums.chessState.NONE,
new Point(row, col));
chPoint[row][col].addMouseListener(this);
this.add(chPoint[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 };
}
//以下为该类的其它非设置获取参数的函数
/**
* @return String 所下的路径点
*/
public String pathString(){
String ans = new String("");
for(int lengths = 0;lengths<path.size();lengths++){
if(lengths%2==0){//偶数,玩家下的棋
ans.concat("(" + (path.get(lengths).getPoint().x + 1) + ","
+ (path.get(lengths).getPoint().y + 1) + ") ");
}else{//奇数,电脑下的棋
ans.concat("<" + (path.get(lengths).getPoint().x + 1) + ","
+ (path.get(lengths).getPoint().y + 1) + "> ; ");
}
}
System.out.println("ans is" + ans);
return ans;
}
/**
* 电脑选择一个点下棋
*/
private void chosePoint() {
resetScore();
evaluate();
getMax();
}
/**
* 重新设置各点的分值
*/
private void resetScore() { // 重设分值
for (int row = 0; row < 15; row++)
for (int col = 0; col < 15; col++) {
if (getchessPoint(new Point(row, col)).getState() == Enums.chessState.NONE) {
getchessPoint(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 (getchessPoint(new Point(xx, yy)).getState() != Enums.chessState.NONE) {
m = 1;
state = getchessPoint(new Point(xx, yy))
.getState();
}
} else {
if (getchessPoint(new Point(xx, yy)).getState() == Enums.chessState.NONE)
continue;
if (state != getchessPoint(new Point(xx, yy))
.getState()) {
continue point;
}
m++;
}
}
// score
if (m == 0)// 当前点分数为零
continue;
if (m == 5) {
end = true;
if (state == Enums.chessState.BLACK) {
// 玩家赢
if (SoundSet.sound == 1) {
new Sound(Sound.WIN);
}
JOptionPane.showMessageDialog(this, "恭喜你获得胜利",
"分出胜负!", JOptionPane.WARNING_MESSAGE);
// 重新布置棋盘
for (int row = 0; row < 15; row++)
for (int col = 0; col < 15; col++) {
getchessPoint(new Point(row, col))
.setState(Enums.chessState.BLACK);
}
return;
} else {
if (SoundSet.sound == 1) {
new Sound(Sound.LOSS);
}
JOptionPane.showMessageDialog(this,
"你输掉了游戏", "分出胜负!",
JOptionPane.WARNING_MESSAGE);
for (int row = 0; row < 15; row++)
for (int col = 0; col < 15; col++) {
getchessPoint(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 (getchessPoint(new Point(xx, yy)).getState() == Enums.chessState.NONE) {
getchessPoint(new Point(xx, yy)).setScore(
getchessPoint(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 (getchessPoint(new Point(row, col)).getState() == Enums.chessState.NONE) {
if (max < getchessPoint(new Point(row, col)).getScore()) {
max = getchessPoint(new Point(row, col)).getScore();
i0 = row;
j0 = col;
}
}
}
}
if (i0 == -1 || j0 == -1) {
i0 = j0 = 15 / 2;
}
}
public boolean valid(int i, int j) {// 判断点是否在棋盘里
return i >= 0 && i < 15 && j >= 0 && j < 15;
}
/**
*
* @return 当前棋盘
*/
public Battel getBattel() {// 获取当前棋盘
return this;
}
/**
* 获取当前棋盘按钮数组
*
* @return 当前棋盘按钮数组
*/
public ChessPoint[][] getchPoint() {
return this.chPoint;
}
/**
* 当想要获取取一颗棋时调用
*
* @param argPoint
* 该点的坐标
* @return 所找点的引用
*/
public ChessPoint getchessPoint(Point argPoint) {// 反回某一个子
return getchPoint()[argPoint.x][argPoint.y];
}
/**
* 设置当前游戏状态
* @param argEnd 是否已经结束
*/
public void setEnd(boolean argEnd) {
end = argEnd;
}
public boolean getEnd(){
return end;
}
//以下加入事件处理
/**
* 当鼠标进入到某个棋子上方时调用
* 如果当前棋子已经下过棋了,则设置鼠标形状为不可用,否则设置为可用并在棋盘上标示
*/
public void mouseEntered(MouseEvent e) {
ChessPoint chessPoint = (ChessPoint) e.getSource();
if (chessPoint.getState() != Enums.chessState.NONE) {// 如果当前点已经有子了
setCursor(cursor);
return;
} else {
setCursor(new Cursor(Cursor.HAND_CURSOR));
chessPoint.setIcon(new ImageIcon("icon/chose.gif"));
// System.out.println(chessPoint.getPoint().x + "..."
// + chessPoint.getPoint().y);// 显示鼠标位置
}
}
/**
* 鼠标移出某个棋
*/
public void mouseExited(MouseEvent e) {
ChessPoint chessPoint = (ChessPoint) e.getSource();
if (chessPoint.getState() != Enums.chessState.NONE) {// 如果当前点已经有子了
setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
return
- 1
- 2
- 3
前往页