java中国象棋代码77.docx
java中国象棋代码77import java.awt.*; import java.awt.event.*; import javax.swing.*; import java.util.*; import java.io.*; /主类 public class Chess public static void main(String args) new ChessMainFrame("中国象棋:观棋不语真君子,棋死无悔大丈夫"); /主框架类 class ChessMainFrame extends JFrame implements ActionListener,MouseListener,Runnable /玩家 JLabel play = new JLabel32; /棋盘 JLabel image; /窗格 Container con; /工具栏 JToolBar jmain; /重新开始 JButton anew; /悔棋 JButton repent; /退出 JButton exit; /当前信息 JLabel text; /保存当前操作 Vector Var; /规则类对象(使于调用方法) ChessRule rule; /* * 单击棋子 * chessManClick = true 闪烁棋子 并给线程响应 * chessManClick = false 吃棋子 停止闪烁 并给线程响应 */ boolean chessManClick; /* * 控制玩家走棋 * chessPlayClick=1 黑棋走棋 * chessPlayClick=2 红棋走棋 默认红棋 * chessPlayClick=3 双方都不能走棋 */ int chessPlayClick=2; /控制棋子闪烁的线程 Thread tmain; /把第一次的单击棋子给线程响应 static int Man,i; ChessMainFrame new ChessMainFrame("中国象棋"); /* * 构造函数 * 初始化图形用户界面 */ ChessMainFrame(String Title) /获行客格引用 con = this.getContentPane; con.setLayout(null); /实例化规则类 rule = new ChessRule; Var = new Vector; /创建工具栏 jmain = new JToolBar; text = new JLabel("欢迎使用象棋对弈系统"); /当鼠标放上显示信息 text.setToolTipText("信息提示"); anew = new JButton(" 新 游 戏 "); anew.setToolTipText("重新开始新的一局"); exit = new JButton(" 退 出 "); exit.setToolTipText("退出象棋程序程序"); repent = new JButton(" 悔 棋 "); repent.setToolTipText("返回到上次走棋的位置"); /把组件添加到工具栏 jmain.setLayout(new GridLayout(0,4); jmain.add(anew); jmain.add(repent); jmain.add(exit); jmain.add(text); jmain.setBounds(0,0,558,30); con.add(jmain); /添加棋子标签 drawChessMan; /注册按扭监听 anew.addActionListener(this); repent.addActionListener(this); exit.addActionListener(this); /注册棋子移动监听 for (int i=0;i<32;i+) con.add(playi); playi.addMouseListener(this); /添加棋盘标签 con.add(image = new JLabel(new ImageIcon("imageMain.GIF"); image.setBounds(0,30,558,620); image.addMouseListener(this); /注册窗体关闭监听 this.addWindowListener( new WindowAdapter public void windowClosing(WindowEvent we) System.exit(0); ); /窗体居中 Dimension screenSize = Toolkit.getDefaultToolkit.getScreenSize; Dimension frameSize = this.getSize; if (frameSize.height > screenSize.height) frameSize.height = screenSize.height; if (frameSize.width > screenSize.width) frameSize.width = screenSize.width; this.setLocation(screenSize.width - frameSize.width) / 2 - 280 ,(screenSize.height - frameSize.height ) / 2 - 350); /设置 this.setIconImage(new ImageIcon("image红将.GIF").getImage); this.setResizable(false); this.setTitle(Title); this.setSize(558,670); this.show; /* * 添加棋子方法 */ public void drawChessMan /流程控制 int i,k; /图标 Icon in; /黑色棋子 /车 in = new ImageIcon("image黑车.GIF"); for (i=0,k=24;i<2;i+,k+=456) playi = new JLabel(in); playi.setBounds(k,56,55,55); playi.setName("车1"); /马 in = new ImageIcon("image黑马.GIF"); for (i=4,k=81;i<6;i+,k+=342) playi = new JLabel(in); playi.setBounds(k,56,55,55); playi.setName("马1"); /相 in = new ImageIcon("image黑象.GIF"); for (i=8,k=138;i<10;i+,k+=228) playi = new JLabel(in); playi.setBounds(k,56,55,55); playi.setName("象1"); /士 in = new ImageIcon("image黑士.GIF"); for (i=12,k=195;i<14;i+,k+=114) playi = new JLabel(in); playi.setBounds(k,56,55,55); playi.setName("士1"); /卒 in = new ImageIcon("image黑卒.GIF"); for (i=16,k=24;i<21;i+,k+=114) playi = new JLabel(in); playi.setBounds(k,227,55,55); playi.setName("卒1" + i); /炮 in = new ImageIcon("image黑炮.GIF"); for (i=26,k=81;i<28;i+,k+=342) playi = new JLabel(in); playi.setBounds(k,170,55,55); playi.setName("炮1" + i); /将 in = new ImageIcon("image黑将.GIF"); play30 = new JLabel(in); play30.setBounds(252,56,55,55); play30.setName("将1"); /红色棋子 /车 in = new ImageIcon("image红车.GIF"); for (i=2,k=24;i<4;i+,k+=456) playi = new JLabel(in); playi.setBounds(k,569,55,55); playi.setName("车2"); /马 in = new ImageIcon("image红马.GIF"); for (i=6,k=81;i<8;i+,k+=342) playi = new JLabel(in); playi.setBounds(k,569,55,55); playi.setName("马2"); /相 in = new ImageIcon("image红象.GIF"); for (i=10,k=138;i<12;i+,k+=228) playi = new JLabel(in); playi.setBounds(k,569,55,55); playi.setName("象2"); /士 in = new ImageIcon("image红士.GIF"); for (i=14,k=195;i<16;i+,k+=114) playi = new JLabel(in); playi.setBounds(k,569,55,55); playi.setName("士2"); /兵 in = new ImageIcon("image红卒.GIF"); for (i=21,k=24;i<26;i+,k+=114) playi = new JLabel(in); playi.setBounds(k,398,55,55); playi.setName("卒2" + i); /炮 in = new ImageIcon("image红炮.GIF"); for (i=28,k=81;i<30;i+,k+=342) playi = new JLabel(in); playi.setBounds(k,455,55,55); playi.setName("炮2" + i); /帅 in = new ImageIcon("image红将.GIF"); play31 = new JLabel(in); play31.setBounds(252,569,55,55); play31.setName("帅2"); /* * 线程方法控制棋子闪烁 */ public void run while (true) /单击棋子第一下开始闪烁 if (chessManClick) playMan.setVisible(false); /时间控制 try tmain.sleep(200); catch(Exception e) playMan.setVisible(true); /闪烁当前提示信息 以免用户看不见 else text.setVisible(false); /时间控制 try tmain.sleep(250); catch(Exception e) text.setVisible(true); try tmain.sleep(350); catch (Exception e) /* * 单击棋子方法 */ public void mouseClicked(MouseEvent me) System.out.println("Mouse"); /当前坐标 int Ex=0,Ey=0; /启动线程 if (tmain = null) tmain = new Thread(this); tmain.start; /单击棋盘(移动棋子) if (me.getSource.equals(image) /该红棋走棋的时候 if (chessPlayClick = 2 && playMan.getName.charAt(1) = '2') Ex = playMan.getX; Ey = playMan.getY; /移动卒、兵 if (Man > 15 && Man < 26) rule.armsRule(Man,playMan,me); /移动炮 else if (Man > 25 && Man < 30) rule.cannonRule(playMan,play,me); /移动车 else if (Man >=0 && Man < 4) rule.cannonRule(playMan,play,me); /移动马 else if (Man > 3 && Man < 8) rule.horseRule(playMan,play,me); /移动相、象 else if (Man > 7 && Man < 12) rule.elephantRule(Man,playMan,play,me); /移动仕、士 else if (Man > 11 && Man < 16) rule.chapRule(Man,playMan,play,me); /移动将、帅 else if (Man = 30 | Man = 31) rule.willRule(Man,playMan,play,me); /是否走棋错误(是否在原地没有动) if (Ex = playMan.getX && Ey = playMan.getY) text.setText(" 红棋走棋"); chessPlayClick=2; else text.setText(" 黑棋走棋"); chessPlayClick=1; /if /该黑棋走棋的时候 else if (chessPlayClick = 1 && playMan.getName.charAt(1) = '1') Ex = playMan.getX; Ey = playMan.getY; /移动卒、兵 if (Man > 15 && Man < 26) rule.armsRule(Man,playMan,me); /移动炮 else if (Man > 25 && Man < 30) rule.cannonRule(playMan,play,me); /移动车 else if (Man >=0 && Man < 4) rule.cannonRule(playMan,play,me); /移动马 else if (Man > 3 && Man < 8) rule.horseRule(playMan,play,me); /移动相、象 else if (Man > 7 && Man < 12) rule.elephantRule(Man,playMan,play,me); /移动仕、士 else if (Man > 11 && Man < 16) rule.chapRule(Man,playMan,play,me); /移动将、帅 else if (Man = 30 | Man = 31) rule.willRule(Man,playMan,play,me); /是否走棋错误(是否在原地没有动) if (Ex = playMan.getX && Ey = playMan.getY) text.setText(" 黑棋走棋"); chessPlayClick=1; else text.setText(" 红棋走棋"); chessPlayClick=2; /else if /当前没有操作(停止闪烁) chessManClick=false; /if /单击棋子 else /第一次单击棋子(闪烁棋子) if (!chessManClick) for (int i=0;i<32;i+) /被单击的棋子 if (me.getSource.equals(playi) /告诉线程让该棋子闪烁 Man=i; /开始闪烁 chessManClick=true; break; /for /if /第二次单击棋子(吃棋子) else if (chessManClick) /当前没有操作(停止闪烁) chessManClick=false; for (i=0;i<32;i+) /找到被吃的棋子 if (me.getSource.equals(playi) /该红棋吃棋的时候 if (chessPlayClick = 2 && playMan.getName.charAt(1) = '2') Ex = playMan.getX; Ey = playMan.getY; /卒、兵吃规则 if (Man > 15 && Man < 26) rule.armsRule(playMan,playi); /炮吃规则 else if (Man > 25 && Man < 30) rule.cannonRule(0,playMan,playi,play,me); /车吃规则 else if (Man >=0 && Man < 4) rule.cannonRule(1,playMan,playi,play,me); /马吃规则 else if (Man > 3 && Man < 8) rule.horseRule(playMan,playi,play,me); /相、象吃规则 else if (Man > 7 && Man < 12) '1') rule.elephantRule(playMan,playi,play); /士、仕吃棋规则 else if (Man > 11 && Man < 16) rule.chapRule(Man,playMan,playi,play); /将、帅吃棋规则 else if (Man = 30 | Man = 31) rule.willRule(Man,playMan,playi,play); playMan.setVisible(true); /是否走棋错误(是否在原地没有动) if (Ex = playMan.getX && Ey = playMan.getY) text.setText(" 红棋走棋"); chessPlayClick=2; break; else text.setText(" 黑棋走棋"); chessPlayClick=1; break; /if /该黑棋吃棋的时候 else if (chessPlayClick = 1 && playMan.getName.charAt(1) = Ex = playMan.getX; Ey = playMan.getY; /卒吃规则 if (Man > 15 && Man < 26) rule.armsRule(playMan,playi); /炮吃规则 else if (Man > 25 && Man < 30) rule.cannonRule(0,playMan,playi,play,me); /车吃规则 else if (Man >=0 && Man < 4) rule.cannonRule(1,playMan,playi,play,me); /马吃规则 else if (Man > 3 && Man < 8) rule.horseRule(playMan,playi,play,me); /相、象吃规则 else if (Man > 7 && Man < 12) rule.elephantRule(playMan,playi,play); /士、仕吃棋规则 else if (Man > 11 && Man < 16) rule.chapRule(Man,playMan,playi,play); /将、帅吃棋规则 else if (Man = 30 | Man = 31) rule.willRule(Man,playMan,playi,play); playMan.setVisible(true); /是否走棋错误(是否在原地没有动) if (Ex = playMan.getX && Ey = playMan.getY) text.setText(" 黑棋走棋"); chessPlayClick=1; break; else text.setText(" 红棋走棋"); chessPlayClick=2; break; /else if /if /for /是否胜利 if (!play31.isVisible) JOptionPane.showConfirmDialog( this,"黑棋胜利","玩家一胜利", JOptionPane.DEFAULT_OPTION,JOptionPane.WARNING_MESSAGE); /双方都不可以在走棋了 chessPlayClick=3; text.setText(" 黑棋胜利"); /if else if (!play30.isVisible) JOptionPane.showConfirmDialog( this,"红棋胜利","玩家二胜利", JOptionPane.DEFAULT_OPTION,JOptionPane.WARNING_MESSAGE); chessPlayClick=3; text.setText(" 红棋胜利"); /else if /else /else public void mousePressed(MouseEvent me) public void mouseReleased(MouseEvent me) public void mouseEntered(MouseEvent me) public void mouseExited(MouseEvent me) /* * 定义按钮的事件响应 */ public void actionPerformed(ActionEvent ae) /重新开始按钮 if (ae.getSource.equals(anew) int i,k; /重新排列每个棋子的位置 /黑色棋子 /车 for (i=0,k=24;i<2;i+,k+=456) playi.setBounds(k,56,55,55); /马 for (i=4,k=81;i<6;i+,k+=342) playi.setBounds(k,56,55,55); /相 for (i=8,k=138;i<10;i+,k+=228) playi.setBounds(k,56,55,55); /士 for (i=12,k=195;i<14;i+,k+=114) playi.setBounds(k,56,55,55); /卒 for (i=16,k=24;i<21;i+,k+=114) playi.setBounds(k,227,55,55); /炮 for (i=26,k=81;i<28;i+,k+=342) playi.setBounds(k,170,55,55); /将 play30.setBounds(252,56,55,55); /红色棋子 /车 for (i=2,k=24;i<4;i+,k+=456) playi.setBounds(k,569,55,55); /马 for (i=6,k=81;i<8;i+,k+=342) playi.setBounds(k,569,55,55); /相 for (i=10,k=138;i<12;i+,k+=228) playi.setBounds(k,569,55,55); /士 for (i=14,k=195;i<16;i+,k+=114) playi.setBounds(k,569,55,55); /兵 for (i=21,k=24;i<26;i+,k+=114) playi.setBounds(k,398,55,55); /炮 for (i=28,k=81;i<30;i+,k+=342) playi.setBounds(k,455,55,55); /帅 play31.setBounds(252,569,55,55); chessPlayClick = 2; text.setText(" 红棋走棋"); for (i=0;i<32;i+) playi.setVisible(true); /清除Vector中的内容 Var.clear; /悔棋按钮 else if (ae.getSource.equals(repent) try /获得setVisible属性值 String S = (String)Var.get(Var.size-4); /获得X坐标 int x = Integer.parseInt(String)Var.get(Var.size-3); /获得Y坐标 int y = Integer.parseInt(String)Var.get(Var.size-2); /获得索引 int M = Integer.parseInt(String)Var.get(Var.size-1); /赋给棋子 playM.setVisible(true); playM.setBounds(x,y,55,55); if (playM.getName.charAt(1) = '1') text.setText(" 黑棋走棋"); chessPlayClick = 1; else text.setText(" 红棋走棋"); chessPlayClick = 2; /删除用过的坐标 Var.remove(Var.size-4); Var.remove(Var.size-3); Var.remove(Var.size-2); Var.remove(Var.size-1); /停止旗子闪烁 chessManClick=false; catch(Exception e) /退出 else if (ae.getSource.equals(exit) int j=JOptionPane.showConfirmDialog( this,"真的要退出吗?","退出", JOptionPane.YES_OPTION,JOptionPane.QUESTION_MESSAGE); if (j = JOptionPane.YES_OPTION) System.exit(0); /*定义中国象棋规则的类*/ class ChessRule /*卒子的移动规则*/ public void armsRule(int Man,JLabel play,MouseEvent me) /黑卒向下 if (Man < 21) /向下移动、得到终点的坐标模糊成合法的坐标 if (me.getY-play.getY) > 27 && (me.getY-play.getY) < 86 && (me.getX-play.getX) < 55 && (me.getX-play.getX) > 0) /当前记录添加到集合(用于悔棋) Var.add(String.valueOf(play.isVisible); Var.add(String.valueOf(play.getX); Var.add(String.valueOf(play.getY); Var.add(String.valueOf(Man); play.setBounds(play.getX,play.getY+57,55,55); /向右移动、得到终点的坐标模糊成合法的坐标、必须过河 else if (play.getY > 284 && (me.getX - play.getX) >= 57 && (me.getX - play.getX) <= 112) play.setBounds(play.getX+57,play.getY,55,55); /向左移动、得到终点的坐标模糊成合法的坐标、必须过河 else if (play.getY > 284 && (play.getX - me.getX) >= 2 && (play.getX - me.getX) <=58) /模糊坐标 play.setBounds(play.getX-57,play.getY,55,55); /红卒向上 else /当前记录添加到集合(用于悔棋) Var.add(String.valueOf(play.isVisible); Var.add(String.valueOf(play.getX); Var.add(String.valueOf(play.getY); Var.add(String.valueOf(Man); /向上移动、得到终点的坐标模糊成合法的坐标 if (me.getX-play.getX) >= 0 && (me.getX-play.getX) <= 55 && (play.getY-me.getY) >27 && play.getY-me.getY < 86) play.setBounds(play.getX,play.getY-57,55,55); /向右移动、得到终点的坐标模糊成合法的坐标、必须过河 else if (play.g