package cn.edu.ynu.main;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import cn.edu.ynu.assist.Screen;
import cn.edu.ynu.board.BitBoard;
import cn.edu.ynu.board.Board;
import cn.edu.ynu.board.DynamicBoard;
import cn.edu.ynu.engine.MoveNode;
import cn.edu.ynu.engine.SearchEngine;
import cn.edu.ynu.engine.LegalMoveNodes;
import cn.edu.ynu.exception.LostException;
import cn.edu.ynu.assist.Header;
import cn.edu.ynu.piece.Piece;
import cn.edu.ynu.piece.PieceBox;
import cn.edu.ynu.piece.PieceFactory;
import cn.edu.ynu.time.PlayerTimer;
import cn.edu.ynu.time.TimeRule;
@SuppressWarnings("serial")
public class ChessGameFrame extends JFrame {
private JButton setSysCfg, resetAll;
private JButton reverseBoard;
private JButton cmpMove;
private JButton turnBack;
private JButton preStep, nextStep;
private JTextField redTime, blackTime;
private JButton getConnect;
private JButton start;
private JPanel panel0, panel1, panel2, panel4;
private JPanel pieceMoveInfoPanel;
private Container content;
private Board board;
private int player = 0;
private boolean boardReversed = false;
private boolean computerIsThinking = false;
private final TimeRule timeRule = new TimeRule();
private SysCfgInfo sysCfg = new SysCfgInfo();
private Ready readyToPlay;
private boolean started = false;
private boolean isFirstStep = true;// 第一步
private int boardGridSize;// 棋板格子的大小
private int view = 0;// 视角0:红方在下方
private int spaceSize;
private DynamicBoard dynamicBoard;
private PieceBox pieceBox = new PieceBox();;
private Piece pieceIndex[] = new Piece[90];;// [90]
private PieceBox capturedPieceBox = new PieceBox();
private Piece lastSelectedPiece;
private LegalMoveNodes allCanMoveNodes;
private int[][] piecesTable = new int[90][90];
private Translation translation;
private SearchEngine searchEngine;
private String pieceMoveInfoStr;
private JTextArea blackPieceMoveInfo,redPieceMoveInfo;
private JScrollPane blackScrollPane,redScrollPane;
private JPanel noteInfoPanel;
private JTextArea noteInfo;
private LastNextTurnBackButtonListener lntLis = new LastNextTurnBackButtonListener();
private SysInfoButtonsListener sLis = new SysInfoButtonsListener();
public ChessGameFrame() {
super();
initialize();
showGUI();
}
private void initialize() {
JPanel contentPanel = new JPanel();
contentPanel.setLayout(null);
this.setContentPane(contentPanel);
initPiecesTable();
setBoardGridSize(50);
getDynamicBoard();
getSearchEngine();
initDynamicBoard();
getTranslation();
getBoard();
timeRule.setPlayerTimer(new TimeCounter());
sysCfg = new SysCfgInfo();
readyToPlay = new Ready();
allCanMoveNodes = new LegalMoveNodes();
}
public void initPiecesTable(){
for (int k = 0; k < 90; k++)
for (int j = 0; j < 90; j++)
piecesTable[k][j] = 0;
}
public void showGUI() {
JDialog.setDefaultLookAndFeelDecorated(true);
content = getContentPane();
this.setSize(new Dimension(getBoard().getWidth() + 265,
getBoard().getHeight() + 40));
this.setLocation(Screen.getLocationForCenter(getSize()));
getPanel1().setLocation(board.getWidth() + 15, 5);
getPanel0().setLocation(board.getWidth() + 15, 150);
getPanel4().setLocation(board.getWidth() + 15, 385);
getNoteInfoPanel().setLocation(board.getWidth() + 15, 460);
getPieceMoveInfoPanel().setLocation(board.getWidth() + 125, 5);
content.add(getPanel1());
content.add(getPanel0());
content.add(getPanel4());
content.add(getBoard());
content.add(getNoteInfoPanel());
content.add(getPieceMoveInfoPanel());
initBoardAndPieces();
displayAllPieces();
}
public void initDynamicBoard() {
getDynamicBoard().loadFen("rnbakabnr/9/1c5c1/p1p1p1p1p/9/9/P1P1P1P1P/1C5C1/9/RNBAKABNR w - - 0 1");
player = 0;
}
public void initBoardAndPieces() {
initPieces();
for (int i = 0; i < pieceBox.getSize(); i++)
content.add(pieceBox.getPiece(i));
content.add(board);
}
private void initPieces() {
PieceFactory.setPieceSize((int) (getBoardGridSize() * 0.9));
String s = dynamicBoard.getFenStr();
Piece tmpPiece;
int row = 9;
int col = 0;
for (int i = 0; i < 90; i++) {
pieceIndex[i] = null;
}
PieceMouseListener PML = new PieceMouseListener();
for (int i = 0; i < s.length() && row >= 0;) {
char tmpChar = s.charAt(i);
if (tmpChar >= '1' && tmpChar <= '9') {
i++;
col += tmpChar - '0';
}
if (s.charAt(i) == '/' || s.charAt(i) == ' ') {
row--;
col = 0;
} else {
tmpPiece = PieceFactory.createOnePiece(s.charAt(i), col, row);
tmpPiece.addMouseListener(PML);
pieceBox.add(tmpPiece);
pieceIndex[col * 10 + row] = pieceBox
.getPiece(pieceBox.getSize() - 1);
col++;
}
i++;
}
}
private void clearPieces() {
for (int i = 0; i < pieceIndex.length; i++) {
pieceIndex[i] = null;
}
Piece piece = null;
int length = pieceBox.getSize();
for (int i = 0; i < length; i++) {
if ((piece = pieceBox.remove(0)) != null) {
content.remove(piece);
}
}
}
public void reverseBoard() {
boardReversed = !boardReversed;
displayAllPieces();
}
public void displayAllPieces() {
for (int i = 0; i < pieceIndex.length; i++) {
displayOnePiece(pieceIndex[i]);
}
}
public void displayOnePiece(Piece piece) {
if (piece == null)
return;
setPieceLocation(piece);
piece.setVisible(true);
}
public void setPieceLocation(Piece piece) {
int x = piece.getCoordinateX();
int y = piece.getCoordinateY();
if (boardReversed) {
x = 8 - x;
y = 9 - y;
}
piece.setLocation((int) (spaceSize + (x - 0.45) * boardGridSize),
(int) (spaceSize + (9 - y - 0.45) * boardGridSize));
}
public boolean undo() {
int oldSrc = dynamicBoard.lastMove().src;
int oldDst = dynamicBoard.lastMove().dst;
int oldCap = dynamicBoard.lastMove().cap;
if (oldSrc < 0 || oldDst < 0) {
noteInfo.setText("撤销结束!");
return false;
}
dynamicBoard.undoMove();
noteInfo.setText("撤销操作!");
translation.moveCurrentToHis();
if (lastSelectedPiece != null) {
lastSelectedPiece.setBorderPainted(false);
}
lastSelectedPiece = pieceIndex[oldDst];
lastSelectedPiece.setBorderPainted(true);
changeLocation(lastSelectedPiece, oldSrc / 10, oldSrc % 10);
if (oldCap != 0) {
pieceIndex[oldDst] = capturedPieceBox.remove(capturedPieceBox.getSize() - 1);
pieceIndex[oldDst].setVisible(true);
}
return true;
}
public boolean redo() {
MoveNode tmpMove = translation.getHisMoveStruct();
if (tmpMove != null) {
int src = tmpMove.src;
int dst = tmpMove.dst;
movePiece(src, dst, false);
translation.moveHisToCurrent();
noteInfo.setText("还原操作!");
return true;
} else {
noteInfo.setText("还原结束!");
return false;
}
}
public void changeLocation(Piece srcPiece, int dstX, int dstY) {
int src = srcPiece.getCoordinate();
int dst = dstX * 10 + dstY;
if (pieceIndex[dst] != null) {
pieceIndex[dst].setVisible(false);
capturedPieceBox.add(pieceIndex[dst]);
pieceIndex[dst] = null;
}
pieceIndex[dst] = srcPiece;
pieceIndex[dst].setCoordinate(dstX, dstY);
setPieceLocation(pieceIndex[dst]);
pieceIndex[dst].setBorderPainted(true);
pieceIndex[src] = null;
}
public boolean movePiece(Piece piece, int dstX, int dstY, boolean needSave) {
if (piece == null)
return false;
int srcX = piece.getCoordinateX(), srcY = piece.getCoordinateY();
allCanMoveNodes.GenMoves(dynamicBoard, piecesTable);
MoveNode tmpMoveNode = null;
int tmpSrc, tmpDst, moveNum = allCanMoveNodes.MoveNum;
for (int i = 0; i < moveNum; i++) {
tmpSrc = allCanMoveNodes.MoveList[i].src;
tmpDst = allCanMoveNodes.MoveList[i].dst;
if (tmpSrc == srcX * 10 + srcY && tmpDst == dstX * 10 + dstY) {
BitBoard pieceBoard = (dynamicBoard.getPieceBits(piece
.getPieceType())).getLeftShift(0);// copy to
// board
tmpMoveNode = new MoveNode(tmpSrc, tmpDst);
if (dynamicBoard.movePiece(tmpMoveNode)) {
if (needSave)
translation.addMove
没有合适的资源?快使用搜索试试~ 我知道了~
JAVA实现的中国象棋游戏
共83个文件
class:37个
java:24个
jpg:14个
需积分: 10 11 下载量 41 浏览量
2009-04-17
18:49:08
上传
评论
收藏 138KB RAR 举报
温馨提示
这是本人用java实现的一个中国象棋小游戏,希望对一些网友带来帮助。
资源推荐
资源详情
资源评论
收起资源包目录
ChineseChessGame.rar (83个子文件)
ChineseChessGame
.project 392B
bin
cn
edu
ynu
main
SysCfgDialog$SysCfgActionListener.class 2KB
SysCfgDialog.class 6KB
SysCfgDialog$ModelRbListener.class 1KB
ChessGameFrame$PieceMouseListener.class 2KB
ChessGameFrame$LastNextTurnBackButtonListener.class 2KB
ChessGameFrame$TimeCounter.class 2KB
ChessGameFrame$BoardMouseListener.class 3KB
SysCfgInfo.class 1KB
Ready.class 1KB
ChessGameFrame$2.class 789B
ChessGameFrame.class 22KB
ChessGameFrame$SysInfoButtonsListener.class 2KB
Translation.class 14KB
ChessGameFrame$1.class 2KB
ChessGameApplication.class 784B
Translation$1.class 888B
piece
Piece.class 3KB
PieceBox.class 1KB
PieceFactory.class 2KB
exception
LostException.class 369B
assist
Header.class 879B
Screen.class 1KB
board
DynamicBoard.class 13KB
Board.class 6KB
BitBoard.class 4KB
time
TimeRule.class 3KB
Rule.class 561B
PlayerTimer.class 234B
engine
LegalMoveNodes.class 9KB
PositionEvalue.class 3KB
BookRecord.class 444B
GeneralMoveNodes.class 8KB
MoveNode.class 1KB
EngineOption.class 405B
SearchEngine.class 13KB
HashRecord.class 562B
KillerStruct.class 504B
image
5.jpg 2KB
3.jpg 2KB
8.jpg 2KB
4.jpg 2KB
12.jpg 2KB
9.jpg 2KB
2.jpg 2KB
10.jpg 2KB
pause.png 2KB
pre.png 2KB
reset.png 1KB
next.png 2KB
1.jpg 2KB
stop.png 2KB
11.jpg 2KB
6.jpg 2KB
run.png 2KB
0.jpg 2KB
7.jpg 2KB
13.jpg 2KB
src
cn
edu
ynu
main
ChessGameFrame.java 23KB
Ready.java 935B
ChessGameApplication.java 350B
Translation.java 15KB
SysCfgDialog.java 6KB
SysCfgInfo.java 794B
piece
Piece.java 2KB
PieceFactory.java 2KB
PieceBox.java 606B
exception
LostException.java 167B
assist
Header.java 539B
Screen.java 610B
board
DynamicBoard.java 17KB
Board.java 6KB
BitBoard.java 6KB
time
Rule.java 395B
TimeRule.java 2KB
PlayerTimer.java 200B
engine
PositionEvalue.java 3KB
MoveNode.java 958B
GeneralMoveNodes.java 13KB
SearchEngine.java 23KB
LegalMoveNodes.java 14KB
EngineOption.java 174B
.classpath 232B
共 83 条
- 1
资源评论
wang_zhou_jian
- 粉丝: 114
- 资源: 14
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- 论文(最终)_20240430235101.pdf
- 基于python编写的Keras深度学习框架开发,利用卷积神经网络CNN,快速识别图片并进行分类
- 最全空间计量实证方法(空间杜宾模型和检验以及结果解释文档).txt
- 5uonly.apk
- 蓝桥杯Python组的历年真题
- 2023-04-06-项目笔记 - 第一百十九阶段 - 4.4.2.117全局变量的作用域-117 -2024.04.30
- 2023-04-06-项目笔记 - 第一百十九阶段 - 4.4.2.117全局变量的作用域-117 -2024.04.30
- 前端开发技术实验报告:内含4四实验&实验报告
- Highlight Plus v20.0.1
- 林周瑜-论文.docx
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功