程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> 關於JAVA >> Java棋類游戲理論當中國象棋

Java棋類游戲理論當中國象棋

編輯:關於JAVA

Java棋類游戲理論當中國象棋。本站提示廣大學習愛好者:(Java棋類游戲理論當中國象棋)文章只能為提供參考,不一定能成為您想要的結果。以下是Java棋類游戲理論當中國象棋正文


本文實例講述了java完成的中國象棋游戲代碼,分享給年夜家供年夜家參考,詳細代碼以下

1、理論目標:

1.鼠標點擊、拖動等事宜的運用與差別

2.棋譜文件的保留與讀取

3.完美象棋的規矩。

2、理論內容:

中國象棋汗青悠長,吸引了有數的人研討,現對中國象棋的對戰和完成棋譜的制造做以下的設計和解釋,供年夜家參考進修。

1、機機棋戰,紅方先手。在相符規矩的情形下拖動棋子到目標地,松鼠標落子。


人人棋戰圖

2、制造棋譜,選擇制造棋譜菜單後,棋戰開端,並記載了下棋進程。


選擇“制造棋譜”菜單



棋譜制造終了紅方勝出

一方勝出後彈出成功新聞對話框。點擊肯定後,選擇“保留棋譜”菜單,彈出保留文件對話框。


保留棋譜對話框

3.演示棋譜,選擇演示棋譜菜單後,彈出翻開對話框,選擇保留好的棋譜,開端演示。


演示棋譜對話框


演示棋譜進程(主動和手動兩種)

3、參考代碼:

1.象棋主類 文件ChineseChess.java

package cn.edu.ouc.chineseChess; 
 
import javax.swing.*; 
import java.awt.*; 
import java.awt.event.*; 
import java.io.*; 
import java.util.LinkedList; 
 
/** 
 * 象棋主類 
 * 
 * @author cnlht 
 */ 
public class ChineseChess extends JFrame implements ActionListener { 
 ChessBoard board = null; 
 Demon demon = null; 
 MakeChessManual record = null; 
 Container con = null; 
 JMenuBar bar; 
 JMenu fileMenu; 
 JMenuItem 制造棋譜, 保留棋譜, 演示棋譜; 
 JFileChooser fileChooser = null; 
 LinkedList 棋譜 = null; 
 
 public ChineseChess() { 
  bar = new JMenuBar(); 
  fileMenu = new JMenu("中國象棋"); 
  制造棋譜 = new JMenuItem("制造棋譜"); 
  保留棋譜 = new JMenuItem("保留棋譜"); 
  保留棋譜.setEnabled(false); 
  演示棋譜 = new JMenuItem("演示棋譜"); 
  fileMenu.add(制造棋譜); 
  fileMenu.add(保留棋譜); 
  fileMenu.add(演示棋譜); 
  bar.add(fileMenu); 
  setJMenuBar(bar); 
  setTitle(制造棋譜.getText()); 
  制造棋譜.addActionListener(this); 
  保留棋譜.addActionListener(this); 
  演示棋譜.addActionListener(this); 
  board = new ChessBoard(45, 45, 9, 10); 
  record = board.record; 
  con = getContentPane(); 
  JSplitPane split = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, true, 
    board, record); 
  split.setDividerSize(5); 
  split.setDividerLocation(460); 
  con.add(split, BorderLayout.CENTER); 
  addWindowListener(new WindowAdapter() { 
   public void windowClosing(WindowEvent e) { 
    System.exit(0); 
   } 
  }); 
  setVisible(true); 
  setBounds(60, 20, 690, 540); 
  fileChooser = new JFileChooser(); 
  con.validate(); 
  validate(); 
 } 
 
 public void actionPerformed(ActionEvent e) { 
  if (e.getSource() == 制造棋譜) { 
   con.removeAll(); 
   保留棋譜.setEnabled(true); 
   this.setTitle(制造棋譜.getText()); 
   board = new ChessBoard(45, 45, 9, 10); 
   record = board.record; 
   JSplitPane split = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, 
     true, board, record); 
   split.setDividerSize(5); 
   split.setDividerLocation(460); 
   con.add(split, BorderLayout.CENTER); 
   validate(); 
  } 
  if (e.getSource() == 保留棋譜) { 
   int state = fileChooser.showSaveDialog(null); 
   File saveFile = fileChooser.getSelectedFile(); 
   if (saveFile != null && state == JFileChooser.APPROVE_OPTION) { 
    try { 
     FileOutputStream outOne = new FileOutputStream(saveFile); 
     ObjectOutputStream outTwo = new ObjectOutputStream(outOne); 
     outTwo.writeObject(record.獲得棋譜()); 
     outOne.close(); 
     outTwo.close(); 
    } catch (IOException event) { 
    } 
   } 
  } 
  if (e.getSource() == 演示棋譜) { 
   con.removeAll(); 
   con.repaint(); 
   con.validate(); 
   validate(); 
   保留棋譜.setEnabled(false); 
 
   int state = fileChooser.showOpenDialog(null); 
   File openFile = fileChooser.getSelectedFile(); 
   if (openFile != null && state == JFileChooser.APPROVE_OPTION) { 
    try { 
     FileInputStream inOne = new FileInputStream(openFile); 
     ObjectInputStream inTwo = new ObjectInputStream(inOne); 
     棋譜 = (LinkedList) inTwo.readObject(); 
     inOne.close(); 
     inTwo.close(); 
     ChessBoard board = new ChessBoard(45, 45, 9, 10); 
     demon = new Demon(board); 
     demon.set棋譜(棋譜); 
     con.add(demon, BorderLayout.CENTER); 
     con.validate(); 
     validate(); 
     this.setTitle(演示棋譜.getText() + ":" + openFile); 
    } catch (Exception event) { 
     JLabel label = new JLabel("不是棋譜文件"); 
     label.setFont(new Font("隸書", Font.BOLD, 60)); 
     label.setForeground(Color.red); 
     label.setHorizontalAlignment(SwingConstants.CENTER); 
     con.add(label, BorderLayout.CENTER); 
     con.validate(); 
     this.setTitle("沒有翻開棋譜"); 
     validate(); 
    } 
   } else { 
    JLabel label = new JLabel("沒有翻開棋譜文件呢"); 
    label.setFont(new Font("隸書", Font.BOLD, 50)); 
    label.setForeground(Color.pink); 
    label.setHorizontalAlignment(SwingConstants.CENTER); 
    con.add(label, BorderLayout.CENTER); 
    con.validate(); 
    this.setTitle("沒有翻開棋譜文件呢"); 
    validate(); 
   } 
  } 
 } 
 
 public static void main(String args[]) { 
  new ChineseChess(); 
 } 
} 

