版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、班级:班级: 姓名:姓名: 学号:学号: 指导教师:指导教师: 实训时间:实训时间:2011.9.16-2011.9.232011.9.16-2011.9.23 目录 目录目录-1 一、实习目的:一、实习目的:-2 二、实习内容:二、实习内容:-2 三、设计要求:三、设计要求:-2 1. 设计要求:-2 2. 游戏所涉及的知识点:-3 3.设计内容: -3 4. 流程图-3 (1)整体流程图-3 (2)循环流程图-4 (3)具体代码-5 四、相关四、相关截截图图:-19 五、设计日志:五、设计日志:-20 六、总结:六、总结:-21 七、教师评语七、教师评语-21 一、实习目的一、实习目的:
2、通过本次实习全面考查前面所学的知识,并运用这些知识设计 java 程序。培养同学 的动手能力,独立分析解决问题的能力和创新精神。熟悉掌握 Java 语言的程序设计方法。 二、实习内容二、实习内容: 关于设计 java 五子棋小游戏的制作。 三、设计要求三、设计要求: 1. 设计要求设计要求: (1)设计面要简洁。 (2)设计功能明确。 (3)熟悉 JAVA 开发环境,掌握 JAVA 程序的编译和运行; (4)掌握 JAVA 基本编程技术和方法; (5)了解异常处理机制。 (6)说明书、流程图要清楚; (7)要求采用模块化程序设计方法,及锯齿型书写格式,要求上机调试通过和按设计 报告格式; (8
3、)设计上交内容:设计报告一份(按格式书写) ;源程序文件。 2. 游戏所涉及的知识点游戏所涉及的知识点: 所涉及的知识点主要有: (1) 、main 在程序的创建 (2) 、Frame 类的一个实例对象的应用 3.设计设计内容:内容: (1) 、题目:五指棋游戏 (2)、具体实现: 、绘制棋盘,(2015)(3020)(4030)条横竖线,在直线交 点处下棋子(实心圆形)。 、自动默认白子先行,但是可以选择先行方然后黑白交替下子,在棋盘 上设定一个与棋盘格大小边长相等的正方形,初始状态,正方形的中 心位于期盼的中心点,不能移出边界。 、当任何一方有五个棋子横,竖,斜连在一起时,系统自动判断赢棋
4、, 并显示黑方或白方胜。结束后,可以重新开始。 4. 流程图流程图 (1)整体流程图)整体流程图 结束游戏 开始 玩家下子 鼠标动作 绘制棋子 显示界面,绘制棋盘 判断输赢 是 否 重置 、 (2)循环流程图)循环流程图 开始 黑棋落子(默认) 判断是否有 棋 白棋落子 判断是否有 棋 黑棋落子 无 没有效果 判断是否获 胜 无 开局退出棋盘模式 人人模式 人机模式 多种棋盘 判断白方(黑方)胜 . ., | | / / | | / / | | / / | |/ ;-._ _/ / ; | / ) / / | / /_/_/ |/ / | ( - | . / | | | | 有 无 结束 (3
5、)具体代码)具体代码 package javaapplication20; import java.awt.*; import java.awt.event.*; import java.applet.*; import javax.swing.*; public class VVVVV public static void main(String args) / TODO code application logic here ChessFrame cf = new ChessFrame(); cf.show(); class ChessFrame extends JFrame implem
6、ents ActionListener private String strsize=20 x15,30 x20,40 x30; private String strmode=人机对弈人机对弈,人人对弈人人对弈; public static boolean iscomputer=true,checkcomputer=true; private int width,height; private ChessModel cm; private MainPanel mp; public ChessFrame() this.setTitle(五子棋游戏五子棋游戏);/构造五子棋游戏的主窗体构造五子棋游
7、戏的主窗体 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(makeM
8、enu(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 ite
9、ms, 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 JM
10、enu(棋盘棋盘); 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 gr
11、oup=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 ite
12、m, 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 JRadioB
13、uttonMenuItem 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
14、(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 th
15、is.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(Win
16、dows) 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(thi
17、s); catch(Exception ee) if(arg.equals(20 x15) this.width=20; this.height=15; cm=new ChessModel(1); MapSize(this.width,this.height); SwingUtilities.updateComponentTreeUI(this); if(arg.equals(30 x20) this.width=30; this.height=20; cm=new ChessModel(2); MapSize(this.width,this.height); SwingUtilities.u
18、pdateComponentTreeUI(this); if(arg.equals(40 x30) 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.g
19、etWidth(),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.
20、equals(关于关于) JOptionPane.showMessageDialog(this, 五子棋游戏五子棋游戏, 关于关于, 0); if(arg.equals(退出退出) System.exit(0); class ChessModel private int width,height,modeChess; private int x=0,y=0; private int arrMapShow; private boolean isOdd,isExist; public ChessModel() public ChessModel(int modeChess) this.isOdd=
21、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 = mode
22、Chess; 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) /判断该坐标位置是否可下棋子判断该坐标位置是否可下棋子
23、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.arrMapSh
24、owxy=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;
25、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_n
26、um,max_temp=0; int x_temp=x,y_temp=y; int x_temp1=x_temp,y_temp1=y_temp; / right for(int i=1;ithis.width) break; if(this.arrMapShowx_temp1y_temp1=black_or_white) num+; else break; / 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_whit
27、e) num+; else break; if(num5) max_temp=num; / up x_temp1=x_temp; y_temp1=y_temp; num=0; for(int i=1;i5;i+) y_temp1-=1; if(y_temp1max_temp / 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_temp1this.width) break; if(this.arrMapShowx_temp1y_tem
28、p1=black_or_white) num+; else break; if(nummax_temp / 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; / left_down x_temp1=x_temp; y_temp1=y_temp; for(int i=1;ith
29、is.height | x_temp1max_temp 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.a
30、rrMapShowx_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) br
31、eak; 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_te
32、mp10 | x_temp1this.width) 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_temp1this.width) break; if(this.arrMapShowx_temp1y_temp1=arrvalue) num+; else break; /左下左下
33、 x_temp1=x_temp; y_temp1=y_temp; for(int i=1;ithis.height | x_temp10) break; if(this.arrMapShowx_temp1y_temp1=arrvalue) num+; else break; if(num=5) return true; return false; public void showSuccess(JPanel jp) JOptionPane.showMessageDialog(jp,你赢了你赢了!,win,/赢棋后的提示赢棋后的提示 JOptionPane.INFORMATION_MESSAGE
34、); public void showDefeat(JPanel jp) JOptionPane.showMessageDialog(jp,你输了你输了!,lost,/输棋后的提示输棋后的提示 JOptionPane.INFORMATION_MESSAGE); class MainPanel extends JPanel implements MouseListener,MouseMotionListener private int width,height;/棋盘的宽度和高度棋盘的宽度和高度 private ChessModel cm; MainPanel(ChessModel mm) /根
35、据棋盘模式设定面板的大小根据棋盘模式设定面板的大小 cm=mm; width=cm.getWidth(); height=cm.getHeight(); addMouseListener(this); public void setModel(ChessModel mm) /根据棋盘模式设定棋盘的宽度和高度根据棋盘模式设定棋盘的宽度和高度 cm = mm; width = cm.getWidth(); height = cm.getHeight(); public void paintComponent(Graphics g) super.paintComponent(g); for(int
36、j = 0; j = height; j+) for(int i = 0; i = width; i+) int v = cm.getarrMapShow()ij; draw(g, i, j, v); public void draw(Graphics g, int i, int j, int v) /根据提供的棋子信息(颜色、坐标)画棋根据提供的棋子信息(颜色、坐标)画棋 子子 int x = 20 * i+20; int y = 20 * j+20; if(i!=width g.drawRect(x,y,20,20); if(v = 1 ) /画黑色棋子画黑色棋子 g.setColor(Color.gray); g.drawOval(x-8,y-8,16,16); g.setColor(Color.black); g.fillOval(x-8,y-8,16,16); if(v = 2 ) /画白色棋子画白色棋子 g.setCol
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2026年国庆节知识普及知识
- 2026年中国心理咨询师仿真题解析
- 2026年农业投放营销推广合同
- 消火栓栓口方向验收记录
- 裱糊工程施工进度控制保证措施
- 冷作钣金工工艺应用试题及答案解析
- 2026年融媒体技术岗位笔考前冲刺测试卷附答案详解【巩固】
- 粮食立筒库施工专项方案
- 护士VTE知信行调查问卷
- 餐饮冷菜制作安全操作手册 (标准版)
- 核心考点03 断句-2026年高考《语文》一轮复习高效培优系列讲义
- 高级微观经济学
- 2025年助产证考试试题及答案
- 智慧树知到《大数据与人工智能(哈尔滨商业大学)》章节测试含答案
- 针灸学试题库(含参考答案)
- 弱电安防知识培训课件
- 肺功能进修生汇报课件
- GJB827B--2020军事设施建设费用定额
- -2025年浙江省衢州市开化县重点高中自主招生 数学 试卷 (学生版+解析版)
- 导演思维基础知识培训课件
- 走出奥米勒斯城的人
评论
0/150
提交评论