




免费预览已结束,剩余26页可下载查看
下载本文档
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
课程设计说明书 NO.31Java五子棋小游戏1.课程设计的目的 通过此次课程设计,巩固所学Java语言基本知识,增进Java语言编辑基本功,掌握JDK、Editplus、Eclipse、JCreator等开发工具的运用,拓宽常用类库的应用。使我们通过该教学环节与手段,把所学课程及相关知识加以融会贯通,全面掌握Java语言的编程思想及面向对象程序设计的方法,为今后从事实际工作打下坚实的基础。2.设计方案论证2.1设计思路(1)界面棋盘设计在对棋盘界面设计方面要考虑简洁友好,符合游戏者需求。棋子的设计方面系统中设置了两种棋子颜色,white或者black, 游戏者可自行选择。棋子怎样画出来,怎样使棋子按我们所想的方式来绘制出来是设计的主要难题。运行时要求当每次点击鼠标的时候就在点击鼠标的地方画一个棋子,所以得定义一个棋子的类使点击鼠标时系统把棋子绘制出来。这样主界面里的棋子就确定了,而且也确定了几个所需要的类。可以先定义好这些类了。有了以上的准备就可以把棋盘绘制出来了,下棋时每次在适当位置点击鼠标的时候就在所点击的位置上画出你这个角色的颜色,然后计算机会自动的下棋,计算机自动下棋要有一个控制变量来控制能不能下棋。人可以随意的下子,可是计算机就要计算在哪个位置下子了。这里由计算机下子的算法来确定,人人对战的话就不象单机游戏一样,要计算机来判断人下子之后计算机下那一个地方了,人人对战只要在双方下子的时候判断赢棋情况,当然还要同步的控制两个人下棋的顺序,这样的话就要用到线程了,各自都继承或实现线程类或接口,以便各自能随时单独控制接发消息。(2)算法设计对于五子棋游戏,无论人机对战,还是双人对战,都需要判断棋盘上是否存在五子连珠情况,这既是游戏名字的由来,也是游戏结束的标志。判断五子连珠的原理是从横,竖,左斜线,右斜线4条线上判断是否存在5个相连的同类棋子。 对战一方落子后,在该处向8个方向检测连续的同类棋子,如果检测到直线方向上存在5个连续的同类棋子(包含本位置棋子),则判断为“连五”并结束检测循环。基于检测结果,可以判断游戏是否结束,并根据获胜方的落子代码判断获胜方是谁。 由于不考虑禁手问题,在实现“连五”过程中,我们可以考虑完成“双四”,“四三”,“双三”,“冲四”,“活三”,“活四”来实现目的。 活四:有两个点能形成“连五”的四就是活四; 冲四:只有一个点能形成“连五”的四叫做“冲四”,或者叫做“单四”; 活三:己方在落一子就能形成“活四”的三叫做“活三”。“活三”分为“连活三”和“跳活三”。连活三:在棋盘某一条阳线或阴线上有同色三子相连,且在此三子两端延长线上有一端至少有一个,另一端至少有两个无子的交叉点与此三子紧密相连。跳活三:中间仅间隔一个无子交叉点的连三,但两端延长线均至少有一个无子的交叉点与此三子相连。 双三:由于黑方走一着在无子交叉点上同时形成二个或二个以上黑方 活 的局面。 双四:由于黑方走一着在无子交叉点上同时形成二个或二个以上黑方 四 的局面。 四三:指某一方同时具备两个先手,其中一个是 四 ,一个是 活三 。 在实现上面这些高级算法的时候,我们需要一个基础,就是对制定位置周围情况进行检测。在棋盘上面,棋子存在8个移动方向,检测时需要对每个方向进行检测。为了简单直观,这里将每步棋子的移动生成一个新的棋子。2.2需求分析 一款小游戏的确立是建立在各种各样的需求上面的,这种需求往往来自于玩家的实际需求,其中玩家的实际需求最为重要.面对游戏拥有不同知识和理解层面的玩家,游戏制作人对玩家需求的理解程度,在很大程度上决定了此类游戏开发的成败.因此如何更好地的了解,分析,明确玩家需求,并且能够准确,清晰以文档的形式表达给游戏制作人,保证开发过程按照满足玩家需求为目的正确开发方向进行,是每游戏游戏制作人需要面对的问题。作为五子棋的设计需要考虑到的最基本的需求莫过于人机对战与人人对战功能的实现,当然还有下棋过程中的下棋悔棋功能以及判断游戏的胜负等方面的要求。当然最好是要考虑到界面的友好性,作为一个娱乐软件,还应该考虑到玩家在游戏时的舒适性。2.3功能模块图开始游戏执棋子颜色五子棋游戏黑子先行判断胜负游戏结束图1.功能模块图2.4设计方法2.4.1 ChessFrame主要功能类ChessFrame主要功能是创建五子棋游戏主窗体和菜单,主要代码如下:class ChessFrame extends JFrame implements ActionListener private String strsize=20x15,30x20,40x30;private String strmode=人机对弈,人人对弈;public static boolean iscomputer=true,checkcomputer=true;private int width,height;private ChessModel cm;private MainPanel mp;2.4.2 游戏主窗体构造五子棋游戏的主窗体,主要代码如下:public ChessFrame() this.setTitle(五子棋游戏); cm=new ChessModel(1); mp=new MainPanel(cm); Container con=this.getContentPane(); con.add(mp,Center); this.setResizable(false); this.addWindowListener(new ChessWindowEvent(); MapSize(20,15); JMenuBar mbar = new JMenuBar(); this.setJMenuBar(mbar); JMenu gameMenu = new JMenu(游戏); mbar.add(makeMenu(gameMenu, new Object 开局, 棋盘,模式, null, 退出 , this); JMenu lookMenu =new JMenu(视图); mbar.add(makeMenu(lookMenu,new Object Metal,Motif,Windows ,this); JMenu helpMenu = new JMenu(帮助); mbar.add(makeMenu(helpMenu, new Object 关于 , this);2.4.3 主菜单构造五子棋游戏的主菜单,代码如下:public JMenu makeMenu(Object parent, Object items, Object target) JMenu m = null; if(parent instanceof JMenu) m = (JMenu)parent; else if(parent instanceof String) m = new JMenu(String)parent); else return null; for(int i = 0; i items.length; i+) if(itemsi = null) m.addSeparator(); else if(itemsi = 棋盘) JMenu jm = new JMenu(棋盘); ButtonGroup group=new ButtonGroup(); JRadioButtonMenuItem rmenu; for (int j=0;jstrsize.length;j+) rmenu=makeRadioButtonMenuItem(strsizej,target); if (j=0) rmenu.setSelected(true); jm.add(rmenu); group.add(rmenu); m.add(jm); else if(itemsi = 模式) JMenu jm = new JMenu(模式); ButtonGroup group=new ButtonGroup(); JRadioButtonMenuItem rmenu; for (int h=0;hstrmode.length;h+) rmenu=makeRadioButtonMenuItem(strmodeh,target); if(h=0) rmenu.setSelected(true); jm.add(rmenu); group.add(rmenu); m.add(jm); else m.add(makeMenuItem(itemsi, target); return m;3.设计结果与分析(1)五子棋游戏的主界面,如图2所示。图2 程序主界面(2)五子棋游戏的结束界面,如图3所示。图3 游戏结束界面(3)游戏游戏栏中的各个选项,如图4所示。图4 Game栏中的选项(4)视图设置栏中的各个选项,如图5所示。图5 Configure栏中的各个选项(5)Help帮助栏中的选项,如图6所示。图6 Help栏中的选项(6)点击Help栏中的About选项弹出的界面,如图7所示。图7 About选项弹出时的界面4.设计体会通过这次课程设计,我进一步加深对基础理论的理解,扩大专业知识面,对收集资料、查阅文献、方案制定等实践方面得到了很好的锻练,促进对所学知识应用能力的提高。同时我渐渐的复习了Java使用方法和编程语法,之后的编程过程也相对得心应手,基本完成了预期计划的要求。在此感谢我的指导老师冯云老师,她在课题的研究上不遗余力的给予指导。她严谨的治学态度深深的影响了我,使我受益匪浅!5.参考文献1耿祥义.JAVA大学实用教程北京:电子工业出版社.2005.3:85-1132朱战立,沈伟Java程序设计实用指南北京:电子工业出版社,2005.1:48-1353 唐大仕.Java程序设计M. 北京:北方交通大学出版社:2007.05:56-92 4叶核亚. JAVA2程序设计实用教程M北京:电子工业出版社;2008.4:64-985邢素萍. JAVA办公自动化项目方案精解M.北京:航空工业出版社, 2006.9:35-120附录/Java编程:五子棋游戏源代码import java.awt.*;import java.awt.event.*;import java.applet.*;import javax.swing.*;import java.io.PrintStream;import javax.swing.JComponent;import javax.swing.JPanel;/*main方法创建了ChessFrame类的一个实例对象(cf),*并启动屏幕显示显示该实例对象。*/public class FiveChessAppletDemo public static void main(String args) ChessFrame cf = new ChessFrame(); cf.show();/*类ChessFrame主要功能是创建五子棋游戏主窗体和菜单*/class ChessFrame extends JFrame implements ActionListener private String strsize=20x15,30x20,40x30;private String strmode=人机对弈,人人对弈;public static boolean iscomputer=true,checkcomputer=true;private int width,height;private ChessModel cm;private MainPanel mp;/构造五子棋游戏的主窗体public ChessFrame() this.setTitle(五子棋游戏); cm=new ChessModel(1); mp=new MainPanel(cm); Container con=this.getContentPane(); con.add(mp,Center); this.setResizable(false); this.addWindowListener(new ChessWindowEvent(); MapSize(20,15); JMenuBar mbar = new JMenuBar(); this.setJMenuBar(mbar); JMenu gameMenu = new JMenu(游戏); mbar.add(makeMenu(gameMenu, new Object 开局, 棋盘,模式, null, 退出 , this); JMenu lookMenu =new JMenu(视图); mbar.add(makeMenu(lookMenu,new Object Metal,Motif,Windows ,this); JMenu helpMenu = new JMenu(帮助); mbar.add(makeMenu(helpMenu, new Object 关于 , this);/构造五子棋游戏的主菜单public JMenu makeMenu(Object parent, Object items, Object target) JMenu m = null; if(parent instanceof JMenu) m = (JMenu)parent; else if(parent instanceof String) m = new JMenu(String)parent); else return null; for(int i = 0; i items.length; i+) if(itemsi = null) m.addSeparator(); else if(itemsi = 棋盘) JMenu jm = new JMenu(棋盘); ButtonGroup group=new ButtonGroup(); JRadioButtonMenuItem rmenu; for (int j=0;jstrsize.length;j+) rmenu=makeRadioButtonMenuItem(strsizej,target); if (j=0) rmenu.setSelected(true); jm.add(rmenu); group.add(rmenu); m.add(jm); else if(itemsi = 模式) JMenu jm = new JMenu(模式); ButtonGroup group=new ButtonGroup(); JRadioButtonMenuItem rmenu; for (int h=0;hstrmode.length;h+) rmenu=makeRadioButtonMenuItem(strmodeh,target); if(h=0) rmenu.setSelected(true); jm.add(rmenu); group.add(rmenu); m.add(jm); else m.add(makeMenuItem(itemsi, target); return m;/构造五子棋游戏的菜单项public JMenuItem makeMenuItem(Object item, Object target) JMenuItem r = null; if(item instanceof String) r = new JMenuItem(String)item); else if(item instanceof JMenuItem) r = (JMenuItem)item; else return null; if(target instanceof ActionListener) r.addActionListener(ActionListener)target); return r;/构造五子棋游戏的单选按钮式菜单项public JRadioButtonMenuItem makeRadioButtonMenuItem( Object item, Object target) JRadioButtonMenuItem r = null; if(item instanceof String) r = new JRadioButtonMenuItem(String)item); else if(item instanceof JRadioButtonMenuItem) r = (JRadioButtonMenuItem)item; else return null; if(target instanceof ActionListener) r.addActionListener(ActionListener)target); return r; public void MapSize(int w,int h) setSize(w * 20+50 , h * 20+100 ); if(this.checkcomputer) this.iscomputer=true; else this.iscomputer=false; mp.setModel(cm); mp.repaint(); public boolean getiscomputer() return this.iscomputer; public void restart() int modeChess = cm.getModeChess(); if(modeChess = 1) cm = new ChessModel(modeChess); MapSize(cm.getWidth(),cm.getHeight(); else System.out.println(u81EAu5B9Au4E49); public void actionPerformed(ActionEvent e) String arg=e.getActionCommand(); try if (arg.equals(Windows) UIManager.setLookAndFeel( com.sun.java.swing.plaf.windows.WindowsLookAndFeel); else if(arg.equals(Motif) UIManager.setLookAndFeel( com.sun.java.swing.plaf.motif.MotifLookAndFeel); else UIManager.setLookAndFeel( javax.swing.plaf.metal.MetalLookAndFeel ); SwingUtilities.updateComponentTreeUI(this); catch(Exception ee) if(arg.equals(20x15) this.width=20; this.height=15; cm=new ChessModel(1); MapSize(this.width,this.height); SwingUtilities.updateComponentTreeUI(this); if(arg.equals(30x20) this.width=30; this.height=20; cm=new ChessModel(2); MapSize(this.width,this.height); SwingUtilities.updateComponentTreeUI(this); if(arg.equals(40x30) this.width=40; this.height=30; cm=new ChessModel(3); MapSize(this.width,this.height); SwingUtilities.updateComponentTreeUI(this); if(arg.equals(人机对弈) this.checkcomputer=true; this.iscomputer=true; cm=new ChessModel(cm.getModeChess(); MapSize(cm.getWidth(),cm.getHeight(); SwingUtilities.updateComponentTreeUI(this); if(arg.equals(人人对弈) this.checkcomputer=false; this.iscomputer=false; cm=new ChessModel(cm.getModeChess(); MapSize(cm.getWidth(),cm.getHeight(); SwingUtilities.updateComponentTreeUI(this); if(arg.equals(开局) restart(); if(arg.equals(关于) JOptionPane.showMessageDialog(this, 五子棋游戏测试版本, 关于, 0); if(arg.equals(退出) System.exit(0);/*类ChessModel实现了整个五子棋程序算法的核心*/class ChessModel /棋盘的宽度、高度、棋盘的模式(如2015)private int width,height,modeChess;/棋盘方格的横向、纵向坐标private int x=0,y=0;/棋盘方格的横向、纵向坐标所对应的棋子颜色,/数组arrMapShow只有3个值:1,2,3,-5,/其中1代表该棋盘方格上下的棋子为黑子,/2代表该棋盘方格上下的棋子为白子,/3代表为该棋盘方格上没有棋子,/-5代表该棋盘方格不能够下棋子private int arrMapShow;/交换棋手的标识,棋盘方格上是否有棋子的标识符private boolean isOdd,isExist;public ChessModel() /该构造方法根据不同的棋盘模式(modeChess)来构建对应大小的棋盘public ChessModel(int modeChess) this.isOdd=true; if(modeChess = 1) PanelInit(20, 15, modeChess); if(modeChess = 2) PanelInit(30, 20, modeChess); if(modeChess = 3) PanelInit(40, 30, modeChess); /按照棋盘模式构建棋盘大小private void PanelInit(int width, int height, int modeChess) this.width = width; this.height = height; this.modeChess = modeChess; arrMapShow = new intwidth+1height+1; for(int i = 0; i = width; i+) for(int j = 0; j = width+20 | x = height+20 | y 0;/计算棋盘上某一方格上八个方向棋子的最大值,/这八个方向分别是:左、右、上、下、左上、左下、右上、右下public boolean chessExist(int i,int j) if(this.arrMapShowij=1 | this.arrMapShowij=2) return true; return false;/判断该坐标位置是否可下棋子public void readyplay(int x,int y) if(badxy(x,y) return; if (chessExist(x,y) return; this.arrMapShowxy=3;/在该坐标位置下棋子public void play(int x,int y) if(badxy(x,y) return; if(chessExist(x,y) this.isExist=true; return; else this.isExist=false; if(getisOdd() setisOdd(false); this.arrMapShowxy=1; else setisOdd(true); this.arrMapShowxy=2; /计算机走棋/*说明:用穷举法判断每一个坐标点的四个方向的的最大棋子数,*最后得出棋子数最大值的坐标,下子*/public void computerDo(int width,int height) int max_black,max_white,max_temp,max=0; setisOdd(true); System.out.println(计算机走棋 .); for(int i = 0; i = width; i+) for(int j = 0; j max) max=max_temp; this.x=i; this.y=j; setX(this.x); setY(this.y); this.arrMapShowthis.xthis.y=2;/记录电脑下子后的横向坐标public void setX(int x) this.x=x;/记录电脑下子后的纵向坐标public void setY(int y) this.y=y;/获取电脑下子的横向坐标public int getX() return this.x;/获取电脑下子的纵向坐标public int getY() return this.y;/计算棋盘上某一方格上八个方向棋子的最大值,/这八个方向分别是:左、右、上、下、左上、左下、右上、右下public int checkMax(int x, int y,int black_or_white) int num=0,max_num,max_temp=0; int x_temp=x,y_temp=y; int x_temp1=x_temp,y_temp1=y_temp; /judge right for(int i=1;ithis.width) break; if(this.arrMapShowx_temp1y_temp1=black_or_white) num+; else break; /judge left x_temp1=x_temp; for(int i=1;i5;i+) x_temp1-=1; if(x_temp10) break; if(this.arrMapShowx_temp1y_temp1=black_or_white) num+; else break; if(num5) max_temp=num; /judge up x_temp1=x_temp; y_temp1=y_temp; num=0; for(int i=1;i5;i+) y_temp1-=1; if(y_temp10) break; if(this.arrMapShowx_temp1y_temp1=black_or_white) num+; else break; /judge down y_temp1=y_temp; for(int i=1;ithis.height) break; if(this.arrMapShowx_temp1y_temp1=black_or_white) num+; else break; if(nummax_temp&num5) max_temp=num; /judge left_up x_temp1=x_temp; y_temp1=y_temp; num=0; for(int i=1;i5;i+) x_temp1-=1; y_temp1-=1; if(y_temp10 | x_temp10) break; if(this.arrMapShowx_temp1y_temp1=black_or_white) num+; else break; /judge right_down x_temp1=x_temp; y_temp1=y_temp; for(int i=1;ithis.height | x_temp1this.width) break; if(this.arrMapShowx_temp1y_temp1=black_or_white) num+; else break; if(nummax_temp&num5) max_temp=num; /judge right_up x_temp1=x_temp; y_temp1=y_temp; num=0; for(int i=1;i5;i+) x_temp1+=1; y_temp1-=1; if(y_temp1this.width) break; if(this.arrMapShowx_temp1y_temp1=black_or_white) num+; else break; /judge left_down x_temp1=x_temp; y_temp1=y_temp; for(int i=1;ithis.height | x_temp1max_temp&num5) max_temp=num; max_num=max_temp; return max_num;/判断胜负public boolean judgeSuccess(int x,int y,boolean isodd) int num=1; int arrvalue; int x_temp=x,y_temp=y; if(isodd) arrvalue=2; else arrvalue=1; int x_temp1=x_temp,y_temp1=y_temp; /判断右边 for(int i=1;ithis.width) break; if(this.arrMapShowx_temp1y_temp1=arrvalue) num+; else break; /判断左边 x_temp1=x_temp; for(int i=1;i6;i+) x_temp1-=1; if(x_temp10) break; if(this.arrMapShowx_temp1y_temp1=arrvalue) num+; else break; if(num=5) return true; /判断上方 x_temp1=x_temp; y_temp1=y_temp; num=1; for(int i=1;i6;i+) y_temp1-=1; if(y_temp10) break; if(this.arrMapShowx_temp1y_temp1=arrvalue) num+; else break; /判断下方 y_temp1=y_temp; for(int i=1;ithis.height) break; if(this.arrMapShowx_temp1y_temp1=arrvalue) num+; else break; if(num=5) return true; /判断左上 x_temp1=x_temp; y_temp1=y_temp; num=1; for(int i=1;i6;i+) x_temp1-=1; y_temp1-=1; if(y_temp10 | x_temp10) break; if(this.arrMapShowx_temp1y_temp1=arrvalue) num+; else break; /判断右下 x_temp1=x_temp; y_temp1=y_temp; for(int i=1;ithis.height | x_temp1this.width) bre
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2025年陕西省公务员行测(A类)真题及答案
- 2025年奉新县城区学校选调教师考试笔试试题【答案】
- 电工类安全培训考试题及答案解析
- 初中英语中考试卷及答案
- 初三中考试卷及答案期末
- 儿科护理学第七版 题库及答案解析
- 医疗辐射安全考试题库及答案解析
- 液压部件装配知识培训课件
- 2025年国家开放大学(电大)《法国语言文学导论》期末考试备考试题及答案解析
- 2025年医共体医院职能科室设置及职责
- 公安矛盾纠纷化解课件
- 2025年恒丰银行笔试题库及答案
- 看板管理管理办法
- 2025至2030镍氢电池隔膜行业市场发展现状及竞争格局与投资价值报告
- 造林质量管理办法
- 冠心病人饮食健康管理
- 2025年陕西省中考数学试题卷(含答案详解)
- 六年级数学考试技能提升教学措施
- 2025至2030全球及中国手机软件行业产业运行态势及投资规划深度研究报告
- 服务器、存储维保项目投标文件
- 2025-2030中国宠物托运服务行业运营状况与盈利前景预测报告
评论
0/150
提交评论