在路上

 找回密码
 立即注册
在路上 站点首页 学习 查看内容

Java棋类游戏实践之中国象棋

2016-8-29 13:12| 发布者: zhangjf| 查看: 608| 评论: 0

摘要: 本文实例讲述了java实现的中国象棋游戏代码,分享给大家供大家参考,具体代码如下 一、实践目的: 1.鼠标点击、拖动等事件的应用与区别 2.棋谱文件的保存与读取 3.完善象棋的规则。 二、实践内容: 中国象棋历史悠久 ...

本文实例讲述了java实现的中国象棋游戏代码,分享给大家供大家参考,具体代码如下

一、实践目的:

1.鼠标点击、拖动等事件的应用与区别

2.棋谱文件的保存与读取

3.完善象棋的规则。

二、实践内容:

中国象棋历史悠久,吸引了无数的人研究,现对中国象棋的对战和实现棋谱的制作做如下的设计和说明,供大家参考学习。

1、机机对弈,红方先手。在符合规则的情况下拖动棋子到目的地,松鼠标落子。


人人对弈图

2、制作棋谱,选择制作棋谱菜单后,对弈开始,并记录了下棋过程。


选择“制作棋谱”菜单



棋谱制作完毕红方胜出

一方胜出后弹出胜利消息对话框。点击确定后,选择“保存棋谱”菜单,弹出保存文件对话框。


保存棋谱对话框

3.演示棋谱,选择演示棋谱菜单后,弹出打开对话框,选择保存好的棋谱,开始演示。


演示棋谱对话框


演示棋谱过程(自动和手动两种)

三、参考代码:

1.象棋主类 文件ChineseChess.java

  1. package cn.edu.ouc.chineseChess;
  2. import javax.swing.*;
  3. import java.awt.*;
  4. import java.awt.event.*;
  5. import java.io.*;
  6. import java.util.LinkedList;
  7. /**
  8. * 象棋主类
  9. *
  10. * @author cnlht
  11. */
  12. public class ChineseChess extends JFrame implements ActionListener {
  13. ChessBoard board = null;
  14. Demon demon = null;
  15. MakeChessManual record = null;
  16. Container con = null;
  17. JMenuBar bar;
  18. JMenu fileMenu;
  19. JMenuItem 制作棋谱, 保存棋谱, 演示棋谱;
  20. JFileChooser fileChooser = null;
  21. LinkedList 棋谱 = null;
  22. public ChineseChess() {
  23. bar = new JMenuBar();
  24. fileMenu = new JMenu("中国象棋");
  25. 制作棋谱 = new JMenuItem("制作棋谱");
  26. 保存棋谱 = new JMenuItem("保存棋谱");
  27. 保存棋谱.setEnabled(false);
  28. 演示棋谱 = new JMenuItem("演示棋谱");
  29. fileMenu.add(制作棋谱);
  30. fileMenu.add(保存棋谱);
  31. fileMenu.add(演示棋谱);
  32. bar.add(fileMenu);
  33. setJMenuBar(bar);
  34. setTitle(制作棋谱.getText());
  35. 制作棋谱.addActionListener(this);
  36. 保存棋谱.addActionListener(this);
  37. 演示棋谱.addActionListener(this);
  38. board = new ChessBoard(45, 45, 9, 10);
  39. record = board.record;
  40. con = getContentPane();
  41. JSplitPane split = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, true,
  42. board, record);
  43. split.setDividerSize(5);
  44. split.setDividerLocation(460);
  45. con.add(split, BorderLayout.CENTER);
  46. addWindowListener(new WindowAdapter() {
  47. public void windowClosing(WindowEvent e) {
  48. System.exit(0);
  49. }
  50. });
  51. setVisible(true);
  52. setBounds(60, 20, 690, 540);
  53. fileChooser = new JFileChooser();
  54. con.validate();
  55. validate();
  56. }
  57. public void actionPerformed(ActionEvent e) {
  58. if (e.getSource() == 制作棋谱) {
  59. con.removeAll();
  60. 保存棋谱.setEnabled(true);
  61. this.setTitle(制作棋谱.getText());
  62. board = new ChessBoard(45, 45, 9, 10);
  63. record = board.record;
  64. JSplitPane split = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,
  65. true, board, record);
  66. split.setDividerSize(5);
  67. split.setDividerLocation(460);
  68. con.add(split, BorderLayout.CENTER);
  69. validate();
  70. }
  71. if (e.getSource() == 保存棋谱) {
  72. int state = fileChooser.showSaveDialog(null);
  73. File saveFile = fileChooser.getSelectedFile();
  74. if (saveFile != null && state == JFileChooser.APPROVE_OPTION) {
  75. try {
  76. FileOutputStream outOne = new FileOutputStream(saveFile);
  77. ObjectOutputStream outTwo = new ObjectOutputStream(outOne);
  78. outTwo.writeObject(record.获取棋谱());
  79. outOne.close();
  80. outTwo.close();
  81. } catch (IOException event) {
  82. }
  83. }
  84. }
  85. if (e.getSource() == 演示棋谱) {
  86. con.removeAll();
  87. con.repaint();
  88. con.validate();
  89. validate();
  90. 保存棋谱.setEnabled(false);
  91. int state = fileChooser.showOpenDialog(null);
  92. File openFile = fileChooser.getSelectedFile();
  93. if (openFile != null && state == JFileChooser.APPROVE_OPTION) {
  94. try {
  95. FileInputStream inOne = new FileInputStream(openFile);
  96. ObjectInputStream inTwo = new ObjectInputStream(inOne);
  97. 棋谱 = (LinkedList) inTwo.readObject();
  98. inOne.close();
  99. inTwo.close();
  100. ChessBoard board = new ChessBoard(45, 45, 9, 10);
  101. demon = new Demon(board);
  102. demon.set棋谱(棋谱);
  103. con.add(demon, BorderLayout.CENTER);
  104. con.validate();
  105. validate();
  106. this.setTitle(演示棋谱.getText() + ":" + openFile);
  107. } catch (Exception event) {
  108. JLabel label = new JLabel("不是棋谱文件");
  109. label.setFont(new Font("隶书", Font.BOLD, 60));
  110. label.setForeground(Color.red);
  111. label.setHorizontalAlignment(SwingConstants.CENTER);
  112. con.add(label, BorderLayout.CENTER);
  113. con.validate();
  114. this.setTitle("没有打开棋谱");
  115. validate();
  116. }
  117. } else {
  118. JLabel label = new JLabel("没有打开棋谱文件呢");
  119. label.setFont(new Font("隶书", Font.BOLD, 50));
  120. label.setForeground(Color.pink);
  121. label.setHorizontalAlignment(SwingConstants.CENTER);
  122. con.add(label, BorderLayout.CENTER);
  123. con.validate();
  124. this.setTitle("没有打开棋谱文件呢");
  125. validate();
  126. }
  127. }
  128. }
  129. public static void main(String args[]) {
  130. new ChineseChess();
  131. }
  132. }
