【Java精品源码栏目提醒】:本文主要为网学会员提供“【精品】中国象棋源代码JAVA程序 - 其它资料”,希望对需要【精品】中国象棋源代码JAVA程序 - 其它资料网友有所帮助,学习一下!
import java.awt.import java.awt.event.import javax.swing.import java.util.import java.io.public class Chess public static void mainString args new ChessMainFramequot中国象棋:观棋不语真君子,棋死无悔大丈夫quot class ChessMainFrame extends JFrame implementsActionListenerMouseListenerRunnable //玩家 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/ 控制玩家走棋 chessPlayClick1 黑棋走棋 chessPlayClick2 红棋走棋 默认红棋 chessPlayClick3 双方都不能走棋/int chessPlayClick2//控制棋子闪烁的线程Thread tmain//把第一次的单击棋子给线程响应static int ManiChessMainFrame new ChessMainFramequot中国象棋quot/ 构造函数 初始化图形用户界面/ChessMainFrameString Title //获行客格引用 con this.getContentPane con.setLayoutnull //实例化规则类 rule new ChessRule Var new Vector //创建工具栏 jmain new JToolBar text new JLabelquot欢迎使用象棋对弈系统quot //当鼠标放上显示信息 text.setToolTipTextquot信息提示quot anew new JButtonquot 新 游 戏 quot anew.setToolTipTextquot重新开始新的一局quot exit new JButtonquot 退 出 quot exit.setToolTipTextquot退出象棋程序程序quot repent new JButtonquot 悔 棋 quot repent.setToolTipTextquot返回到上次走棋的位置quot //把组件添加到工具栏 jmain.setLayoutnew GridLayout04 jmain.addanewjmain.addrepentjmain.addexitjmain.addtextjmain.setBounds0055830con.addjmain//添加棋子标签drawChessMan//注册按扭监听anew.addActionListenerthisrepent.addActionListenerthisexit.addActionListenerthis//注册棋子移动监听for int i0ilt32i con.addplayi playi.addMouseListenerthis//添加棋盘标签con.addimage new JLabelnew ImageIconquotimageMain.GIFquotimage.setBounds030558620image.addMouseListenerthis//注册窗体关闭监听this.addWindowListener new WindowAdapter public void windowClosingWindowEvent we System.exit0 //窗体居中Dimension screenSize Toolkit.getDefaultToolkit.getScreenSizeDimension frameSize this.getSizeif frameSize.height gt screenSize.height frameSize.height screenSize.heightif frameSize.width gt screenSize.width frameSize.width screenSize.width this.setLocationscreenSize.width - frameSize.width / 2 -280 screenSize.height - frameSize.height / 2 - 350 //设置 this.setIconImagenew ImageIconquotimage红将.GIFquot.getImage this.setResizablefalse this.setTitleTitle this.setSize558670 this.show / 添加棋子方法 / public void drawChessMan //流程控制 int ik //图标 Icon in //黑色棋子 //车 in new ImageIconquotimage黑车.GIFquot for i0k24ilt2ik456 playi new JLabelin playi.setBoundsk565555 playi.setNamequot车 1quot //马 in new ImageIconquotimage黑马.GIFquot for i4k81ilt6ik342 playi new JLabelin playi.setBoundsk565555 playi.setNamequot马 1quot //相 in new ImageIconquotimage黑象.GIFquot for i8k138ilt10ik228 playi new JLabelin playi.setBoundsk565555 playi.setNamequot象 1quot//士in new ImageIconquotimage黑士.GIFquotfor i12k195ilt14ik114 playi new JLabelin playi.setBoundsk565555 playi.setNamequot士 1quot//卒in new ImageIconquotimage黑卒.GIFquotfor i16k24ilt21ik114 playi new JLabelin playi.setBoundsk2275555 playi.setNamequot卒 1quot i//炮in new ImageIconquotimage黑炮.GIFquotfor i26k81ilt28ik342 playi new JLabelin playi.setBoundsk1705555 playi.setNamequot炮 1quot i//将in new ImageIconquotimage黑将.GIFquotplay30 new JLabelinplay30.setBounds252565555play30.setNamequot将 1quot//红色棋子//车in new ImageIconquotimage红车.GIFquotfor i2k24ilt4ik456 playi new JLabelin playi.setBoundsk5695555 playi.setNamequot车 2quot//马in new ImageIconquotimage红马.GIFquot for i6k81ilt8ik342 playi new JLabelin playi.setBoundsk5695555 playi.setNamequot马 2quot //相 in new ImageIconquotimage红象.GIFquot for i10k138ilt12ik228 playi new JLabelin playi.setBoundsk5695555 playi.setNamequot象 2quot //士 in new ImageIconquotimage红士.GIFquot for i14k195ilt16ik114 playi new JLabelin playi.setBoundsk5695555 playi.setNamequot士 2quot //兵 in new ImageIconquotimage红卒.GIFquot for i21k24ilt26ik114 playi new JLabelin playi.setBoundsk3985555 playi.setNamequot卒 2quot i //炮 in new ImageIconquotimage红炮.GIFquot for i28k81ilt30ik342 playi new JLabelin playi.setBoundsk4555555 playi.setNamequot炮 2quot i //帅 in new ImageIconquotimage红将.GIFquot play31 new JLabelin play31.setBounds2525695555 play31.setNamequot帅 2quot/ 线程方法控制棋子闪烁/public void run while true //单击棋子第一下开始闪烁 if chessManClick playMan.setVisiblefalse //时间控制 try tmain.sleep200 catchException e playMan.setVisibletrue //闪烁当前提示信息 以免用户看不见 else text.setVisiblefalse //时间控制 try tmain.sleep250 catchException e text.setVisibletrue try tmain.sleep350 catch Exception e / 单击棋子方法/public void mouseClickedMouseEvent me System.out.printlnquotMousequot //当前坐标 int Ex0Ey0 //启动线程 if tmain null tmain new Threadthis tmain.start //单击棋盘移动棋子 if me.getSource.equalsimage //该红棋走棋的时候 if chessPlayClick 2 ampamp playMan.getName.charAt1 2 Ex playMan.getX Ey playMan.getY //移动卒、兵 if Man gt 15 ampamp Man lt 26 rule.armsRuleManplayManme //移动炮 else if Man gt 25 ampamp Man lt 30 rule.cannonRuleplayManplayme //移动车 else if Man gt0 ampamp Man lt 4 rule.cannonRuleplayManplayme //移动马 else if Man gt 3 ampamp Man lt 8 rule.horseRuleplayManplayme //移动相、象 else if Man gt 7 ampamp Man lt 12 rule.elephantRuleManplayManplayme //移动仕、士 else if Man gt 11 ampamp Man lt 16 rule.chapRuleManplayManplayme //移动将、帅 else if Man 30 Man 31 rule.willRuleManplayManplayme //是否走棋错误是否在原地没有动 if Ex playMan.getX ampamp Ey playMan.getY text.setTextquot 红棋走棋quot chessPlayClick2 else text.setTextquot 黑棋走棋quot chessPlayClick1 //if //该黑棋走棋的时候 else if chessPlayClick 1 ampamp playMan.getName.charAt1 1 Ex playMan.getX Ey playMan.getY //移动卒、兵 if Man gt 15 ampamp Man lt 26 rule.armsRuleManplayManme //移动炮 else if Man gt 25 ampamp Man lt 30 rule.cannonRuleplayManplayme //移动车 else if Man gt0 ampamp Man lt 4 rule.cannonRuleplayManplayme //移动马 else if Man gt 3 ampamp Man lt 8 rule.horseRuleplayManplayme //移动相、象 else if Man gt 7 ampamp Man lt 12 rule.elephantRuleManplayManplayme //移动仕、士 else if Man gt 11 ampamp Man lt 16 rule.chapRuleManplayManplayme //移动将、帅 else if Man 30 Man 31 rule.willRuleManplayManplayme //是否走棋错误是否在原地没有动 if Ex playMan.getX ampamp Ey playMan.getY text.setTextquot 黑棋走棋quot chessPlayClick1 else text.setTextquot 红棋走棋quot chessPlayClick2 //else if //当前没有操作停止闪烁 chessManClickfalse//if//单击棋子else //第一次单击棋子闪烁棋子 if chessManClick for int i0ilt32i //被单击的棋子 if me.getSource.equalsplayi //告诉线程让该棋子闪烁 Mani //开始闪烁 chessManClicktrue break //for //if //第二次单击棋子吃棋子 else if chessManClick //当前没有操作停止闪烁 chessManClickfalse for i0ilt32i //找到被吃的棋子 if me.getSource.equalsplayi //该红棋吃棋的时候 if chessPlayClick 2 ampamp playMan.getName.charAt1 2 Ex playMan.getX Ey playMan.getY //卒、兵吃规则 if Man gt 15 ampamp Man lt 26 rule.armsRuleplayManplayi //炮吃规则 else if Man gt 25 ampamp Man lt 30 rule.cannonRule0playManplayiplayme //车吃规则 else if Man gt0 ampamp Man lt 4 rule.cannonRule1playManplayiplayme //马吃规则 else if Man gt 3 ampamp Man lt 8 rule.horseRuleplayManplayiplayme //相、象吃规则 else if Man gt 7 ampamp Man lt 12 rule.elephantRuleplayManplayiplay //士、仕吃棋规则 else if Man gt 11 ampamp Man lt 16 rule.chapRuleManplayManplayiplay //将、帅吃棋规则 else if Man 30 Man 31 rule.willRuleManplayManplayiplay playMan.setVisibletrue //是否走棋错误是否在原地没有动 if Ex playMan.getX ampamp Ey playMan.getY text.setTextquot 红棋走棋quot chessPlayClick2 break else text.setTextquot 黑棋走棋quot chessPlayClick1 break //if //该黑棋吃棋的时候 else if chessPlayClick 1 ampampplayMan.getName.charAt1 1 Ex playMan.getX Ey playMan.getY //卒吃规则 if Man gt 15 ampamp Man lt 26 rule.armsRuleplayManplayi //炮吃规则 else if Man gt 25 ampamp Man lt 30 rule.cannonRule0playManplayiplayme //车吃规则 else if Man gt0 ampamp Man lt 4 rule.cannonRule1playManplayiplayme //马吃规则 else if Man gt 3 ampamp Man lt 8 rule.horseRuleplayManplayiplayme //相、象吃规则 else if Man gt 7 ampamp Man lt 12 rule.elephantRuleplayManplayiplay //士、仕吃棋规则 else if Man gt 11 ampamp Man lt 16 rule.chapRuleManplayManplayiplay //将、帅吃棋规则 else if Man 30 Man 31 rule.willRuleManplayManplayiplay playMan.setVisibletrue //是否走棋错误是否在原地没有动 if Ex playMan.getX ampamp Ey playMan.getY text.setTextquot 黑棋走棋quot chessPlayClick1 break else text.setTextquot 红棋走棋quot chessPlayClick2 break //else if //if //for //是否胜利 if play31.isVisible JOptionPane.showConfirmDialog thisquot黑棋胜利quotquot玩家一胜利quotJOptionPane.DEFAULT_OPTIONJOptionPane.WARNING_MESSAGE //双方都不可以在走棋了 chessPlayClick3 text.setTextquot 黑棋胜利quot //if else if play30.isVisible JOptionPane.showConfirmDialog thisquot红棋胜利quotquot玩家二胜利quotJOp.