【笔记】JAVA 中国象棋游戏 部分源码
/**
?* 演示棋譜類
?*
?* @author cnlht
?*/
public class Demon extends JPanel implements ActionListener, Runnable {
?public JButton replay = null, next = null, auto = null, stop = null;
?LinkedList 棋譜 = null;
?Thread 自動演示 = null; 中國象棋:http://xiangqi.sz4j.com/forum-39-2.html
?int index = -1;
?ChessBoard board = null;
?JTextArea text;
?JTextField 時間間隔 = null;
?int time = 1000;
?String 演示過程 = "";
?JSplitPane splitH = null, splitV = null;
?public Demon(ChessBoard board) {
??this.board = board;
??replay = new JButton("重新演示");
??next = new JButton("下一步");
??auto = new JButton("自動演示");
??stop = new JButton("暫停演示");
??自動演示 = new Thread(this);
??replay.addActionListener(this);
??next.addActionListener(this);
??auto.addActionListener(this);
??stop.addActionListener(this);
??text = new JTextArea();
??時間間隔 = new JTextField("1");
??setLayout(new BorderLayout());
??JScrollPane pane = new JScrollPane(text);
??JPanel p = new JPanel(new GridLayout(3, 2));
??p.add(next);
??p.add(replay);
??p.add(auto);
??p.add(stop);
??p.add(new JLabel("時間間隔(秒)", SwingConstants.CENTER));
??p.add(時間間隔); 象棋游戲:http://xiangqi.sz4j.com/forum-39-3.html
??splitV = new JSplitPane(JSplitPane.VERTICAL_SPLIT, pane, p);
??splitH = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, board, splitV);
??splitV.setDividerSize(5);
??splitV.setDividerLocation(400);
??splitH.setDividerSize(5);
??splitH.setDividerLocation(460);
??add(splitH, BorderLayout.CENTER);
??validate();
?}
?public void set棋譜(LinkedList 棋譜) {
??this.棋譜 = 棋譜;
?}
?public char numberToLetter(int n) {
??char c = '\0';
??switch (n) {
??case 1:
???c = 'A';
???break;
??case 2:
???c = 'B';
???break;
??case 3:
???c = 'C';
???break;
??case 4:
???c = 'D';
???break;
??case 5:
???c = 'E';
???break;
??case 6:
???c = 'F';
???break;
??case 7:
???c = 'G';
???break;
??case 8:
???c = 'H';
???break;
??case 9:
???c = 'I';
???break;
??case 10:
???c = 'J';
???break;
??}
??return c;
?}
?public void actionPerformed(ActionEvent e) {
??if (e.getSource() == next) {
???index++;
???if (index < 棋譜.size()) {
????演示一步(index);
???} else {
????演示結(jié)束("棋譜演示完畢");
???}
??}
??if (e.getSource() == replay) {
???board = new ChessBoard(45, 45, 9, 10);
???splitH.remove(board);
???splitH.setDividerSize(5);
???splitH.setDividerLocation(460);
???splitH.setLeftComponent(board);
???splitH.validate();
???index = -1;
???text.setText(null);
??}
??if (e.getSource() == auto) {
???next.setEnabled(false);
???replay.setEnabled(false);
???try {
????time = 1000 * Integer.parseInt(時間間隔.getText().trim());
???} catch (NumberFormatException ee) {
????time = 1000;
???}
???if (!(自動演示.isAlive())) {
????自動演示 = new Thread(this);
????board = new ChessBoard(45, 45, 9, 10);
????splitH.remove(board);
????splitH.setDividerSize(5);
????splitH.setDividerLocation(460);
????splitH.setLeftComponent(board);
????splitH.validate();
????text.setText(null);
????自動演示.start();
???}
??}
??if (e.getSource() == stop) {
???if (e.getActionCommand().equals("暫停演示")) {
????演示過程 = "暫停演示";
????stop.setText("繼續(xù)演示");
????stop.repaint();
???}
???if (e.getActionCommand().equals("繼續(xù)演示")) {
????演示過程 = "繼續(xù)演示";
????自動演示.interrupt();
????stop.setText("暫停演示");
????stop.repaint();
???}
??}
?}
?public synchronized void run() {
??for (index = 0; index < 棋譜.size(); index++) {
???try {
????Thread.sleep(time);
???} catch (InterruptedException e) {
???}
???while (演示過程.equals("暫停演示")) {
????try {
?????wait();
????} catch (InterruptedException e) {
?????notifyAll();
????}
???}
???演示一步(index);
??}
??if (index >= 棋譜.size()) {
???演示結(jié)束("棋譜演示完畢");
???next.setEnabled(true);
???replay.setEnabled(true);
??}
?}
?public void 演示一步(int index) {
??MoveStep step = (MoveStep) 棋譜.get(index);
??Point pStart = step.pStart;
??Point pEnd = step.pEnd;
??int startI = pStart.x;
??int startJ = pStart.y;
??int endI = pEnd.x;
??int endJ = pEnd.y;
??ChessPiece piece = (board.point)[startI][startJ].getPiece();
??if ((board.point)[endI][endJ].isPiece() == true) {
???ChessPiece pieceRemoved = (board.point)[endI][endJ].getPiece();
???(board.point)[endI][endJ].reMovePiece(pieceRemoved, board);
???board.repaint();
???(board.point)[endI][endJ].setPiece(piece, board);
???(board.point)[startI][startJ].set有棋子(false);
???board.repaint();
??} else {
???(board.point)[endI][endJ].setPiece(piece, board);
???(board.point)[startI][startJ].set有棋子(false);
??}
??String 棋子類別 = piece.棋子類別();
??String name = piece.getName();
??String m = "#" + 棋子類別 + name + ": " + startI + numberToLetter(startJ)
????+ " 到 " + endI + numberToLetter(endJ);
??text.append(m);
??if (piece.棋子類別().equals(board.黑方顏色))
???text.append("\n");
?}
?public void 演示結(jié)束(String message) {
??splitH.remove(board);
??splitH.setDividerSize(5);
??splitH.setDividerLocation(460);
??JLabel label = new JLabel(message);
??label.setFont(new Font("隸書", Font.BOLD, 40));
??label.setForeground(Color.blue);
??label.setHorizontalAlignment(SwingConstants.CENTER);
??splitH.setLeftComponent(label);
??splitH.validate();
?}
}
?
總結(jié)
以上是生活随笔為你收集整理的【笔记】JAVA 中国象棋游戏 部分源码的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 听说IT人的目标都是成为架构师,那么请收
- 下一篇: 有了报表FineReport,为什么还要