欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2024-03-09 11:25:11
本文实例讲述了java实现的中国象棋游戏代码,分享给大家供大家参考,具体代码如下 一、实践目的: 1.鼠标点击、拖动等事件的应用与区别 2.棋谱文件的保存与读取 3...

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

一、实践目的:

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

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

3.完善象棋的规则。

二、实践内容:

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

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

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

人人对弈图

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

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

选择“制作棋谱”菜单

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

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

棋谱制作完毕红方胜出

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

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

保存棋谱对话框

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

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

演示棋谱对话框

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

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

三、参考代码:

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(); 
 } 
} 

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

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