2.象棋棋盤類文件ChessBoard.java

package cn.edu.ouc.chineseChess; 
 
import javax.swing.*; 
import java.awt.*; 
import java.awt.event.*; 
 
/** 
 * 棋盤類 
 * 
 * @author cnlht 
 */ 
public class ChessBoard extends JPanel implements MouseListener, 
  MouseMotionListener { 
 public ChessPoint point[][]; 
 public int unitWidth, unitHeight; 
 private int x軸長, y軸長; 
 private int x, y; 
 private Image img; 
 protected Image pieceImg; 
 private boolean move = false; 
 public String 紅方色彩 = "紅方", 黑方色彩 = "黑方"; 
 ChessPiece 紅車1, 紅車2, 紅馬1, 紅馬2, 紅相1, 紅相2, 紅帥, 紅士1, 紅士2, 紅兵1, 紅兵2, 紅兵3, 紅兵4, 
   紅兵5, 紅炮1, 紅炮2; 
 ChessPiece 黑車1, 黑車2, 黑馬1, 黑馬2, 黑將, 黑士1, 黑士2, 黑卒1, 黑卒2, 黑卒3, 黑卒4, 黑卒5, 黑象1, 
   黑象2, 黑炮1, 黑炮2; 
 
 int startX, startY; 
 int startI, startJ; 
 public boolean 紅方走棋 = true, 黑方走棋 = false; 
 Rule rule = null; 
 public MakeChessManual record = null; 
 
 public ChessBoard(int w, int h, int r, int c) { 
  setLayout(null); 
  addMouseListener(this); 
  addMouseMotionListener(this); 
  Color bc = getBackground(); 
  unitWidth = w; 
  unitHeight = h; 
  x軸長 = r; 
  y軸長 = c; 
 
  point = new ChessPoint[r + 1][c + 1]; 
 
  for (int i = 1; i <= r; i++) { 
   for (int j = 1; j <= c; j++) { 
    point[i][j] = new ChessPoint(i * unitWidth, j * unitHeight, 
      false); 
   } 
  } 
 
  rule = new Rule(this, point); 
  record = new MakeChessManual(this, point); 
 
  img = Toolkit.getDefaultToolkit().getImage("board.jpg"); 
  pieceImg = Toolkit.getDefaultToolkit().getImage("piece.gif"); 
   
  紅車1 = new ChessPiece("車", Color.red, bc, w - 4, h - 4, this); 
  紅車1.set棋子種別(紅方色彩); 
  紅車2 = new ChessPiece("車", Color.red, bc, w - 4, h - 4, this); 
  紅車2.set棋子種別(紅方色彩); 
  紅馬1 = new ChessPiece("馬", Color.red, bc, w - 4, h - 4, this); 
  紅馬1.set棋子種別(紅方色彩); 
  紅馬2 = new ChessPiece("馬", Color.red, bc, w - 4, h - 4, this); 
  紅馬2.set棋子種別(紅方色彩); 
  紅炮1 = new ChessPiece("炮", Color.red, bc, w - 4, h - 4, this); 
  紅炮1.set棋子種別(紅方色彩); 
  紅炮2 = new ChessPiece("炮", Color.red, bc, w - 4, h - 4, this); 
  紅炮2.set棋子種別(紅方色彩); 
  紅相1 = new ChessPiece("相", Color.red, bc, w - 4, h - 4, this); 
  紅相1.set棋子種別(紅方色彩); 
  紅相2 = new ChessPiece("相", Color.red, bc, w - 4, h - 4, this); 
  紅相2.set棋子種別(紅方色彩); 
  紅士1 = new ChessPiece("仕", Color.red, bc, w - 4, h - 4, this); 
  紅士1.set棋子種別(紅方色彩); 
  紅士2 = new ChessPiece("仕", Color.red, bc, w - 4, h - 4, this); 
  紅士2.set棋子種別(紅方色彩); 
  紅帥 = new ChessPiece("帥", Color.red, bc, w - 4, h - 4, this); 
  紅帥.set棋子種別(紅方色彩); 
  紅兵1 = new ChessPiece("兵", Color.red, bc, w - 4, h - 4, this); 
  紅兵1.set棋子種別(紅方色彩); 
  紅兵2 = new ChessPiece("兵", Color.red, bc, w - 4, h - 4, this); 
  紅兵2.set棋子種別(紅方色彩); 
  紅兵3 = new ChessPiece("兵", Color.red, bc, w - 4, h - 4, this); 
  紅兵3.set棋子種別(紅方色彩); 
  紅兵4 = new ChessPiece("兵", Color.red, bc, w - 4, h - 4, this); 
  紅兵4.set棋子種別(紅方色彩); 
  紅兵5 = new ChessPiece("兵", Color.red, bc, w - 4, h - 4, this); 
  紅兵5.set棋子種別(紅方色彩); 
 
  黑將 = new ChessPiece("將", Color.black, bc, w - 4, h - 4, this); 
  黑將.set棋子種別(黑方色彩); 
  黑士1 = new ChessPiece("士", Color.black, bc, w - 4, h - 4, this); 
  黑士1.set棋子種別(黑方色彩); 
  黑士2 = new ChessPiece("士", Color.black, bc, w - 4, h - 4, this); 
  黑士2.set棋子種別(黑方色彩); 
  黑車1 = new ChessPiece("車", Color.black, bc, w - 4, h - 4, this); 
  黑車1.set棋子種別(黑方色彩); 
  黑車2 = new ChessPiece("車", Color.black, bc, w - 4, h - 4, this); 
  黑車2.set棋子種別(黑方色彩); 
  黑炮1 = new ChessPiece("炮", Color.black, bc, w - 4, h - 4, this); 
  黑炮1.set棋子種別(黑方色彩); 
  黑炮2 = new ChessPiece("炮", Color.black, bc, w - 4, h - 4, this); 
  黑炮2.set棋子種別(黑方色彩); 
  黑象1 = new ChessPiece("象", Color.black, bc, w - 4, h - 4, this); 
  黑象1.set棋子種別(黑方色彩); 
  黑象2 = new ChessPiece("象", Color.black, bc, w - 4, h - 4, this); 
  黑象2.set棋子種別(黑方色彩); 
  黑馬1 = new ChessPiece("馬", Color.black, bc, w - 4, h - 4, this); 
  黑馬1.set棋子種別(黑方色彩); 
  黑馬2 = new ChessPiece("馬", Color.black, bc, w - 4, h - 4, this); 
  黑馬2.set棋子種別(黑方色彩); 
  黑卒1 = new ChessPiece("卒", Color.black, bc, w - 4, h - 4, this); 
  黑卒1.set棋子種別(黑方色彩); 
  黑卒2 = new ChessPiece("卒", Color.black, bc, w - 4, h - 4, this); 
  黑卒2.set棋子種別(黑方色彩); 
  黑卒3 = new ChessPiece("卒", Color.black, bc, w - 4, h - 4, this); 
  黑卒3.set棋子種別(黑方色彩); 
  黑卒4 = new ChessPiece("卒", Color.black, bc, w - 4, h - 4, this); 
  黑卒4.set棋子種別(黑方色彩); 
  黑卒5 = new ChessPiece("卒", Color.black, bc, w - 4, h - 4, this); 
  黑卒5.set棋子種別(黑方色彩); 
  point[1][10].setPiece(紅車1, this); 
  point[2][10].setPiece(紅馬1, this); 
  point[3][10].setPiece(紅相1, this); 
  point[4][10].setPiece(紅士1, this); 
  point[5][10].setPiece(紅帥, this); 
  point[6][10].setPiece(紅士2, this); 
  point[7][10].setPiece(紅相2, this); 
  point[8][10].setPiece(紅馬2, this); 
  point[9][10].setPiece(紅車2, this); 
  point[2][8].setPiece(紅炮1, this); 
  point[8][8].setPiece(紅炮2, this); 
  point[1][7].setPiece(紅兵1, this); 
  point[3][7].setPiece(紅兵2, this); 
  point[5][7].setPiece(紅兵3, this); 
  point[7][7].setPiece(紅兵4, this); 
  point[9][7].setPiece(紅兵5, this); 
 
  point[1][1].setPiece(黑車1, this); 
  point[2][1].setPiece(黑馬1, this); 
  point[3][1].setPiece(黑象1, this); 
  point[4][1].setPiece(黑士1, this); 
  point[5][1].setPiece(黑將, this); 
  point[6][1].setPiece(黑士2, this); 
  point[7][1].setPiece(黑象2, this); 
  point[8][1].setPiece(黑馬2, this); 
  point[9][1].setPiece(黑車2, this); 
  point[2][3].setPiece(黑炮1, this); 
  point[8][3].setPiece(黑炮2, this); 
  point[1][4].setPiece(黑卒1, this); 
  point[3][4].setPiece(黑卒2, this); 
  point[5][4].setPiece(黑卒3, this); 
  point[7][4].setPiece(黑卒4, this); 
  point[9][4].setPiece(黑卒5, this); 
 
 } 
 
 public void paintComponent(Graphics g) { 
  super.paintComponent(g); 
 
  int imgWidth = img.getWidth(this); 
  int imgHeight = img.getHeight(this);// 取得圖片的寬度與高度 
  int FWidth = getWidth(); 
  int FHeight = getHeight();// 取得窗口的寬度與高度 
  int x = (FWidth - imgWidth) / 2; 
  int y = (FHeight - imgHeight) / 2; 
  g.drawImage(img, x, y, null); 
 
  for (int j = 1; j <= y軸長; j++) { 
   g.drawLine(point[1][j].x, point[1][j].y, point[x軸長][j].x, 
     point[x軸長][j].y); 
  } 
  for (int i = 1; i <= x軸長; i++) { 
   if (i != 1 && i != x軸長) { 
    g.drawLine(point[i][1].x, point[i][1].y, point[i][y軸長 - 5].x, 
      point[i][y軸長 - 5].y); 
    g.drawLine(point[i][y軸長 - 4].x, point[i][y軸長 - 4].y, 
      point[i][y軸長].x, point[i][y軸長].y); 
   } else { 
    g.drawLine(point[i][1].x, point[i][1].y, point[i][y軸長].x, 
      point[i][y軸長].y); 
   } 
  } 
 
  g.drawLine(point[4][1].x, point[4][1].y, point[6][3].x, point[6][3].y); 
  g.drawLine(point[6][1].x, point[6][1].y, point[4][3].x, point[4][3].y); 
  g.drawLine(point[4][8].x, point[4][8].y, point[6][y軸長].x, 
    point[6][y軸長].y); 
  g.drawLine(point[4][y軸長].x, point[4][y軸長].y, point[6][8].x, 
    point[6][8].y); 
 
  for (int i = 1; i <= x軸長; i++) { 
   g.drawString("" + i, i * unitWidth, unitHeight / 2); 
  } 
  int j = 1; 
  for (char c = 'A'; c <= 'J'; c++) { 
   g.drawString("" + c, unitWidth / 4, j * unitHeight); 
   j++; 
  } 
 
 } 
 
 /**鼠標按下事宜*/ 
 public void mousePressed(MouseEvent e) { 
  ChessPiece piece = null; 
  Rectangle rect = null; 
  if (e.getSource() == this) 
   move = false; 
  if (move == false) 
   if (e.getSource() instanceof ChessPiece) { 
    piece = (ChessPiece) e.getSource(); 
    startX = piece.getBounds().x; 
    startY = piece.getBounds().y; 
 
    rect = piece.getBounds(); 
    for (int i = 1; i <= x軸長; i++) { 
     for (int j = 1; j <= y軸長; j++) { 
      int x = point[i][j].getX(); 
      int y = point[i][j].getY(); 
      if (rect.contains(x, y)) { 
       startI = i; 
       startJ = j; 
       break; 
      } 
 
     } 
    } 
   } 
 } 
 
 public void mouseMoved(MouseEvent e) { 
 } 
 
 /**鼠標拖動事宜*/ 
 public void mouseDragged(MouseEvent e) { 
 
  ChessPiece piece = null; 
  if (e.getSource() instanceof ChessPiece) { 
   piece = (ChessPiece) e.getSource(); 
 
   move = true; 
 
   e = SwingUtilities.convertMouseEvent(piece, e, this); 
  } 
 
  if (e.getSource() == this) { 
   if (move && piece != null) { 
    x = e.getX(); 
    y = e.getY(); 
    if (紅方走棋 && ((piece.棋子種別()).equals(紅方色彩))) { 
     piece.setLocation(x - piece.getWidth() / 2, 
       y - piece.getHeight() / 2); 
    } 
    if (黑方走棋 && (piece.棋子種別().equals(黑方色彩))) { 
     piece.setLocation(x - piece.getWidth() / 2, 
       y - piece.getHeight() / 2); 
    } 
   } 
  } 
 } 
 
 /**松開鼠標事宜*/ 
 public void mouseReleased(MouseEvent e) { 
  ChessPiece piece = null; 
  move = false; 
  Rectangle rect = null; 
  if (e.getSource() instanceof ChessPiece) { 
   piece = (ChessPiece) e.getSource(); 
   rect = piece.getBounds(); 
 
   e = SwingUtilities.convertMouseEvent(piece, e, this); 
  } 
  if (e.getSource() == this) { 
   boolean containChessPoint = false; 
   int x = 0, y = 0; 
   int m = 0, n = 0; 
   if (piece != null) { 
    for (int i = 1; i <= x軸長; i++) { 
     for (int j = 1; j <= y軸長; j++) { 
      x = point[i][j].getX(); 
      y = point[i][j].getY(); 
      if (rect.contains(x, y)) { 
 
       containChessPoint = true; 
       m = i; 
       n = j; 
       break; 
      } 
 
     } 
    } 
   } 
   if (piece != null && containChessPoint) { 
    Color pieceColor = piece.獲得棋子色彩(); 
    if (point[m][n].isPiece()) { 
     Color c = (point[m][n].getPiece()).獲得棋子色彩(); 
     if (pieceColor.getRGB() == c.getRGB()) { 
      piece.setLocation(startX, startY); 
 
      (point[startI][startJ]).set有棋子(true); 
     } else { 
      boolean ok = rule.movePieceRule(piece, startI, startJ, 
        m, n); 
      if (ok) { 
       ChessPiece pieceRemoved = point[m][n].getPiece(); 
       point[m][n].reMovePiece(pieceRemoved, this); 
       point[m][n].setPiece(piece, this); 
       (point[startI][startJ]).set有棋子(false); 
       record.記載棋譜(piece, startI, startJ, m, n); 
       record.記載吃失落的棋子(pieceRemoved); 
       rule.isWine(pieceRemoved); 
       if (piece.棋子種別().equals(紅方色彩)) { 
        紅方走棋 = false; 
        黑方走棋 = true; 
       } 
       if (piece.棋子種別().equals(黑方色彩)) { 
        黑方走棋 = false; 
        紅方走棋 = true; 
       } 
       validate(); 
       repaint(); 
      } else { 
       piece.setLocation(startX, startY); 
       (point[startI][startJ]).set有棋子(true); 
      } 
     } 
 
    } else { 
 
     boolean ok = rule 
       .movePieceRule(piece, startI, startJ, m, n); 
     if (ok) { 
      point[m][n].setPiece(piece, this); 
      (point[startI][startJ]).set有棋子(false); 
      record.記載棋譜(piece, startI, startJ, m, n); 
      record.記載吃失落的棋子("沒吃棋子"); 
 
      if (piece.棋子種別().equals(紅方色彩)) { 
       紅方走棋 = false; 
       黑方走棋 = true; 
      } 
      if (piece.棋子種別().equals(黑方色彩)) { 
       黑方走棋 = false; 
       紅方走棋 = true; 
      } 
     } else { 
      piece.setLocation(startX, startY); 
      (point[startI][startJ]).set有棋子(true); 
     } 
    } 
   } 
 
   if (piece != null && !containChessPoint) { 
    piece.setLocation(startX, startY); 
    (point[startI][startJ]).set有棋子(true); 
   } 
  } 
 } 
 
 public void mouseEntered(MouseEvent e) { 
 } 
 
 public void mouseExited(MouseEvent e) { 
 } 
 
 public void mouseClicked(MouseEvent e) { 
 } 
} 

3.棋子類文件ChessPiece.java

package cn.edu.ouc.chineseChess; 
 
import javax.swing.*; 
import java.awt.*; 
import java.awt.event.*; 
 
/** 
 * 棋子類 
 * 
 * @author cnlht 
 */ 
public class ChessPiece extends JLabel { 
 String name; // 棋子名字 
 Color backColor = null, foreColor;// 配景色和遠景色 
 String 色彩種別 = null; 
 ChessBoard board = null; 
 int width, height;// 年夜小 
 
 public ChessPiece(String name, Color fc, Color bc, int width, int height, 
   ChessBoard board) {// 結構棋子 
  this.name = name; 
  this.board = board; 
  this.width = width; 
  this.height = height; 
  foreColor = fc; 
  backColor = bc; 
  setSize(width, height); 
  setBackground(bc); 
  addMouseMotionListener(board); 
  addMouseListener(board); 
 } 
 
 // 繪制棋子 
 public void paint(Graphics g) {  
  g.drawImage(board.pieceImg, 2, 2, width-2, height-2, null); 
  g.setColor(foreColor); 
  g.setFont(new Font("楷體", Font.BOLD, 26)); 
  g.drawString(name, 7, height - 8);// 在棋子上繪制 “棋子名” 
  g.setColor(Color.black); 
  //g.drawOval(1, 1, width - 1, height - 1); 
  float lineWidth = 2.3f; 
  ((Graphics2D)g).setStroke(new BasicStroke(lineWidth)); 
  ((Graphics2D)g).drawOval(2, 2, width-2, height-2); 
 } 
 
 public int getWidth() { 
  return width; 
 } 
 
 public int getHeight() { 
  return height; 
 } 
 
 public String getName() { 
  return name; 
 } 
 
 public Color 獲得棋子色彩() { 
  return foreColor; 
 } 
 
 public void set棋子種別(String 種別) { 
  色彩種別 = 種別; 
 } 
 
 public String 棋子種別() { 
  return 色彩種別; 
 } 
} 

4.棋子點坐標類文件

package cn.edu.ouc.chineseChess; 
 
/** 
 * 棋點類 
 * 
 * @author cnlht 
 */ 
public class ChessPoint { 
 /** 棋子坐標 */ 
 int x, y; 
  
 /** 該坐標 能否有子*/ 
 boolean 有棋子; 
  
 /** 改坐標的棋子 */ 
 ChessPiece piece = null; 
  
 /** 坐標所屬棋盤 */ 
 ChessBoard board = null; 
 
 public ChessPoint(int x, int y, boolean boo) { 
  this.x = x; 
  this.y = y; 
  有棋子 = boo; 
 } 
 
 public boolean isPiece() { 
  return 有棋子; 
 } 
 
 public void set有棋子(boolean boo) { 
  有棋子 = boo; 
 } 
 
 public int getX() { 
  return x; 
 } 
 
 public int getY() { 
  return y; 
 } 
 
 // 設置改點棋子 
 public void setPiece(ChessPiece piece, ChessBoard board) { 
  this.board = board; 
  this.piece = piece; 
  board.add(piece); 
  int w = (board.unitWidth); 
  int h = (board.unitHeight); 
  piece.setBounds(x - w / 2, y - h / 2, w, h);// 棋子地位,寬度,高度 
  有棋子 = true; 
  board.validate(); 
 } 
 
 public ChessPiece getPiece() { 
  return piece; 
 } 
 
 public void reMovePiece(ChessPiece piece, ChessBoard board) { 
  this.board = board; 
  this.piece = piece; 
  board.remove(piece); 
  board.validate(); 
  有棋子 = false; 
 } 
} 

5.弄法規矩類文件Rule.java

package cn.edu.ouc.chineseChess; 
 
import javax.swing.*; 
 
import java.awt.*; 
import java.awt.event.*; 
 
/** 
 * 走棋規矩類 
 * 
 * @author cnlht 
 */ 
public class Rule { 
 ChessBoard board = null; 
 ChessPiece piece = null; 
 ChessPoint point[][]; 
 int startI, startJ, endI, endJ; 
 
 public Rule(ChessBoard board, ChessPoint point[][]) { 
  this.board = board; 
  this.point = point; 
 } 
 
 public void isWine(ChessPiece piece) { 
  this.piece = piece; 
  if (piece.getName() == "將" || piece.getName() == "帥") { 
   if (piece.色彩種別 == "紅方") { 
    JOptionPane.showMessageDialog(null, "黑方 成功!"); 
   } else { 
    JOptionPane.showMessageDialog(null, "紅方 成功!"); 
   } 
  } 
 } 
 
 public boolean movePieceRule(ChessPiece piece, int startI, int startJ, 
   int endI, int endJ) { 
  this.piece = piece; 
  this.startI = startI; 
  this.startJ = startJ; 
  this.endI = endI; 
  this.endJ = endJ; 
  int minI = Math.min(startI, endI); 
  int maxI = Math.max(startI, endI); 
  int minJ = Math.min(startJ, endJ); 
  int maxJ = Math.max(startJ, endJ); 
  boolean 能否走棋 = false; 
  if (piece.getName().equals("車")) { 
   if (startI == endI) { 
    int j = 0; 
    for (j = minJ + 1; j <= maxJ - 1; j++) { 
     if (point[startI][j].isPiece()) { 
      能否走棋 = false; 
      break; 
     } 
    } 
    if (j == maxJ) { 
     能否走棋 = true; 
    } 
   } else if (startJ == endJ) { 
    int i = 0; 
    for (i = minI + 1; i <= maxI - 1; i++) { 
     if (point[i][startJ].isPiece()) { 
      能否走棋 = false; 
      break; 
     } 
    } 
    if (i == maxI) { 
     能否走棋 = true; 
    } 
   } else { 
    能否走棋 = false; 
   } 
 
  } else if (piece.getName().equals("車")) { 
   if (startI == endI) { 
    int j = 0; 
    for (j = minJ + 1; j <= maxJ - 1; j++) { 
     if (point[startI][j].isPiece()) { 
      能否走棋 = false; 
      break; 
     } 
    } 
    if (j == maxJ) { 
     能否走棋 = true; 
    } 
   } else if (startJ == endJ) { 
    int i = 0; 
    for (i = minI + 1; i <= maxI - 1; i++) { 
     if (point[i][startJ].isPiece()) { 
      能否走棋 = false; 
      break; 
     } 
    } 
    if (i == maxI) { 
     能否走棋 = true; 
    } 
   } else { 
    能否走棋 = false; 
   } 
 
  }else if (piece.getName().equals("馬")) { 
   int xAxle = Math.abs(startI - endI); 
   int yAxle = Math.abs(startJ - endJ); 
 
   if (xAxle == 2 && yAxle == 1) { 
    if (endI > startI) { 
     if (point[startI + 1][startJ].isPiece()) { 
      能否走棋 = false; 
     } else { 
      能否走棋 = true; 
     } 
    } 
    if (endI < startI) { 
     if (point[startI - 1][startJ].isPiece()) { 
      能否走棋 = false; 
     } else { 
      能否走棋 = true; 
     } 
    } 
 
   }else if (xAxle == 1 && yAxle == 2) { 
    if (endJ > startJ) { 
     if (point[startI][startJ + 1].isPiece()) { 
      能否走棋 = false; 
     } else { 
      能否走棋 = true; 
     } 
    } 
    if (endJ < startJ) { 
     if (point[startI][startJ - 1].isPiece()) { 
      能否走棋 = false; 
     } else { 
      能否走棋 = true; 
     } 
    } 
 
   } else { 
    能否走棋 = false; 
   } 
  } else if (piece.getName().equals("馬")) { 
   int xAxle = Math.abs(startI - endI); 
   int yAxle = Math.abs(startJ - endJ); 
 
   if (xAxle == 2 && yAxle == 1) { 
    if (endI > startI) { 
     if (point[startI + 1][startJ].isPiece()) { 
      能否走棋 = false; 
     } else { 
      能否走棋 = true; 
     } 
    } 
    if (endI < startI) { 
     if (point[startI - 1][startJ].isPiece()) { 
      能否走棋 = false; 
     } else { 
      能否走棋 = true; 
     } 
    } 
 
   }else if (xAxle == 1 && yAxle == 2) { 
    if (endJ > startJ) { 
     if (point[startI][startJ + 1].isPiece()) { 
      能否走棋 = false; 
     } else { 
      能否走棋 = true; 
     } 
    } 
    if (endJ < startJ) { 
     if (point[startI][startJ - 1].isPiece()) { 
      能否走棋 = false; 
     } else { 
      能否走棋 = true; 
     } 
    } 
 
   } else { 
    能否走棋 = false; 
   } 
  } else if (piece.getName().equals("象")) { 
   int centerI = (startI + endI) / 2; 
   int centerJ = (startJ + endJ) / 2; 
   int xAxle = Math.abs(startI - endI); 
   int yAxle = Math.abs(startJ - endJ); 
   if (xAxle == 2 && yAxle == 2 && endJ <= 5) { 
    if (point[centerI][centerJ].isPiece()) { 
     能否走棋 = false; 
    } else { 
     能否走棋 = true; 
    } 
   } else { 
    能否走棋 = false; 
   } 
  } else if (piece.getName().equals("相")) { 
   int centerI = (startI + endI) / 2; 
   int centerJ = (startJ + endJ) / 2; 
   int xAxle = Math.abs(startI - endI); 
   int yAxle = Math.abs(startJ - endJ); 
   if (xAxle == 2 && yAxle == 2 && endJ >= 6) { 
    if (point[centerI][centerJ].isPiece()) { 
     能否走棋 = false; 
    } else { 
     能否走棋 = true; 
    } 
   } else { 
    能否走棋 = false; 
   } 
  } else if (piece.getName().equals("炮")) { 
   int number = 0; 
   if (startI == endI) { 
    int j = 0; 
    for (j = minJ + 1; j <= maxJ - 1; j++) { 
     if (point[startI][j].isPiece()) { 
      number++; 
     } 
    } 
    if (number > 1) { 
     能否走棋 = false; 
    } else if (number == 1) { 
     if (point[endI][endJ].isPiece()) { 
      能否走棋 = true; 
     } 
    } else if (number == 0 && !point[endI][endJ].isPiece()) { 
     能否走棋 = true; 
    } 
   } else if (startJ == endJ) { 
    int i = 0; 
    for (i = minI + 1; i <= maxI - 1; i++) { 
     if (point[i][startJ].isPiece()) { 
      number++; 
     } 
    } 
    if (number > 1) { 
     能否走棋 = false; 
    } else if (number == 1) { 
     if (point[endI][endJ].isPiece()) { 
      能否走棋 = true; 
     } 
    } else if (number == 0 && !point[endI][endJ].isPiece()) { 
     能否走棋 = true; 
    } 
   } else { 
    能否走棋 = false; 
   } 
  } else if (piece.getName().equals("兵")) { 
   int xAxle = Math.abs(startI - endI); 
   int yAxle = Math.abs(startJ - endJ); 
 
   if (endJ >= 6) { 
    if (startJ - endJ == 1 && xAxle == 0) { 
     能否走棋 = true; 
    } 
 
    else { 
     能否走棋 = false; 
    } 
   } else if (endJ <= 5) { 
    if ((startJ - endJ == 1) && (xAxle == 0)) { 
     能否走棋 = true; 
    } else if ((endJ - startJ == 0) && (xAxle == 1)) { 
     能否走棋 = true; 
    } else { 
     能否走棋 = false; 
    } 
   } 
  } else if (piece.getName().equals("卒")) { 
   int xAxle = Math.abs(startI - endI); 
   int yAxle = Math.abs(startJ - endJ); 
 
   if (endJ <= 5) { 
    if (endJ - startJ == 1 && xAxle == 0) { 
     能否走棋 = true; 
    } else { 
     能否走棋 = false; 
    } 
   } else if (endJ >= 6) { 
    if ((endJ - startJ == 1) && (xAxle == 0)) { 
     能否走棋 = true; 
    } else if ((endJ - startJ == 0) && (xAxle == 1)) { 
     能否走棋 = true; 
    } else { 
     能否走棋 = false; 
    } 
   } 
  } 
 
  else if (piece.getName().equals("士")) { 
   int xAxle = Math.abs(startI - endI); 
   int yAxle = Math.abs(startJ - endJ); 
   if (endI <= 6 && endI >= 4 && xAxle == 1 && yAxle == 1) { 
    能否走棋 = true; 
   } else { 
    能否走棋 = false; 
   } 
  } else if (piece.getName().equals("仕")) { 
   int xAxle = Math.abs(startI - endI); 
   int yAxle = Math.abs(startJ - endJ); 
   if (endI <= 6 && endI >= 4 && xAxle == 1 && yAxle == 1) { 
    能否走棋 = true; 
   } else { 
    能否走棋 = false; 
   } 
  } else if ((piece.getName().equals("帥")) 
    || (piece.getName().equals("將"))) { 
   int xAxle = Math.abs(startI - endI); 
   int yAxle = Math.abs(startJ - endJ); 
   if (endI <= 6 && endI >= 4) { 
    if ((xAxle == 1 && yAxle == 0) || (xAxle == 0 && yAxle == 1)) { 
     能否走棋 = true; 
    } else { 
     能否走棋 = false; 
    } 
   } else { 
    能否走棋 = false; 
   } 
  } 
 
  return 能否走棋; 
 
 } 
} 

6.走步類文件MoveStep.java

package cn.edu.ouc.chineseChess; 
 
import java.awt.Point; 
 
/** 
 * 走步類 
 * 
 * @author cnlht 
 * 
 */ 
public class MoveStep implements java.io.Serializable { 
 public Point pStart, pEnd; 
 
 public MoveStep(Point p1, Point p2) { 
  pStart = p1; 
  pEnd = p2; 
 } 
} 

7.制造棋譜類MakeChessManual.java

package cn.edu.ouc.chineseChess; 
 
import javax.swing.*; 
import java.awt.*; 
import java.awt.event.*; 
import java.util.LinkedList; 
 
/** 
 * 制造棋譜類 
 * 
 * @author cnlht 
 */ 
public class MakeChessManual extends JPanel implements ActionListener { 
 JTextArea text = null; 
 JScrollPane scroll = null; 
 ChessBoard board = null; 
 ChessPoint[][] point; 
 LinkedList 棋譜 = null; 
 LinkedList 吃失落的棋子 = null; 
 JButton buttonUndo; 
 int i = 0; 
 
 public MakeChessManual(ChessBoard board, ChessPoint[][] point) { 
  this.board = board; 
  this.point = point; 
  text = new JTextArea(); 
  scroll = new JScrollPane(text); 
  棋譜 = new LinkedList(); 
  吃失落的棋子 = new LinkedList(); 
  buttonUndo = new JButton("悔棋"); 
  buttonUndo.setFont(new Font("隸書", Font.PLAIN, 18)); 
  setLayout(new BorderLayout()); 
  add(scroll, BorderLayout.CENTER); 
  add(buttonUndo, BorderLayout.SOUTH); 
  buttonUndo.addActionListener(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 記載棋譜(ChessPiece piece, int startI, int startJ, int endI, 
   int endJ) { 
  Point pStart = new Point(startI, startJ); 
  Point pEnd = new Point(endI, endJ); 
  MoveStep step = new MoveStep(pStart, pEnd); 
  棋譜.add(step); 
 
  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 記載吃失落的棋子(Object object) { 
  吃失落的棋子.add(object); 
 } 
 
 public LinkedList 獲得棋譜() { 
  return 棋譜; 
 } 
 
 public void actionPerformed(ActionEvent e) { 
  int position = text.getText().lastIndexOf("#"); 
  if (position != -1) 
   text.replaceRange("", position, text.getText().length()); 
  if (棋譜.size() > 0) { 
   MoveStep lastStep = (MoveStep) 棋譜.getLast(); 
   棋譜.removeLast(); 
   Object qizi = 吃失落的棋子.getLast(); 
   吃失落的棋子.removeLast(); 
   String temp = qizi.toString(); 
   if (temp.equals("沒吃棋子")) { 
    int startI = lastStep.pStart.x; 
    int startJ = lastStep.pStart.y; 
    int endI = lastStep.pEnd.x; 
    int endJ = lastStep.pEnd.y; 
    ChessPiece piece = point[endI][endJ].getPiece(); 
 
    point[startI][startJ].setPiece(piece, board); 
    (point[endI][endJ]).set有棋子(false); 
 
    if (piece.棋子種別().equals(board.紅方色彩)) { 
     board.紅方走棋 = true; 
     board.黑方走棋 = false; 
    } 
    if (piece.棋子種別().equals(board.黑方色彩)) { 
     board.黑方走棋 = true; 
     board.紅方走棋 = false; 
    } 
   } else { 
    ChessPiece removedPiece = (ChessPiece) qizi; 
    int startI = lastStep.pStart.x; 
    int startJ = lastStep.pStart.y; 
    int endI = lastStep.pEnd.x; 
    int endJ = lastStep.pEnd.y; 
    ChessPiece piece = point[endI][endJ].getPiece(); 
    point[startI][startJ].setPiece(piece, board); 
    point[endI][endJ].setPiece(removedPiece, board); 
    (point[endI][endJ]).set有棋子(true); 
 
    if (piece.棋子種別().equals(board.紅方色彩)) { 
     board.紅方走棋 = true; 
     board.黑方走棋 = false; 
    } 
    if (piece.棋子種別().equals(board.黑方色彩)) { 
     board.黑方走棋 = true; 
     board.紅方走棋 = false; 
    } 
   } 
  } 
 } 
} 

8.演示棋譜類文件Demon.java

package cn.edu.ouc.chineseChess; 
 
import javax.swing.*; 
import java.awt.*; 
import java.awt.event.*; 
import java.util.*; 
 
/** 
 * 演示棋譜類 
 * 
 * @author cnlht 
 */ 
public class Demon extends JPanel implements ActionListener, Runnable { 
 public JButton replay = null, next = null, auto = null, stop = null; 
 LinkedList 棋譜 = null; 
 Thread 主動演示 = null; 
 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(時光距離); 
  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 { 
    演示停止("棋譜演示終了"); 
   } 
  } 
  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("持續演示"); 
    stop.repaint(); 
   } 
   if (e.getActionCommand().equals("持續演示")) { 
    演示進程 = "持續演示"; 
    主動演示.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()) { 
   演示停止("棋譜演示終了"); 
   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 演示停止(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(); 
 } 
} 

4、總結與請求
1.懂得8個文件,沒有太龐雜的代碼。
2.懂得鼠標的MouseListener,MouseMotionListener兩個接口的差別,五子棋的完成不須要MouseMotionListener。
3.應用LinkedList記載棋譜的辦法。

願望年夜家愛好這篇文章,制造一款屬於本身的中國象棋游戲。

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved