【Java精品源码栏目提醒】:文章导读:在新的一年中,各位网友都进入紧张的学习或是工作阶段。
网学会员整理了Java精品源码-JAVA课程设计黑白棋源代码-课程设计 - 行政公文的相关内容供大家参考,祝大家在新的一年里工作和学习顺利!
JAVA课程设计黑白棋源代码
java课程设计数据库课程设计计算机课程设计
精品课程网站设计 import
java.awt. import javax.swing. import
java.awt.event. //建立一个大框架用来放置棋盘面板 class Game extends JFrame int Max//定义的棋盘大小为Max × Max的正方形 boolean ChessCheck false//定义一个变量,用来设置棋子是否能下在所在位置 boolean IamFirst true//定义一个变量,用来判断轮到谁下棋,黑棋为true白棋为false JPanel mainPanel//定义主面板 Object options quot确定quot //警告框按钮 String msg//警告框信息 public Game superquot黑白棋(二人对战)quot//窗口标题 setDefaultCloseOperationJFrame.EXIT_ON_CLOSE//默认窗口关闭按钮 setLocation200 200 //设置组件位置XY坐标 Image frame_icon Toolkit.getDefaultToolkit.createImagegetClass.getResourcequotimagebw.jpgquot//定义图标文件 setIconImageframe_icon//设置窗口图标 setVisibletrue mainPanel new JPanel getContentPane.addmainPanel //起始设置窗口 //该窗口包含2个Label显示,其中一个为作者信息显示 //另一个为输入提示,文本框用来输入棋盘大小 //开始游戏按钮用来检查输入内容是否合法,若合法则进入游戏 class Start extends JFrame Object options quot确定quot String msg JPanel S_panel JLabel Msg JLabel PMax JTextField TMax JButton GameStart GameEvent bEGame BorderLayout SLayout public Start superquot黑白棋(二人对战)选择难度!quot setDefaultCloseOperationJFrame.EXIT_ON_CLOSE setLocation200 200 //设置组件位置XY坐标 setSize400 100 S_panelnew JPanel Msg new JLabelquot程序制作:杜世忠 Email:dushizhonggmail.comquot PMax new JLabelquot选择方格。
建议选择16以内,即16×16以下。
必须为偶数。
quot TMax new JTextFieldquot10quot GameStartnew JButtonquot开始游戏quot bEGame new GameEvent GameStart.addActionListenerbEGame SLayoutnew BorderLayout22 getContentPane.addS_panel S_panel.setLayoutSLayout S_panel.addquotNorthquotMsg S_panel.addquotWestquotPMax S_panel.addquotCenterquot TMax S_panel.addquotSouthquot GameStart setVisibletrue setResizablefalse System.out.printlnquot面板加载完成quot //事件监听类,处理按钮事件 class GameEvent implements ActionListener public void actionPerformedActionEvent Ge System.out.printlnquot进入监听quot Object Gobj Ge.getSource if Gobj GameStart System.out.printlnquot开始处理quot try //将输入的文本转换为int型,若输入不是数字则抛出异常 int num Integer.parseIntTMax.getText if num 2 0//判断是否是偶数 if num lt 8//判断输入是否大于等于8 PMax.setTextquot你输入了非法字符!必须输入大于等于8的偶数!quot //警告窗口 msg new Stringquot你输入了非法字符!必须输入大于等于8的偶数!quot JOptionPane.showOptionDialognull msg quot输入错误!quot JOptionPane.DEFAULT_OPTION JOptionPane.WARNING_MESSAGE null options options0 else //输入符合要求进入游戏 System.out.printlnquot正在进入游戏!quot GameStart.setTextquot 正 在 游 戏 中 quot GameStart.setEnabledfalse new Chess_panelnum else //如果输入了奇数,提示并返回要求重新输入 System.out.printlnquot你输入了非法字符!必须输入偶数!quot PMax.setTextquot你输入了非法字符!必须输入偶数!quot msg new Stringquot你输入了非法字符!必须输入偶数!quot JOptionPane.showOptionDialognull msg quot输入错误!quot JOptionPane.DEFAULT_OPTION JOptionPane.WARNING_MESSAGE null options options0 catchException e //如果输入的不是数字,抛出异常,并提示 System.out.printlnquot你输入了非法字符!必须为数字quote PMax.setTextquot你输入了非法字符!必须为数字!quot msg new Stringquot你输入了非法字符!必须输入数字!quot JOptionPane.showOptionDialognull msg quot输入错误!quot JOptionPane.DEFAULT_OPTION JOptionPane.WARNING_MESSAGE null options options0 //棋盘面板 //棋盘面板包括黑白双方的下棋提示,双方棋子数量即时显示,功能按钮,总比分显示 //功能按钮包括(结束,重新开始,让步,裁判) //功能描述: // 双方开始游戏之后,黑棋先下,当下棋过程中一方无子可下的时候点击让步按钮,让另一方继续下 // 直到最后格子下完,或者双方都无子可下的时候,点击裁判按钮,算出胜负,记下比分,并重新开始游戏 // 在下棋过程中,点击新一局按钮将重新开始新一局游戏,但是并不会保存当前游戏的比分。
// 任意时刻按结束按钮,程序退出 class Chess_panel extends Game int Mark1 0 Mark2 0//定义双方的分数变量 UserEvent bEObj JButton Chess public Chess_panelint input Max input2//获取输入的棋盘大小参数 setResizabletrue setSize600 600 GridLayout grid new GridLayoutMax Max mainPanel.setLayoutgrid Chess new JButtonMaxMax bEObj new UserEvent //整个面板由按钮组成,按行列绘制 for int i 0 i lt Max i for int j 0 j lt Max j Chessij new JButton Chessij.setFontnew
java.awt.Fontquot宋体quot Font.PLAIN 15 if i 0 ampamp i Max - 1 ampamp j 0 ampamp j Max - 1 Chessij.setBackgroundColor.green else Chessij.setEnabledfalse mainPanel.addChessij Chessij.addActionListenerbEObj this.setVisibletrue //设置外围棋盘按钮 Chess00.setTextquot黑quot Chess01.setTextquot0quot //Chess01.setEnabledtrue Chess0Max - 2.setTextquot白quot Chess0Max - 1.setTextquot0quot //Chess0Max - 1.setEnabledtrue Chess0Max / 2 - 2.setTextquot黑quot Chess0Max / 2 - 1.setTextquot方quot Chess0Max / 2.setTextquot先quot Chess0Max / 2 1.setTextquot下quot //设置棋盘默认初始状态 ChessMax - 2 / 2Max - 2 / 2.setTextquot●quot ChessMax - 2 / 2 1Max - 2 / 2 1.setTextquot●quot ChessMax - 2 / 2Max - 2 / 2 1.setTextquot○quot ChessMax - 2 / 2 1Max - 2 / 2.setTextquot○quot //外围颜色,去掉按钮边框,设置按钮字体颜色 for int m 0 m lt Max m Chess0m.setBordernull ChessMax - 1m.setBordernull ChessMax - 1m.setForegroundColor.blue Chess0m.setBackgroundColor.yellow ChessMax - 1m.setBackgroundColor.yellow for int m 1 m lt Max - 1 m Chessm0.setBordernull ChessmMax - 1.setBordernull/ Chessm0.setTextquot◎quot ChessmMax - 1.setTextquot◎quot /Chessm0.setBackgroundColor.yellow ChessmMax - 1.setBackgroundColor.yellow //功能按钮 ChessMax - 11.setEnabledtrue ChessMax - 12.setEnabledtrue ChessMax - 13.setEnabledtrue ChessMax - 14.setEnabledtrue ChessMax - 11.setTextquot结束quot ChessMax - 12.setTextquot新一局quot ChessMax - 13.setTextquot让步quot ChessMax - 14.setTextquot裁判quot //比分显示 ChessMax - 16.setTextquot总比分quot ChessMax - 17.setTextquot黑quotMark1 ChessMax - 18.setTextquot白quotMark2 Chess01.setBackgroundColor.white Chess0Max - 1.setBackgroundColor.white //设置功能按钮的事件侦听事件 class UserEvent implements ActionListener public void actionPerformedActionEvent e Object obj e.getSource //System.out.printlnquotyou have pressed a areaquot if obj ChessMax - 11 System.exit1 //退出按钮 if obj ChessMax - 12 new Reset //退出按钮 if obj ChessMax - 13//让步按钮 if IamFirst false//如果白棋让步 IamFirst true Chess00.setBackgroundColor.red Chess0Max-2.setBackgroundColor.white else //如果黑棋让步 IamFirst false Chess00.setBackgroundColor.white Chess0Max-2.setBackgroundColor.red ifobj ChessMax - 14//如果按裁判按钮,计算成绩 int M1 0 M2 0 M1 Integer.parseIntChess01.getText M2 Integer.parseIntChess0Max - 1.getText if M1gtM2 System.out.printlnquot黑棋胜quot msg new Stringquot裁判郑重宣布:n胜利方为黑棋!n恭喜恭喜!黑棋取得胜利!继续努力吧!n白棋不要气馁,再接再厉!quot JOptionPane.showOptionDialognull msg quot成绩统计结果!quot JOptionPane.DEFAULT_OPTION JOptionPane.WARNING_MESSAGE null options options0 Mark1 Mark1 1//修改比分 ChessMax - 17.setTextquot黑quot Mark1 if M1ltM2 System.out.printlnquot白棋胜quot msg new Stringquot裁判郑重宣布:n胜利方为白棋!n恭喜恭喜!白棋取得胜利!继续努力吧!n黑棋不要气馁,再接再厉!quot JOptionPane.showOptionDialognull msg quot成绩统计结果!quot JOptionPane.DEFAULT_OPTION JOptionPane.WARNING_MESSAGE null options options0 Mark2 Mark2 1 ChessMax - 18.setTextquot白quot Mark1 if M1 M2 System.out.printlnquot双方战平quot msg new Stringquot裁判郑重宣布:n双方战平!n大家水平相当!努力吧,争取下次打倒对手!quot JOptionPane.showOptionDialognull msg quot成绩统计结果!quot JOptionPane.DEFAULT_OPTION JOptionPane.WARNING_MESSAGE null options options0 new Reset//调用重设棋盘方法 //循环侦听棋盘上按钮事件 for int i 1 i lt Max - 1 i //System.out.printlnquotline:quoti for int j 1 j lt Max - 1 j //System.out.printlnquotcoulue:quotj if obj Chessij //System.out.printlnquotyou have pressed Chessquot i j if Chessij.getText quotquot//只侦听棋盘上的空白区域 //System.out.printlnquotthis area has no Chessquot //检查当前点击的按钮周围8个按钮是否为空,如果为空,则不进行处理,程序返回 if Chessi 1j.getText quot●quot Chessi - 1j.getText quot●quot Chessi 1j 1.getText quot●quot Chessi - 1j - 1.getText quot●quot Chessi 1j - 1.getText quot●quot Chessi - 1j 1.getText quot●quot Chessij 1.getText quot●quot Chessij - 1.getText quot●quot Chessi 1j.getText quot○quot Chessi - 1j.getText quot○quot Chessi 1j 1.getText quot○quot Chessi - 1j - 1.getText quot○quot Chessi 1j - 1.getText quot○quot Chessi - 1j 1.getText quot○quot Chessij 1.getText quot○quot Chessij - 1.getText quot○quot //System.out.printlnquotthere is at least 1 Chess around itquot //判断是谁下的棋 if IamFirst true System.out.printlnquotI am blackquot rulesi j quot●quot if ChessCheck true Chessij.setTextquot●quot IamFirst false Chess00.setBackgroundColor.white Chess0Max-2.setBackgroundColor.red WhoWin else System.out.printlnquotI am whitequot rulesi j quot○quot if ChessCheck true Chessij.setTextquot○quot IamFirst true Chess00.setBackgroundColor.red Chess0Max-2.setBackgroundColor.white WhoWin /下棋规则,以当前下棋的位置为中心,向其周围的8个方向散射搜寻。
1.如果其搜寻方向上的第一个棋子不是对方棋子,则停止该方向上的搜寻, 设置该方向为不能 ChessCheckfalse 2.如果搜寻方向上的第一个棋子为对方棋子,则继续搜寻直到找到己方棋 子则将中间部分的对方棋子变成己方棋子并设置该方向能 ChessChecktrue, 否则,设置标志为该方向不能ChessCheckfalse 3.如果所有方向返回的都是不能ChessCheckfalse / public void rulesint i int j String chess boolean ChessCheck1 false boolean ChessCheck2 false boolean ChessCheck3 false boolean ChessCheck4 false boolean ChessCheck5 false boolean ChessCheck6 false boolean ChessCheck7 false boolean ChessCheck8 false //判断是谁下的棋 String mychess String yourchess if chess quot○quot mychess quot○quot yourchess quot●quot else mychess quot●quot yourchess quot○quot System.out.printlnquot你是quot mychess quot棋,对方是quot yourchess quot棋quot //检查右边 System.out.printlnquot检查右边quot if Chessij 1.getText yourchess //System.out.printlnquot右边第一个是我的棋子quot ChessCheck1 false else //向右移动找出是否有自己的棋子,如果有则吃掉对方的子,否则返回false //System.out.printlnquot向右移动找出是否有自己的棋子,如果有则吃掉对方的子,否则返回falsequot for int m 1 m lt Max - j - 1 m if Chessij 1 m.getText mychess//如果是己方棋子 //System.out.printlnquot如果是己方棋子quot int n 1 while n lt m //System.out.printlnquot吃掉己方2颗棋子中间的对方棋子quot Chessij n.setTextmychess//吃掉己方2颗棋子中间的对方棋子 n ChessCheck1 true break else ChessCheck1 false //如果不是己方棋子,继续寻找 //检查左边 System.out.printlnquot检查左边quot if Chessij - 1.getText yourchess ChessCheck2 false else //向左移动找出是否有自己的棋子,如果有则吃掉对方的子,否则返回false //System.out.printlnquot向左移动找出是否有自己的棋子,如果有则吃掉对方的子,否则返回falsequot //System.out.printlnquot左移的时候列数从大到小quot for int m j - 2 m gt 0 m--//左移的时候列数从大到小 //System.out.printlnquot如果是己方棋子quot if Chessim.getText mychess//如果是己方棋子 int n 0 while n lt j - m - 1 //System.out.printlnquot吃掉己方2颗棋子中间的对方棋子quot Chessij - n - 1.setTextmychess//吃掉己方2颗棋子中间的对方棋子 n ChessCheck2 true break else ChessCheck2 false //如果不是己方棋子,继续寻找 //检查上方 System.out.printlnquot检查上方quot if Chessi - 1j.getText yourchess ChessCheck3 false else //向上移动找出是否有自己的棋子,如果有则吃掉对方的子,否则返回false //System.out.printlnquot向上移动找出是否有自己的棋子,如果有则吃掉对方的子,否则返回falsequot //System.out.printlnquot上移的时候行数从大到小quot for int m i - 2 m gt 0 m--//上移的时候行数从大到小 //System.out.printlnquot如果是己方棋子quot m Chessm - 1j.getText if Chessmj.getText mychess//如果是己方棋子 int n 0 while n lt i - m - 1 //System.out.printlnquot吃掉己方2颗棋子中间的对方棋子quot Chessi - n - 1j.setTextmychess//吃掉己方2颗棋子中间的对方棋子 n ChessCheck3 true break else ChessCheck3 false //如果不是己方棋子,继续寻找 //检查下方 System.out.printlnquot检查下方quot if Chessi 1j.getText yourchess //System.out.printlnquot下方边第一个是我的棋子quot ChessCheck4 false else //向右移动找出是否有自己的棋子,如果有则吃掉对方的子,否则返回false //System.out.printlnquot向下移动找出是否有自己的棋子,如果有则吃掉对方的子,否则返回falsequot for int m 1 m lt Max - i - 1 m if Chessi 1 mj.getText mychess//如果是己方棋子 //System.out.printlnquot如果是己方棋子quot int n 1 while n lt m //System.out.printlnquot吃掉己方2颗棋子中间的对方棋子quot Chessi nj.setTextmychess//吃掉己方2颗棋子中间的对方棋子 n ChessCheck4 true break else ChessCheck4 false //如果不是己方棋子,继续寻找 //上面已经完成对水平和垂直方向的检查过程,下面开始是些对角方向的检查 //检查右上角 System.out.printlnquot检查右上角quot if Chessi - 1j 1.getText yourchess //System.out.printlnquot右上角第一个是我的棋子quot ChessCheck5 false else //向右上移动找出是否有自己的棋子,如果有则吃掉对方的子,否则返回false //System.out.printlnquot向右上移动找出是否有自己的棋子,如果有则吃掉对方的子,否则返回falsequot int x Max - j int y i int m 1 int tj if x gt y tj y - 2 else tj x - 2 while m lt tj //System.out.printlnquot寻找棋子quot m tj if Chessi - m - 1j 1 m.getText mychess//如果是己方棋子 //System.out.printlnquot如果是己方棋子quot Chessi - m - 1j 1 m.getText int n 1 while n lt m //System.out.printlnquot吃掉己方2颗棋子中间的对方棋子quot Chessi - nj n.setTextmychess//吃掉己方2颗棋子中间的对方棋子 n ChessCheck5 true break else ChessCheck5 false //如果不是己方棋子,继续寻找 m //右下角 System.out.printlnquot检查右.