复制代码

2.象棋棋盘类文件ChessBoard.java

  1. package cn.edu.ouc.chineseChess;
  2. import javax.swing.*;
  3. import java.awt.*;
  4. import java.awt.event.*;
  5. /**
  6. * 棋盘类
  7. *
  8. * @author cnlht
  9. */
  10. public class ChessBoard extends JPanel implements MouseListener,
  11. MouseMotionListener {
  12. public ChessPoint point[][];
  13. public int unitWidth, unitHeight;
  14. private int x轴长, y轴长;
  15. private int x, y;
  16. private Image img;
  17. protected Image pieceImg;
  18. private boolean move = false;
  19. public String 红方颜色 = "红方", 黑方颜色 = "黑方";
  20. ChessPiece 红车1, 红车2, 红马1, 红马2, 红相1, 红相2, 红帅, 红士1, 红士2, 红兵1, 红兵2, 红兵3, 红兵4,
  21. 红兵5, 红炮1, 红炮2;
  22. ChessPiece 黑车1, 黑车2, 黑马1, 黑马2, 黑将, 黑士1, 黑士2, 黑卒1, 黑卒2, 黑卒3, 黑卒4, 黑卒5, 黑象1,
  23. 黑象2, 黑炮1, 黑炮2;
  24. int startX, startY;
  25. int startI, startJ;
  26. public boolean 红方走棋 = true, 黑方走棋 = false;
  27. Rule rule = null;
  28. public MakeChessManual record = null;
  29. public ChessBoard(int w, int h, int r, int c) {
  30. setLayout(null);
  31. addMouseListener(this);
  32. addMouseMotionListener(this);
  33. Color bc = getBackground();
  34. unitWidth = w;
  35. unitHeight = h;
  36. x轴长 = r;
  37. y轴长 = c;
  38. point = new ChessPoint[r + 1][c + 1];
  39. for (int i = 1; i <= r; i++) {
  40. for (int j = 1; j <= c; j++) {
  41. point[i][j] = new ChessPoint(i * unitWidth, j * unitHeight,
  42. false);
  43. }
  44. }
  45. rule = new Rule(this, point);
  46. record = new MakeChessManual(this, point);
  47. img = Toolkit.getDefaultToolkit().getImage("board.jpg");
  48. pieceImg = Toolkit.getDefaultToolkit().getImage("piece.gif");
  49. 红车1 = new ChessPiece("車", Color.red, bc, w - 4, h - 4, this);
  50. 红车1.set棋子类别(红方颜色);
  51. 红车2 = new ChessPiece("車", Color.red, bc, w - 4, h - 4, this);
  52. 红车2.set棋子类别(红方颜色);
  53. 红马1 = new ChessPiece("馬", Color.red, bc, w - 4, h - 4, this);
  54. 红马1.set棋子类别(红方颜色);
  55. 红马2 = new ChessPiece("馬", Color.red, bc, w - 4, h - 4, this);
  56. 红马2.set棋子类别(红方颜色);
  57. 红炮1 = new ChessPiece("炮", Color.red, bc, w - 4, h - 4, this);
  58. 红炮1.set棋子类别(红方颜色);
  59. 红炮2 = new ChessPiece("炮", Color.red, bc, w - 4, h - 4, this);
  60. 红炮2.set棋子类别(红方颜色);
  61. 红相1 = new ChessPiece("相", Color.red, bc, w - 4, h - 4, this);
  62. 红相1.set棋子类别(红方颜色);
  63. 红相2 = new ChessPiece("相", Color.red, bc, w - 4, h - 4, this);
  64. 红相2.set棋子类别(红方颜色);
  65. 红士1 = new ChessPiece("仕", Color.red, bc, w - 4, h - 4, this);
  66. 红士1.set棋子类别(红方颜色);
  67. 红士2 = new ChessPiece("仕", Color.red, bc, w - 4, h - 4, this);
  68. 红士2.set棋子类别(红方颜色);
  69. 红帅 = new ChessPiece("帅", Color.red, bc, w - 4, h - 4, this);
  70. 红帅.set棋子类别(红方颜色);
  71. 红兵1 = new ChessPiece("兵", Color.red, bc, w - 4, h - 4, this);
  72. 红兵1.set棋子类别(红方颜色);
  73. 红兵2 = new ChessPiece("兵", Color.red, bc, w - 4, h - 4, this);
  74. 红兵2.set棋子类别(红方颜色);
  75. 红兵3 = new ChessPiece("兵", Color.red, bc, w - 4, h - 4, this);
  76. 红兵3.set棋子类别(红方颜色);
  77. 红兵4 = new ChessPiece("兵", Color.red, bc, w - 4, h - 4, this);
  78. 红兵4.set棋子类别(红方颜色);
  79. 红兵5 = new ChessPiece("兵", Color.red, bc, w - 4, h - 4, this);
  80. 红兵5.set棋子类别(红方颜色);
  81. 黑将 = new ChessPiece("将", Color.black, bc, w - 4, h - 4, this);
  82. 黑将.set棋子类别(黑方颜色);
  83. 黑士1 = new ChessPiece("士", Color.black, bc, w - 4, h - 4, this);
  84. 黑士1.set棋子类别(黑方颜色);
  85. 黑士2 = new ChessPiece("士", Color.black, bc, w - 4, h - 4, this);
  86. 黑士2.set棋子类别(黑方颜色);
  87. 黑车1 = new ChessPiece("车", Color.black, bc, w - 4, h - 4, this);
  88. 黑车1.set棋子类别(黑方颜色);
  89. 黑车2 = new ChessPiece("车", Color.black, bc, w - 4, h - 4, this);
  90. 黑车2.set棋子类别(黑方颜色);
  91. 黑炮1 = new ChessPiece("炮", Color.black, bc, w - 4, h - 4, this);
  92. 黑炮1.set棋子类别(黑方颜色);
  93. 黑炮2 = new ChessPiece("炮", Color.black, bc, w - 4, h - 4, this);
  94. 黑炮2.set棋子类别(黑方颜色);
  95. 黑象1 = new ChessPiece("象", Color.black, bc, w - 4, h - 4, this);
  96. 黑象1.set棋子类别(黑方颜色);
  97. 黑象2 = new ChessPiece("象", Color.black, bc, w - 4, h - 4, this);
  98. 黑象2.set棋子类别(黑方颜色);
  99. 黑马1 = new ChessPiece("马", Color.black, bc, w - 4, h - 4, this);
  100. 黑马1.set棋子类别(黑方颜色);
  101. 黑马2 = new ChessPiece("马", Color.black, bc, w - 4, h - 4, this);
  102. 黑马2.set棋子类别(黑方颜色);
  103. 黑卒1 = new ChessPiece("卒", Color.black, bc, w - 4, h - 4, this);
  104. 黑卒1.set棋子类别(黑方颜色);
  105. 黑卒2 = new ChessPiece("卒", Color.black, bc, w - 4, h - 4, this);
  106. 黑卒2.set棋子类别(黑方颜色);
  107. 黑卒3 = new ChessPiece("卒", Color.black, bc, w - 4, h - 4, this);
  108. 黑卒3.set棋子类别(黑方颜色);
  109. 黑卒4 = new ChessPiece("卒", Color.black, bc, w - 4, h - 4, this);
  110. 黑卒4.set棋子类别(黑方颜色);
  111. 黑卒5 = new ChessPiece("卒", Color.black, bc, w - 4, h - 4, this);
  112. 黑卒5.set棋子类别(黑方颜色);
  113. point[1][10].setPiece(红车1, this);
  114. point[2][10].setPiece(红马1, this);
  115. point[3][10].setPiece(红相1, this);
  116. point[4][10].setPiece(红士1, this);
  117. point[5][10].setPiece(红帅, this);
  118. point[6][10].setPiece(红士2, this);
  119. point[7][10].setPiece(红相2, this);
  120. point[8][10].setPiece(红马2, this);
  121. point[9][10].setPiece(红车2, this);
  122. point[2][8].setPiece(红炮1, this);
  123. point[8][8].setPiece(红炮2, this);
  124. point[1][7].setPiece(红兵1, this);
  125. point[3][7].setPiece(红兵2, this);
  126. point[5][7].setPiece(红兵3, this);
  127. point[7][7].setPiece(红兵4, this);
  128. point[9][7].setPiece(红兵5, this);
  129. point[1][1].setPiece(黑车1, this);
  130. point[2][1].setPiece(黑马1, this);
  131. point[3][1].setPiece(黑象1, this);
  132. point[4][1].setPiece(黑士1, this);
  133. point[5][1].setPiece(黑将, this);
  134. point[6][1].setPiece(黑士2, this);
  135. point[7][1].setPiece(黑象2, this);
  136. point[8][1].setPiece(黑马2, this);
  137. point[9][1].setPiece(黑车2, this);
  138. point[2][3].setPiece(黑炮1, this);
  139. point[8][3].setPiece(黑炮2, this);
  140. point[1][4].setPiece(黑卒1, this);
  141. point[3][4].setPiece(黑卒2, this);
  142. point[5][4].setPiece(黑卒3, this);
  143. point[7][4].setPiece(黑卒4, this);
  144. point[9][4].setPiece(黑卒5, this);
  145. }
  146. public void paintComponent(Graphics g) {
  147. super.paintComponent(g);
  148. int imgWidth = img.getWidth(this);
  149. int imgHeight = img.getHeight(this);// 获得图片的宽度与高度
  150. int FWidth = getWidth();
  151. int FHeight = getHeight();// 获得窗口的宽度与高度
  152. int x = (FWidth - imgWidth) / 2;
  153. int y = (FHeight - imgHeight) / 2;
  154. g.drawImage(img, x, y, null);
  155. for (int j = 1; j <= y轴长; j++) {
  156. g.drawLine(point[1][j].x, point[1][j].y, point[x轴长][j].x,
  157. point[x轴长][j].y);
  158. }
  159. for (int i = 1; i <= x轴长; i++) {
  160. if (i != 1 && i != x轴长) {
  161. g.drawLine(point[i][1].x, point[i][1].y, point[i][y轴长 - 5].x,
  162. point[i][y轴长 - 5].y);
  163. g.drawLine(point[i][y轴长 - 4].x, point[i][y轴长 - 4].y,
  164. point[i][y轴长].x, point[i][y轴长].y);
  165. } else {
  166. g.drawLine(point[i][1].x, point[i][1].y, point[i][y轴长].x,
  167. point[i][y轴长].y);
  168. }
  169. }
  170. g.drawLine(point[4][1].x, point[4][1].y, point[6][3].x, point[6][3].y);
  171. g.drawLine(point[6][1].x, point[6][1].y, point[4][3].x, point[4][3].y);
  172. g.drawLine(point[4][8].x, point[4][8].y, point[6][y轴长].x,
  173. point[6][y轴长].y);
  174. g.drawLine(point[4][y轴长].x, point[4][y轴长].y, point[6][8].x,
  175. point[6][8].y);
  176. for (int i = 1; i <= x轴长; i++) {
  177. g.drawString("" + i, i * unitWidth, unitHeight / 2);
  178. }
  179. int j = 1;
  180. for (char c = 'A'; c <= 'J'; c++) {
  181. g.drawString("" + c, unitWidth / 4, j * unitHeight);
  182. j++;
  183. }
  184. }
  185. /**鼠标按下事件*/
  186. public void mousePressed(MouseEvent e) {
  187. ChessPiece piece = null;
  188. Rectangle rect = null;
  189. if (e.getSource() == this)
  190. move = false;
  191. if (move == false)
  192. if (e.getSource() instanceof ChessPiece) {
  193. piece = (ChessPiece) e.getSource();
  194. startX = piece.getBounds().x;
  195. startY = piece.getBounds().y;
  196. rect = piece.getBounds();
  197. for (int i = 1; i <= x轴长; i++) {
  198. for (int j = 1; j <= y轴长; j++) {
  199. int x = point[i][j].getX();
  200. int y = point[i][j].getY();
  201. if (rect.contains(x, y)) {
  202. startI = i;
  203. startJ = j;
  204. break;
  205. }
  206. }
  207. }
  208. }
  209. }
  210. public void mouseMoved(MouseEvent e) {
  211. }
  212. /**鼠标拖动事件*/
  213. public void mouseDragged(MouseEvent e) {
  214. ChessPiece piece = null;
  215. if (e.getSource() instanceof ChessPiece) {
  216. piece = (ChessPiece) e.getSource();
  217. move = true;
  218. e = SwingUtilities.convertMouseEvent(piece, e, this);
  219. }
  220. if (e.getSource() == this) {
  221. if (move && piece != null) {
  222. x = e.getX();
  223. y = e.getY();
  224. if (红方走棋 && ((piece.棋子类别()).equals(红方颜色))) {
  225. piece.setLocation(x - piece.getWidth() / 2,
  226. y - piece.getHeight() / 2);
  227. }
  228. if (黑方走棋 && (piece.棋子类别().equals(黑方颜色))) {
  229. piece.setLocation(x - piece.getWidth() / 2,
  230. y - piece.getHeight() / 2);
  231. }
  232. }
  233. }
  234. }
  235. /**松开鼠标事件*/
  236. public void mouseReleased(MouseEvent e) {
  237. ChessPiece piece = null;
  238. move = false;
  239. Rectangle rect = null;
  240. if (e.getSource() instanceof ChessPiece) {
  241. piece = (ChessPiece) e.getSource();
  242. rect = piece.getBounds();
  243. e = SwingUtilities.convertMouseEvent(piece, e, this);
  244. }
  245. if (e.getSource() == this) {
  246. boolean containChessPoint = false;
  247. int x = 0, y = 0;
  248. int m = 0, n = 0;
  249. if (piece != null) {
  250. for (int i = 1; i <= x轴长; i++) {
  251. for (int j = 1; j <= y轴长; j++) {
  252. x = point[i][j].getX();
  253. y = point[i][j].getY();
  254. if (rect.contains(x, y)) {
  255. containChessPoint = true;
  256. m = i;
  257. n = j;
  258. break;
  259. }
  260. }
  261. }
  262. }
  263. if (piece != null && containChessPoint) {
  264. Color pieceColor = piece.获取棋子颜色();
  265. if (point[m][n].isPiece()) {
  266. Color c = (point[m][n].getPiece()).获取棋子颜色();
  267. if (pieceColor.getRGB() == c.getRGB()) {
  268. piece.setLocation(startX, startY);
  269. (point[startI][startJ]).set有棋子(true);
  270. } else {
  271. boolean ok = rule.movePieceRule(piece, startI, startJ,
  272. m, n);
  273. if (ok) {
  274. ChessPiece pieceRemoved = point[m][n].getPiece();
  275. point[m][n].reMovePiece(pieceRemoved, this);
  276. point[m][n].setPiece(piece, this);
  277. (point[startI][startJ]).set有棋子(false);
  278. record.记录棋谱(piece, startI, startJ, m, n);
  279. record.记录吃掉的棋子(pieceRemoved);
  280. rule.isWine(pieceRemoved);
  281. if (piece.棋子类别().equals(红方颜色)) {
  282. 红方走棋 = false;
  283. 黑方走棋 = true;
  284. }
  285. if (piece.棋子类别().equals(黑方颜色)) {
  286. 黑方走棋 = false;
  287. 红方走棋 = true;
  288. }
  289. validate();
  290. repaint();
  291. } else {
  292. piece.setLocation(startX, startY);
  293. (point[startI][startJ]).set有棋子(true);
  294. }
  295. }
  296. } else {
  297. boolean ok = rule
  298. .movePieceRule(piece, startI, startJ, m, n);
  299. if (ok) {
  300. point[m][n].setPiece(piece, this);
  301. (point[startI][startJ]).set有棋子(false);
  302. record.记录棋谱(piece, startI, startJ, m, n);
  303. record.记录吃掉的棋子("没吃棋子");
  304. if (piece.棋子类别().equals(红方颜色)) {
  305. 红方走棋 = false;
  306. 黑方走棋 = true;
  307. }
  308. if (piece.棋子类别().equals(黑方颜色)) {
  309. 黑方走棋 = false;
  310. 红方走棋 = true;
  311. }
  312. } else {
  313. piece.setLocation(startX, startY);
  314. (point[startI][startJ]).set有棋子(true);
  315. }
  316. }
  317. }
  318. if (piece != null && !containChessPoint) {
  319. piece.setLocation(startX, startY);
  320. (point[startI][startJ]).set有棋子(true);
  321. }
  322. }
  323. }
  324. public void mouseEntered(MouseEvent e) {
  325. }
  326. public void mouseExited(MouseEvent e) {
  327. }
  328. public void mouseClicked(MouseEvent e) {
  329. }
  330. }
复制代码

3.棋子类文件ChessPiece.java

  1. package cn.edu.ouc.chineseChess;
  2. import javax.swing.*;
  3. import java.awt.*;
  4. import java.awt.event.*;
  5. /**
  6. * 棋子类
  7. *
  8. * @author cnlht
  9. */
  10. public class ChessPiece extends JLabel {
  11. String name; // 棋子名字
  12. Color backColor = null, foreColor;// 背景色和前景色
  13. String 颜色类别 = null;
  14. ChessBoard board = null;
  15. int width, height;// 大小
  16. public ChessPiece(String name, Color fc, Color bc, int width, int height,
  17. ChessBoard board) {// 构造棋子
  18. this.name = name;
  19. this.board = board;
  20. this.width = width;
  21. this.height = height;
  22. foreColor = fc;
  23. backColor = bc;
  24. setSize(width, height);
  25. setBackground(bc);
  26. addMouseMotionListener(board);
  27. addMouseListener(board);
  28. }
  29. // 绘制棋子
  30. public void paint(Graphics g) {
  31. g.drawImage(board.pieceImg, 2, 2, width-2, height-2, null);
  32. g.setColor(foreColor);
  33. g.setFont(new Font("楷体", Font.BOLD, 26));
  34. g.drawString(name, 7, height - 8);// 在棋子上绘制 “棋子名”
  35. g.setColor(Color.black);
  36. //g.drawOval(1, 1, width - 1, height - 1);
  37. float lineWidth = 2.3f;
  38. ((Graphics2D)g).setStroke(new BasicStroke(lineWidth));
  39. ((Graphics2D)g).drawOval(2, 2, width-2, height-2);
  40. }
  41. public int getWidth() {
  42. return width;
  43. }
  44. public int getHeight() {
  45. return height;
  46. }
  47. public String getName() {
  48. return name;
  49. }
  50. public Color 获取棋子颜色() {
  51. return foreColor;
  52. }
  53. public void set棋子类别(String 类别) {
  54. 颜色类别 = 类别;
  55. }
  56. public String 棋子类别() {
  57. return 颜色类别;
  58. }
  59. }
