📄 battel.java
字号:
package dao;
import iniset.PropertySet;
import iniset.finals.SetDatas;
import java.awt.Cursor;
import java.awt.GridLayout;
import java.awt.Point;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.LinkedList;
import javax.swing.JLabel;
import dao.popinf.PopInf;
import view.CreatWindow;
import vo.ChessPoint;
import vo.CursorsAndImg;
import vo.Enums;
import vo.sounds.Sound;
/**
* 棋盘类,继承自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;
private PropertySet propertySet = null;
CreatWindow creatWindow = null;
/**
* 用于存放悔棋路径
*/
public LinkedList<ChessPoint> path = new LinkedList<ChessPoint>();
/**
* 是否已经分出胜负的变量
*/
boolean end;
/**
* 用来实现棋盘的按钮
*/
private ChessPoint[][] chPoint = new ChessPoint[15][15];
private Enums.chessState nowPlayer;
/**
* 构造函数
*/
public Battel(CreatWindow window) {
this.creatWindow = window;
this.setIcon(CursorsAndImg.BACK);
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 };
propertySet = PropertySet.getPropertySet();
// 初始化当前玩家
nowPlayer = propertySet.getFirst() == SetDatas.BLACK_FIRST ? Enums.chessState.BLACK
: Enums.chessState.WHITE;
}
// 以下为该类的其它非设置获取参数的函数
/**
* 电脑选择一个点下棋
*/
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(propertySet.getSound() == SetDatas.SOUND_ON) {
Sound.getSound().play(Sound.WIN);//播放玩家赢时的音效
}
PopInf.informBlackWin(this);
// 重新布置棋盘
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(propertySet.getSound() == SetDatas.SOUND_ON) {
if(propertySet.getModel() == SetDatas.PEOPLE_VS_COMPUTER){//如果人机对战则此时人输
Sound.getSound().play(Sound.LOSS);//播放玩家输时的音效
} else { //播放赢的音效
Sound.getSound().play(Sound.WIN);
}
}
PopInf.informWhiteWin(this);
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;
}
public Enums.chessState getNowPlayer() {
return nowPlayer;
}
public void setNowPlayer(Enums.chessState nowPlayer) {
this.nowPlayer = nowPlayer;
}
/**
* 获取当前棋盘按钮数组
*
* @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(CursorsAndImg.UN_CHESS_ABLE);
return;
} else {
setCursor(new Cursor(CursorsAndImg.HAND_CURSOR));
chessPoint.setIcon(CursorsAndImg.PUT_ABLE);
// 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(CursorsAndImg.DEFAULT_CURSOR));
return;
} else {
setCursor(new Cursor(CursorsAndImg.DEFAULT_CURSOR));
chessPoint.setIcon(CursorsAndImg.NONE_CHESS);
}
}
/**
* 鼠标在某个位置上单击了一下
*/
public void mouseClicked(MouseEvent e) {
ChessPoint chessPoint = (ChessPoint) e.getSource();
if (chessPoint.getState() != Enums.chessState.NONE) {// 如果当前点已经有子了
return;
} else if (propertySet.getModel() == SetDatas.PEOPLE_VS_COMPUTER) {// 人与电脑下模式
// if (SoundSet.sound == 1) {
// new Sound(Sound.PUTCHESS);
// }
if(propertySet.getSound() == SetDatas.SOUND_ON) {
Sound.getSound().play(Sound.PUTCHESS);//播放下棋的音效
}
chessPoint.setState(Enums.chessState.BLACK); // 下子
chessPoint.setIcon(CursorsAndImg.BLACK_CHESS);
creatWindow.addTxtTwo("玩家下棋: " + "坐标("
+ (chessPoint.getPoint().x + 1) + ", "
+ (chessPoint.getPoint().y + 1) + ");\n ");
// 加入到路径中去
path.offerLast(getchPoint()[chessPoint.getPoint().x][chessPoint
.getPoint().y]);
setCursor(CursorsAndImg.UN_CHESS_ABLE);
// 此处通知电脑下子
chosePoint();
if (end) {// 已分出胜负
return;
}
getchessPoint(new Point(i0, j0)).setState(Enums.chessState.WHITE);// 电脑下棋
getchessPoint(new Point(i0, j0)).setIcon(CursorsAndImg.WHITE_CHESS);
// 加入路径
path.offerLast(getchPoint()[i0][j0]);
creatWindow.addTxtTwo("电脑下棋: " + "坐标(" + (i0 + 1) + ", "
+ (j0 + 1) + ");\n");
evaluate();
} else if (propertySet.getModel() == SetDatas.PEOPLE_VS_PEOPLE) {// 人与人下模式
if (nowPlayer == Enums.chessState.BLACK) {
if(propertySet.getSound() == SetDatas.SOUND_ON) {
Sound.getSound().play(Sound.PUTCHESS);//播放下棋的音效
}
chessPoint.setState(Enums.chessState.BLACK);
chessPoint.setIcon(CursorsAndImg.BLACK_CHESS);
nowPlayer = Enums.chessState.WHITE;
creatWindow.addTxtTwo("黑方下棋: " + "坐标("
+ (chessPoint.getPoint().x + 1) + ", "
+ (chessPoint.getPoint().y + 1) + ");\n");
// 此处加入声音等设置
} else if (nowPlayer == Enums.chessState.WHITE) {
if(propertySet.getSound() == SetDatas.SOUND_ON) {
Sound.getSound().play(Sound.PUTCHESS);//播放下棋的音效
}
chessPoint.setState(Enums.chessState.WHITE);
chessPoint.setIcon(CursorsAndImg.WHITE_CHESS);
nowPlayer = Enums.chessState.BLACK;
creatWindow.addTxtTwo("白方下棋: " + "坐标("
+ (chessPoint.getPoint().x + 1) + ", "
+ (chessPoint.getPoint().y + 1) + ");\n");
// 此处加入声音等设置
}
path.offerLast(getchPoint()[chessPoint.getPoint().x][chessPoint
.getPoint().y]);
setCursor(CursorsAndImg.UN_CHESS_ABLE);
evaluate();
}
}
public void mouseReleased(MouseEvent e) {
// 空实现
return;
}
public void mousePressed(MouseEvent e) {
// 空实现
return;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -