JAVA实践(一)————中国象棋打谱系统

来源:互联网 发布:世界上的光 海明威知乎 编辑:程序博客网 时间:2024/06/03 20:18

一、实践目的:

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

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

3.完善象棋的规则。

二、实践内容:

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

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


人人对弈图

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


选择“制作棋谱”菜单




棋谱制作完毕红方胜出

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


保存棋谱对话框

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


演示棋谱对话框



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

三、参考代码:

1.象棋主类 文件ChineseChess.java

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

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

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

3.棋子类文件ChessPiece.java

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

4.棋子点坐标类文件

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


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

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

6.走步类文件MoveStep.java

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


7.制作棋谱类MakeChessManual.java

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


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

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