复制代码

4.棋子点坐标类文件

  1. package cn.edu.ouc.chineseChess;
  2. /**
  3. * 棋点类
  4. *
  5. * @author cnlht
  6. */
  7. public class ChessPoint {
  8. /** 棋子坐标 */
  9. int x, y;
  10. /** 该坐标 是否有子*/
  11. boolean 有棋子;
  12. /** 改坐标的棋子 */
  13. ChessPiece piece = null;
  14. /** 坐标所属棋盘 */
  15. ChessBoard board = null;
  16. public ChessPoint(int x, int y, boolean boo) {
  17. this.x = x;
  18. this.y = y;
  19. 有棋子 = boo;
  20. }
  21. public boolean isPiece() {
  22. return 有棋子;
  23. }
  24. public void set有棋子(boolean boo) {
  25. 有棋子 = boo;
  26. }
  27. public int getX() {
  28. return x;
  29. }
  30. public int getY() {
  31. return y;
  32. }
  33. // 设置改点棋子
  34. public void setPiece(ChessPiece piece, ChessBoard board) {
  35. this.board = board;
  36. this.piece = piece;
  37. board.add(piece);
  38. int w = (board.unitWidth);
  39. int h = (board.unitHeight);
  40. piece.setBounds(x - w / 2, y - h / 2, w, h);// 棋子位置,宽度,高度
  41. 有棋子 = true;
  42. board.validate();
  43. }
  44. public ChessPiece getPiece() {
  45. return piece;
  46. }
  47. public void reMovePiece(ChessPiece piece, ChessBoard board) {
  48. this.board = board;
  49. this.piece = piece;
  50. board.remove(piece);
  51. board.validate();
  52. 有棋子 = false;
  53. }
  54. }
复制代码

5.玩法规则类文件Rule.java

  1. package cn.edu.ouc.chineseChess;
  2. import javax.swing.*;
  3. import java.awt.*;
  4. import java.awt.event.*;
  5. /**
  6. * 走棋规则类
  7. *
  8. * @author cnlht
  9. */
  10. public class Rule {
  11. ChessBoard board = null;
  12. ChessPiece piece = null;
  13. ChessPoint point[][];
  14. int startI, startJ, endI, endJ;
  15. public Rule(ChessBoard board, ChessPoint point[][]) {
  16. this.board = board;
  17. this.point = point;
  18. }
  19. public void isWine(ChessPiece piece) {
  20. this.piece = piece;
  21. if (piece.getName() == "将" || piece.getName() == "帅") {
  22. if (piece.颜色类别 == "红方") {
  23. JOptionPane.showMessageDialog(null, "黑方 胜利!");
  24. } else {
  25. JOptionPane.showMessageDialog(null, "红方 胜利!");
  26. }
  27. }
  28. }
  29. public boolean movePieceRule(ChessPiece piece, int startI, int startJ,
  30. int endI, int endJ) {
  31. this.piece = piece;
  32. this.startI = startI;
  33. this.startJ = startJ;
  34. this.endI = endI;
  35. this.endJ = endJ;
  36. int minI = Math.min(startI, endI);
  37. int maxI = Math.max(startI, endI);
  38. int minJ = Math.min(startJ, endJ);
  39. int maxJ = Math.max(startJ, endJ);
  40. boolean 可否走棋 = false;
  41. if (piece.getName().equals("车")) {
  42. if (startI == endI) {
  43. int j = 0;
  44. for (j = minJ + 1; j <= maxJ - 1; j++) {
  45. if (point[startI][j].isPiece()) {
  46. 可否走棋 = false;
  47. break;
  48. }
  49. }
  50. if (j == maxJ) {
  51. 可否走棋 = true;
  52. }
  53. } else if (startJ == endJ) {
  54. int i = 0;
  55. for (i = minI + 1; i <= maxI - 1; i++) {
  56. if (point[i][startJ].isPiece()) {
  57. 可否走棋 = false;
  58. break;
  59. }
  60. }
  61. if (i == maxI) {
  62. 可否走棋 = true;
  63. }
  64. } else {
  65. 可否走棋 = false;
  66. }
  67. } else if (piece.getName().equals("車")) {
  68. if (startI == endI) {
  69. int j = 0;
  70. for (j = minJ + 1; j <= maxJ - 1; j++) {
  71. if (point[startI][j].isPiece()) {
  72. 可否走棋 = false;
  73. break;
  74. }
  75. }
  76. if (j == maxJ) {
  77. 可否走棋 = true;
  78. }
  79. } else if (startJ == endJ) {
  80. int i = 0;
  81. for (i = minI + 1; i <= maxI - 1; i++) {
  82. if (point[i][startJ].isPiece()) {
  83. 可否走棋 = false;
  84. break;
  85. }
  86. }
  87. if (i == maxI) {
  88. 可否走棋 = true;
  89. }
  90. } else {
  91. 可否走棋 = false;
  92. }
  93. }else if (piece.getName().equals("马")) {
  94. int xAxle = Math.abs(startI - endI);
  95. int yAxle = Math.abs(startJ - endJ);
  96. if (xAxle == 2 && yAxle == 1) {
  97. if (endI > startI) {
  98. if (point[startI + 1][startJ].isPiece()) {
  99. 可否走棋 = false;
  100. } else {
  101. 可否走棋 = true;
  102. }
  103. }
  104. if (endI < startI) {
  105. if (point[startI - 1][startJ].isPiece()) {
  106. 可否走棋 = false;
  107. } else {
  108. 可否走棋 = true;
  109. }
  110. }
  111. }else if (xAxle == 1 && yAxle == 2) {
  112. if (endJ > startJ) {
  113. if (point[startI][startJ + 1].isPiece()) {
  114. 可否走棋 = false;
  115. } else {
  116. 可否走棋 = true;
  117. }
  118. }
  119. if (endJ < startJ) {
  120. if (point[startI][startJ - 1].isPiece()) {
  121. 可否走棋 = false;
  122. } else {
  123. 可否走棋 = true;
  124. }
  125. }
  126. } else {
  127. 可否走棋 = false;
  128. }
  129. } else if (piece.getName().equals("馬")) {
  130. int xAxle = Math.abs(startI - endI);
  131. int yAxle = Math.abs(startJ - endJ);
  132. if (xAxle == 2 && yAxle == 1) {
  133. if (endI > startI) {
  134. if (point[startI + 1][startJ].isPiece()) {
  135. 可否走棋 = false;
  136. } else {
  137. 可否走棋 = true;
  138. }
  139. }
  140. if (endI < startI) {
  141. if (point[startI - 1][startJ].isPiece()) {
  142. 可否走棋 = false;
  143. } else {
  144. 可否走棋 = true;
  145. }
  146. }
  147. }else if (xAxle == 1 && yAxle == 2) {
  148. if (endJ > startJ) {
  149. if (point[startI][startJ + 1].isPiece()) {
  150. 可否走棋 = false;
  151. } else {
  152. 可否走棋 = true;
  153. }
  154. }
  155. if (endJ < startJ) {
  156. if (point[startI][startJ - 1].isPiece()) {
  157. 可否走棋 = false;
  158. } else {
  159. 可否走棋 = true;
  160. }
  161. }
  162. } else {
  163. 可否走棋 = false;
  164. }
  165. } else if (piece.getName().equals("象")) {
  166. int centerI = (startI + endI) / 2;
  167. int centerJ = (startJ + endJ) / 2;
  168. int xAxle = Math.abs(startI - endI);
  169. int yAxle = Math.abs(startJ - endJ);
  170. if (xAxle == 2 && yAxle == 2 && endJ <= 5) {
  171. if (point[centerI][centerJ].isPiece()) {
  172. 可否走棋 = false;
  173. } else {
  174. 可否走棋 = true;
  175. }
  176. } else {
  177. 可否走棋 = false;
  178. }
  179. } else if (piece.getName().equals("相")) {
  180. int centerI = (startI + endI) / 2;
  181. int centerJ = (startJ + endJ) / 2;
  182. int xAxle = Math.abs(startI - endI);
  183. int yAxle = Math.abs(startJ - endJ);
  184. if (xAxle == 2 && yAxle == 2 && endJ >= 6) {
  185. if (point[centerI][centerJ].isPiece()) {
  186. 可否走棋 = false;
  187. } else {
  188. 可否走棋 = true;
  189. }
  190. } else {
  191. 可否走棋 = false;
  192. }
  193. } else if (piece.getName().equals("炮")) {
  194. int number = 0;
  195. if (startI == endI) {
  196. int j = 0;
  197. for (j = minJ + 1; j <= maxJ - 1; j++) {
  198. if (point[startI][j].isPiece()) {
  199. number++;
  200. }
  201. }
  202. if (number > 1) {
  203. 可否走棋 = false;
  204. } else if (number == 1) {
  205. if (point[endI][endJ].isPiece()) {
  206. 可否走棋 = true;
  207. }
  208. } else if (number == 0 && !point[endI][endJ].isPiece()) {
  209. 可否走棋 = true;
  210. }
  211. } else if (startJ == endJ) {
  212. int i = 0;
  213. for (i = minI + 1; i <= maxI - 1; i++) {
  214. if (point[i][startJ].isPiece()) {
  215. number++;
  216. }
  217. }
  218. if (number > 1) {
  219. 可否走棋 = false;
  220. } else if (number == 1) {
  221. if (point[endI][endJ].isPiece()) {
  222. 可否走棋 = true;
  223. }
  224. } else if (number == 0 && !point[endI][endJ].isPiece()) {
  225. 可否走棋 = true;
  226. }
  227. } else {
  228. 可否走棋 = false;
  229. }
  230. } else if (piece.getName().equals("兵")) {
  231. int xAxle = Math.abs(startI - endI);
  232. int yAxle = Math.abs(startJ - endJ);
  233. if (endJ >= 6) {
  234. if (startJ - endJ == 1 && xAxle == 0) {
  235. 可否走棋 = true;
  236. }
  237. else {
  238. 可否走棋 = false;
  239. }
  240. } else if (endJ <= 5) {
  241. if ((startJ - endJ == 1) && (xAxle == 0)) {
  242. 可否走棋 = true;
  243. } else if ((endJ - startJ == 0) && (xAxle == 1)) {
  244. 可否走棋 = true;
  245. } else {
  246. 可否走棋 = false;
  247. }
  248. }
  249. } else if (piece.getName().equals("卒")) {
  250. int xAxle = Math.abs(startI - endI);
  251. int yAxle = Math.abs(startJ - endJ);
  252. if (endJ <= 5) {
  253. if (endJ - startJ == 1 && xAxle == 0) {
  254. 可否走棋 = true;
  255. } else {
  256. 可否走棋 = false;
  257. }
  258. } else if (endJ >= 6) {
  259. if ((endJ - startJ == 1) && (xAxle == 0)) {
  260. 可否走棋 = true;
  261. } else if ((endJ - startJ == 0) && (xAxle == 1)) {
  262. 可否走棋 = true;
  263. } else {
  264. 可否走棋 = false;
  265. }
  266. }
  267. }
  268. else if (piece.getName().equals("士")) {
  269. int xAxle = Math.abs(startI - endI);
  270. int yAxle = Math.abs(startJ - endJ);
  271. if (endI <= 6 && endI >= 4 && xAxle == 1 && yAxle == 1) {
  272. 可否走棋 = true;
  273. } else {
  274. 可否走棋 = false;
  275. }
  276. } else if (piece.getName().equals("仕")) {
  277. int xAxle = Math.abs(startI - endI);
  278. int yAxle = Math.abs(startJ - endJ);
  279. if (endI <= 6 && endI >= 4 && xAxle == 1 && yAxle == 1) {
  280. 可否走棋 = true;
  281. } else {
  282. 可否走棋 = false;
  283. }
  284. } else if ((piece.getName().equals("帅"))
  285. || (piece.getName().equals("将"))) {
  286. int xAxle = Math.abs(startI - endI);
  287. int yAxle = Math.abs(startJ - endJ);
  288. if (endI <= 6 && endI >= 4) {
  289. if ((xAxle == 1 && yAxle == 0) || (xAxle == 0 && yAxle == 1)) {
  290. 可否走棋 = true;
  291. } else {
  292. 可否走棋 = false;
  293. }
  294. } else {
  295. 可否走棋 = false;
  296. }
  297. }
  298. return 可否走棋;
  299. }
  300. }
复制代码

6.走步类文件MoveStep.java

  1. package cn.edu.ouc.chineseChess;
  2. import java.awt.Point;
  3. /**
  4. * 走步类
  5. *
  6. * @author cnlht
  7. *
  8. */
  9. public class MoveStep implements java.io.Serializable {
  10. public Point pStart, pEnd;
  11. public MoveStep(Point p1, Point p2) {
  12. pStart = p1;
  13. pEnd = p2;
  14. }
  15. }
复制代码

7.制作棋谱类MakeChessManual.java

  1. package cn.edu.ouc.chineseChess;
  2. import javax.swing.*;
  3. import java.awt.*;
  4. import java.awt.event.*;
  5. import java.util.LinkedList;
  6. /**
  7. * 制作棋谱类
  8. *
  9. * @author cnlht
  10. */
  11. public class MakeChessManual extends JPanel implements ActionListener {
  12. JTextArea text = null;
  13. JScrollPane scroll = null;
  14. ChessBoard board = null;
  15. ChessPoint[][] point;
  16. LinkedList 棋谱 = null;
  17. LinkedList 吃掉的棋子 = null;
  18. JButton buttonUndo;
  19. int i = 0;
  20. public MakeChessManual(ChessBoard board, ChessPoint[][] point) {
  21. this.board = board;
  22. this.point = point;
  23. text = new JTextArea();
  24. scroll = new JScrollPane(text);
  25. 棋谱 = new LinkedList();
  26. 吃掉的棋子 = new LinkedList();
  27. buttonUndo = new JButton("悔棋");
  28. buttonUndo.setFont(new Font("隶书", Font.PLAIN, 18));
  29. setLayout(new BorderLayout());
  30. add(scroll, BorderLayout.CENTER);
  31. add(buttonUndo, BorderLayout.SOUTH);
  32. buttonUndo.addActionListener(this);
  33. }
  34. public char numberToLetter(int n) {
  35. char c = '';
  36. switch (n) {
  37. case 1:
  38. c = 'A';
  39. break;
  40. case 2:
  41. c = 'B';
  42. break;
  43. case 3:
  44. c = 'C';
  45. break;
  46. case 4:
  47. c = 'D';
  48. break;
  49. case 5:
  50. c = 'E';
  51. break;
  52. case 6:
  53. c = 'F';
  54. break;
  55. case 7:
  56. c = 'G';
  57. break;
  58. case 8:
  59. c = 'H';
  60. break;
  61. case 9:
  62. c = 'I';
  63. break;
  64. case 10:
  65. c = 'J';
  66. break;
  67. }
  68. return c;
  69. }
  70. public void 记录棋谱(ChessPiece piece, int startI, int startJ, int endI,
  71. int endJ) {
  72. Point pStart = new Point(startI, startJ);
  73. Point pEnd = new Point(endI, endJ);
  74. MoveStep step = new MoveStep(pStart, pEnd);
  75. 棋谱.add(step);
  76. String 棋子类别 = piece.棋子类别();
  77. String name = piece.getName();
  78. String m = "#" + 棋子类别 + name + ": " + startI + numberToLetter(startJ)
  79. + " 到 " + endI + numberToLetter(endJ);
  80. text.append(m);
  81. if (piece.棋子类别().equals(board.黑方颜色))
  82. text.append("n");
  83. }
  84. public void 记录吃掉的棋子(Object object) {
  85. 吃掉的棋子.add(object);
  86. }
  87. public LinkedList 获取棋谱() {
  88. return 棋谱;
  89. }
  90. public void actionPerformed(ActionEvent e) {
  91. int position = text.getText().lastIndexOf("#");
  92. if (position != -1)
  93. text.replaceRange("", position, text.getText().length());
  94. if (棋谱.size() > 0) {
  95. MoveStep lastStep = (MoveStep) 棋谱.getLast();
  96. 棋谱.removeLast();
  97. Object qizi = 吃掉的棋子.getLast();
  98. 吃掉的棋子.removeLast();
  99. String temp = qizi.toString();
  100. if (temp.equals("没吃棋子")) {
  101. int startI = lastStep.pStart.x;
  102. int startJ = lastStep.pStart.y;
  103. int endI = lastStep.pEnd.x;
  104. int endJ = lastStep.pEnd.y;
  105. ChessPiece piece = point[endI][endJ].getPiece();
  106. point[startI][startJ].setPiece(piece, board);
  107. (point[endI][endJ]).set有棋子(false);
  108. if (piece.棋子类别().equals(board.红方颜色)) {
  109. board.红方走棋 = true;
  110. board.黑方走棋 = false;
  111. }
  112. if (piece.棋子类别().equals(board.黑方颜色)) {
  113. board.黑方走棋 = true;
  114. board.红方走棋 = false;
  115. }
  116. } else {
  117. ChessPiece removedPiece = (ChessPiece) qizi;
  118. int startI = lastStep.pStart.x;
  119. int startJ = lastStep.pStart.y;
  120. int endI = lastStep.pEnd.x;
  121. int endJ = lastStep.pEnd.y;
  122. ChessPiece piece = point[endI][endJ].getPiece();
  123. point[startI][startJ].setPiece(piece, board);
  124. point[endI][endJ].setPiece(removedPiece, board);
  125. (point[endI][endJ]).set有棋子(true);
  126. if (piece.棋子类别().equals(board.红方颜色)) {
  127. board.红方走棋 = true;
  128. board.黑方走棋 = false;
  129. }
  130. if (piece.棋子类别().equals(board.黑方颜色)) {
  131. board.黑方走棋 = true;
  132. board.红方走棋 = false;
  133. }
  134. }
  135. }
  136. }
  137. }
复制代码

8.演示棋谱类文件Demon.java

  1. package cn.edu.ouc.chineseChess;
  2. import javax.swing.*;
  3. import java.awt.*;
  4. import java.awt.event.*;
  5. import java.util.*;
  6. /**
  7. * 演示棋谱类
  8. *
  9. * @author cnlht
  10. */
  11. public class Demon extends JPanel implements ActionListener, Runnable {
  12. public JButton replay = null, next = null, auto = null, stop = null;
  13. LinkedList 棋谱 = null;
  14. Thread 自动演示 = null;
  15. int index = -1;
  16. ChessBoard board = null;
  17. JTextArea text;
  18. JTextField 时间间隔 = null;
  19. int time = 1000;
  20. String 演示过程 = "";
  21. JSplitPane splitH = null, splitV = null;
  22. public Demon(ChessBoard board) {
  23. this.board = board;
  24. replay = new JButton("重新演示");
  25. next = new JButton("下一步");
  26. auto = new JButton("自动演示");
  27. stop = new JButton("暂停演示");
  28. 自动演示 = new Thread(this);
  29. replay.addActionListener(this);
  30. next.addActionListener(this);
  31. auto.addActionListener(this);
  32. stop.addActionListener(this);
  33. text = new JTextArea();
  34. 时间间隔 = new JTextField("1");
  35. setLayout(new BorderLayout());
  36. JScrollPane pane = new JScrollPane(text);
  37. JPanel p = new JPanel(new GridLayout(3, 2));
  38. p.add(next);
  39. p.add(replay);
  40. p.add(auto);
  41. p.add(stop);
  42. p.add(new JLabel("时间间隔(秒)", SwingConstants.CENTER));
  43. p.add(时间间隔);
  44. splitV = new JSplitPane(JSplitPane.VERTICAL_SPLIT, pane, p);
  45. splitH = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, board, splitV);
  46. splitV.setDividerSize(5);
  47. splitV.setDividerLocation(400);
  48. splitH.setDividerSize(5);
  49. splitH.setDividerLocation(460);
  50. add(splitH, BorderLayout.CENTER);
  51. validate();
  52. }
  53. public void set棋谱(LinkedList 棋谱) {
  54. this.棋谱 = 棋谱;
  55. }
  56. public char numberToLetter(int n) {
  57. char c = '';
  58. switch (n) {
  59. case 1:
  60. c = 'A';
  61. break;
  62. case 2:
  63. c = 'B';
  64. break;
  65. case 3:
  66. c = 'C';
  67. break;
  68. case 4:
  69. c = 'D';
  70. break;
  71. case 5:
  72. c = 'E';
  73. break;
  74. case 6:
  75. c = 'F';
  76. break;
  77. case 7:
  78. c = 'G';
  79. break;
  80. case 8:
  81. c = 'H';
  82. break;
  83. case 9:
  84. c = 'I';
  85. break;
  86. case 10:
  87. c = 'J';
  88. break;
  89. }
  90. return c;
  91. }
  92. public void actionPerformed(ActionEvent e) {
  93. if (e.getSource() == next) {
  94. index++;
  95. if (index < 棋谱.size()) {
  96. 演示一步(index);
  97. } else {
  98. 演示结束("棋谱演示完毕");
  99. }
  100. }
  101. if (e.getSource() == replay) {
  102. board = new ChessBoard(45, 45, 9, 10);
  103. splitH.remove(board);
  104. splitH.setDividerSize(5);
  105. splitH.setDividerLocation(460);
  106. splitH.setLeftComponent(board);
  107. splitH.validate();
  108. index = -1;
  109. text.setText(null);
  110. }
  111. if (e.getSource() == auto) {
  112. next.setEnabled(false);
  113. replay.setEnabled(false);
  114. try {
  115. time = 1000 * Integer.parseInt(时间间隔.getText().trim());
  116. } catch (NumberFormatException ee) {
  117. time = 1000;
  118. }
  119. if (!(自动演示.isAlive())) {
  120. 自动演示 = new Thread(this);
  121. board = new ChessBoard(45, 45, 9, 10);
  122. splitH.remove(board);
  123. splitH.setDividerSize(5);
  124. splitH.setDividerLocation(460);
  125. splitH.setLeftComponent(board);
  126. splitH.validate();
  127. text.setText(null);
  128. 自动演示.start();
  129. }
  130. }
  131. if (e.getSource() == stop) {
  132. if (e.getActionCommand().equals("暂停演示")) {
  133. 演示过程 = "暂停演示";
  134. stop.setText("继续演示");
  135. stop.repaint();
  136. }
  137. if (e.getActionCommand().equals("继续演示")) {
  138. 演示过程 = "继续演示";
  139. 自动演示.interrupt();
  140. stop.setText("暂停演示");
  141. stop.repaint();
  142. }
  143. }
  144. }
  145. public synchronized void run() {
  146. for (index = 0; index < 棋谱.size(); index++) {
  147. try {
  148. Thread.sleep(time);
  149. } catch (InterruptedException e) {
  150. }
  151. while (演示过程.equals("暂停演示")) {
  152. try {
  153. wait();
  154. } catch (InterruptedException e) {
  155. notifyAll();
  156. }
  157. }
  158. 演示一步(index);
  159. }
  160. if (index >= 棋谱.size()) {
  161. 演示结束("棋谱演示完毕");
  162. next.setEnabled(true);
  163. replay.setEnabled(true);
  164. }
  165. }
  166. public void 演示一步(int index) {
  167. MoveStep step = (MoveStep) 棋谱.get(index);
  168. Point pStart = step.pStart;
  169. Point pEnd = step.pEnd;
  170. int startI = pStart.x;
  171. int startJ = pStart.y;
  172. int endI = pEnd.x;
  173. int endJ = pEnd.y;
  174. ChessPiece piece = (board.point)[startI][startJ].getPiece();
  175. if ((board.point)[endI][endJ].isPiece() == true) {
  176. ChessPiece pieceRemoved = (board.point)[endI][endJ].getPiece();
  177. (board.point)[endI][endJ].reMovePiece(pieceRemoved, board);
  178. board.repaint();
  179. (board.point)[endI][endJ].setPiece(piece, board);
  180. (board.point)[startI][startJ].set有棋子(false);
  181. board.repaint();
  182. } else {
  183. (board.point)[endI][endJ].setPiece(piece, board);
  184. (board.point)[startI][startJ].set有棋子(false);
  185. }
  186. String 棋子类别 = piece.棋子类别();
  187. String name = piece.getName();
  188. String m = "#" + 棋子类别 + name + ": " + startI + numberToLetter(startJ)
  189. + " 到 " + endI + numberToLetter(endJ);
  190. text.append(m);
  191. if (piece.棋子类别().equals(board.黑方颜色))
  192. text.append("n");
  193. }
  194. public void 演示结束(String message) {
  195. splitH.remove(board);
  196. splitH.setDividerSize(5);
  197. splitH.setDividerLocation(460);
  198. JLabel label = new JLabel(message);
  199. label.setFont(new Font("隶书", Font.BOLD, 40));
  200. label.setForeground(Color.blue);
  201. label.setHorizontalAlignment(SwingConstants.CENTER);
  202. splitH.setLeftComponent(label);
  203. splitH.validate();
  204. }
  205. }
复制代码

四、总结与要求
1.理解8个文件,没有太复杂的代码。
2.理解鼠标的MouseListener,MouseMotionListener两个接口的区别,五子棋的实现不需要MouseMotionListener。
3.使用LinkedList记录棋谱的方法。

希望大家喜欢这篇文章,制作一款属于自己的中国象棋游戏。

最新评论

小黑屋|在路上 ( 蜀ICP备15035742号-1 

;

GMT+8, 2025-7-8 07:04

Copyright 2015-2025 djqfx

返回顶部