




已阅读5页,还剩23页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
课程设计 论文 任务书 学学 院院 专专 业业 班班 一 课程设计一 课程设计 论文论文 题目 题目 人机对弈五子棋游戏 二 课程设计二 课程设计 论文论文 工作 工作 三 课程设计三 课程设计 论文论文 地点地点 四 课程设计四 课程设计 论文论文 内容要求 内容要求 1 1 本课程设计的目的 本课程设计的目的 1 使学生掌握软件开发的基本工作流程 2 巩固 JAVA 程序设计课程所学的内容 3 培养学生的计算机思维能力以及合作的精神 4 培养学生分析 解决问题的能力 5 提高学生的科技论文写作能力 2 2 课程设计的任务及要求 课程设计的任务及要求 1 1 基本要求 基本要求 1 研究课程设计任务 并进行系统需求分析 2 对系统进行总体设计 分解系统功能模块 进行任务分配 以实现分工合作 3 实现各功能模块代码 4 系统组装 测试 完善系统 2 2 创新要求 创新要求 在基本要求达到后 可进行创新设计 如改进界面 增加功能或进行代码优化 3 3 课程设计论文编写要求 课程设计论文编写要求 1 要按照书稿的规格打印誊写课程设计论文 2 论文包括封面 设计任务书 含评语 摘要 目录 设计内容 设计小结 3 论文装订按学校的统一要求完成 4 4 参考文献 参考文献 1 丁振凡 JAVA 语言程序设计 清华大学出版社 2 丁振凡 JAVA 语言程序设计实验指导与习题解答 清华大学出版社 3 5 5 课程设计进度安排 课程设计进度安排 内容内容 天数天数 地点地点 系统总体设计 1 实验室 软件设计及调试 1 实验室 答辩及撰写报告 1 实验室 图书馆 学生签名 学生签名 2015 年 6 月 25 日 课程设计课程设计 论文论文 评审意见评审意见 1 课程设计过程 20 分 优 良 中 一般 差 2 是否完成调试 系统运行效果 30 分 优 良 中 一般 差 3 回答问题 20 分 优 良 中 一般 差 4 课程设计报告 30 分 优 良 中 一般 差 5 格式规范性及考勤是否降等级 是 否 评阅人评阅人 职称职称 教授 2015 年 6 月 25 日 人机对弈五子棋游戏设计人机对弈五子棋游戏设计 摘要 摘要 近些年来五子棋游戏成为了人们的最爱 游戏的下载量很高可以说一 直排在前几位 也是人们日常生活中放松娱乐的最爱 特别是最近IT行业的 发展 人工智能的实现 移动客户端的发展和普及为我们五子棋游戏的发 展带来了新的生机 新的高潮 人工智能与五子棋的完美结合是五子棋的 又一项重要发展 而我们现在开发的人机对弈更是掀起了五子棋发展的新 篇章 为五子棋的发展与传播提供了新的机会 但是机会与挑战并存 我 们相信通过我们对五子棋程序的不断优化和完善 五子棋一定会越来越受 到大家的欢迎 本次课程设计要求利用 Java 语言设计出一个人机对弈五子棋游戏 能实现人机对弈五子棋 了解五子棋对弈规则后 利用本学期所学的 Java 高级图形界面编程方法的知识 并通过穷举法 考虑八个方向连 成五子的几率高低 让计算机具有人工智能 成功设计出人机对弈五子 棋的游戏 除了人机对弈的功能 我们还增加了人人对战的功能 这项 功能使得人与人之间能够互下五子棋 不仅仅是人与计算机对弈 除此 之外 还增加了再来一局 认输 悔棋 退出游戏的功能 为了让游戏 功能更加丰富 还增加了棋盘 模式 外观三个菜单栏供用户选择相应 功能的菜单项 目录目录 1 1 课程设计的目的及目标 课程设计的目的及目标 1 1 1 1 课程设计的目的课程设计的目的 1 1 1 2 课程设计的目标课程设计的目标 1 1 2 2 概要设计 概要设计 1 1 2 1 开发工具及环境开发工具及环境 1 1 2 2 任务及需求分析任务及需求分析 1 1 2 3 工作原理工作原理 1 1 3 3 具体实现 具体实现 2 2 3 1 类设计类设计 2 2 4 4 运行调试与分析讨论 运行调试与分析讨论 3 3 5 5 设计体会与小结 设计体会与小结 1010 6 6 参考文献 参考文献 1010 附录 源程序 附录 源程序 1 10 0 0 1 课程设计的目的及目标 1 11 1 课程设计的目的 课程设计的目的 本次课程设计的目的是为了让学生使用 java 语言编程的知识 通 过编写一个简单的五子棋游戏 检验一个学期以来学习 java 语言的收 获成果 以及检查自己在学习 java 语言的过程中的漏洞来弥补不足 以 便以后更好 更熟练地使用 java 语言来编写方便 娱乐生活的程序 1 21 2 课程设计的目标 课程设计的目标 本次课程设计所要求的五子棋游戏应达到以下几方面的要求 1 运行程序后即刻出现棋盘并可以开始下棋 2 先手为黑棋 后手为白棋 人为先手 计算机为后手 3 程序能响应鼠标点击并在相应位置画出棋子 4 计算机具有一定的智能 能够与人进行五子棋对弈 5 程序可以正确判断出局面的胜负情况并及时报出结果 终止棋局 6 尽可能实现人人对战功能 2 概要设计 2 12 1 开发工具及环境开发工具及环境 文本编辑器 记事本 运行环境 命令提示符 DOS 环境 2 22 2 任务及需求分析任务及需求分析 设计人机对弈五子棋程序 实现人与计算机进行五子棋对弈 具体任务如下 1 棋盘布局设计 2 计算机下子的算法分析 3 编程实现棋子的绘制以及具体的再来一局 认输 悔棋 退出游戏等功能 4 运行测试程序 调试纠正运行错误 2 32 3 工作原理工作原理 1 工作过程 运行程序后 首先出现五子棋游戏窗体 用户点击游戏菜单项 出现相关子菜单 选择模 式子菜单中的人机对弈便可进入人机对弈模式 然后移动鼠标 在窗体的棋盘上点击鼠标下子 即可在棋盘中相应位置显示下的对应棋子 接下来计算机会根据用户下的棋 采取穷举法选择 八个方向上赢面最大的位置下子 用户与计算机谁先达到五子连成一线 谁便是获胜方 若选 择模式子菜单中的人人对战便可进入人人对战模式 两个用户可同时玩游戏 同样谁先达到五 子连成一线 谁便是获胜方 若下棋过程中 双方中有一方要认输 便可选择游戏菜单项中的 认输按钮 认输的一方显然失败 另外一方获胜 若双方中有一方要悔棋 便可选择游戏菜单 项中的悔棋按钮 经过对方的同意后 才能够重新翻盘 再来一局 若双方势均力敌 可选择 1 游戏菜单项中的棋盘子菜单 点击大号棋盘或超大号棋盘 以便避免棋盘空间不足造成无法分 出胜负的情况 若觉得窗体外观不太美观 可选择外观菜单中的三个选项进行选择 为了显示 程序的相关制作信息 程序提供了版本菜单进行显示 具体功能实现见第四章节图 2 工作原理 利用图形界面编程实现 利用到的知识点包括 窗体的创建和应用界面布局 事件处理 包括按钮的动作事件处理 鼠标事件 窗体事件 接口知识 事件处理中通过接口约束事件监听者必须实现相应接口中定义的方法 流程控制语句 通过条件判断区分不同事件源 作不同操作处理 3 具体实现 3 13 1 类设计类设计 1 类名 ChessFrame 作用 创建五子棋游戏主窗体和菜单 该类继承 JFrame 类 实现 ActionListener 接口 类中含有的方法介绍 ChessFrame 构造方法 构造五子棋游戏的主窗体 makeMenu 方法 构造五子棋游戏的主菜单 makeMenuItem 方法 构造五子棋游戏的菜单项 makeRadioButtonMenuItem 方法 构造五子棋游戏的单选按钮式菜单项 2 类名 ChessModel 作用 规定棋盘的宽度 高度 棋盘的模式 规定棋盘方格的横向 纵向坐标 棋盘方格的 横向 纵向坐标所对应的棋子颜色 并且规定了记录棋盘棋子信息数组arrMapShow只有4个值 1 2 3 1 其中1代表该棋盘方格上下的棋子为黑子 2代表该棋盘方格上下的棋子为白子 3代表为该棋盘方格上没有棋子 1代表该棋盘方格不能够下棋子 定义了交换棋手的标识符 isOdd 棋盘方格上是否有棋子的标识符isExist 类中含有的主要方法介绍 ChessModel 构造方法 根据不同的棋盘模式 modeChess 来构建对应大小的棋盘 badxy 方法 判断下子的横向 纵向坐标是否越界 readyplay 方法 判断该坐标位置是否可下棋子 play 方法 在该坐标位置下棋子 computerDo 方法 用穷举法判断每一个坐标点的八个方向的的最大棋子数 最后得 出棋子数最大值的坐标 计算机再下子 这八个方向分别是 左 右 上 下 左上 左下 右上 右下 checkMax 方法 计算棋盘上某一方格上八个方向棋子的最大值 showSuccess 方法 赢棋后的提示 showDefeat 方法 输棋后的提示 3 类名 MainPanel 作用 1 构建一个面板 在该面板上画上棋盘 2 处理在该棋盘上的鼠标事件 如鼠标左键点击 鼠标右键点击 鼠标拖动等 该类继承 JPanel 类 实现 MouseListener MouseMotionListener 接口 2 类中含有的主要方法介绍 paintComponent 方法 根据坐标计算出棋盘方格棋子的信息 如白子还是黑子 然后调用 draw 方法在棋盘上画出相应的棋子 mousePressed 方法 响应鼠标的点击事件 根据鼠标的点击来下棋 根据下棋判断 胜负等 mouseMoved 方法 响应鼠标的拖动事件 4 类名 FiveChessAppletDemo 作用 显示五子棋游戏窗体 类中含有的方法介绍 main 方法 程序运行的入口 创建了 ChessFrame 类的一个实例对象 cf 并启动 屏幕显示显示该实例对象 5 类名 ChessWindowEvent 作用 响应退出窗口 类中含有的方法介绍 windowClosing 方法 处理窗口关闭 ChessWindowEvent 构造方法 提供无参构造方法 4 运行调试与分析讨论 图 4 1 人机对弈模式选择界面 3 图 4 2 人机对弈界面 图 4 3 结果信息显示界面 4 图 4 4 人人对战模式选择界面 图 4 5 悔棋界面 图 4 6 悔棋提示界面 5 图 4 7 悔棋后棋盘清空提示界面 图 4 8 悔棋后棋盘清空界面 6 图 4 9 认输界面 图 4 10 认输提示界面 7 图 4 11 认输后结果信息界面 图 4 12 棋盘选择界面 8 图 4 13 外观类型选择界面 图 4 14 版本选择界面 9 图 4 15 版本信息介绍界面 5 5 设计体会与小结 设计体会与小结 通过独立完成五子棋对弈系统的后 发现自己在进行软件开发方面提高不少 同时积极利 用所学到的新技术用于自己的设计开发过程 另外 在整个开发的过程中 时间也比较仓促 因此 该系统必然会存在一些缺陷和不足 如 没有讨论五子棋禁手的问题 另一个就是电脑 按即定的算法去与玩家下子 这种算法有 固定 不太会变 玩家若是仔细观察 可以掌握 其规律 还有就是界面不是很华丽有待改进 尽管本系统存在着很多不足 但其实现了最重要 的功能就是有人工智能 这也让我对计算机中的人工智能领域有一定的了解 另一个就在做系 统的过程中 我学到了 Java 的一些基本结构 还有就是对于 Java 有更深一步的认识 6 参考文献 1 丁振凡 JAVA 语言程序设计 清华大学出版社 2 丁振凡 JAVA 语言程序设计实验指导与习题解答 清华大学出版社 3 Charles Petzold Windows 程序设计 北京大学出版社 2002 4 陈明 Java 语言程序设计 清华大学出版社 附录 源程序 import java awt import java awt event import javax swing class ChessFrame extends JFrame implements ActionListener private String strsize 标准棋盘 大号棋盘 超大号棋盘 private String strmode 人机对弈 人人对战 public static boolean iscomputer true checkcomputer true private int width height 10 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 14 14 JMenuBar mbar new JMenuBar this setJMenuBar mbar JMenu gameMenu new JMenu 游戏 mbar add makeMenu gameMenu new Object 再来一局 null 棋盘 null 模式 null 认输 null 悔棋 null 退出 this JMenu lookMenu new JMenu 外观 mbar add makeMenu lookMenu new Object 类型一 类型二 类型三 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 items i null m addSeparator else if items i 棋盘 JMenu jm new JMenu 棋盘 ButtonGroup group new ButtonGroup JRadioButtonMenuItem rmenu for int j 0 j strsize length j rmenu makeRadioButtonMenuItem strsize j target if j 0 rmenu setSelected true jm add rmenu group add rmenu m add jm else if items i 模式 11 JMenu jm new JMenu 模式 ButtonGroup group new ButtonGroup JRadioButtonMenuItem rmenu for int h 0 h strmode length h rmenu makeRadioButtonMenuItem strmode h target if h 0 rmenu setSelected true jm add rmenu group add rmenu m add jm else m add makeMenuItem items i 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 24 h 27 if this checkcomputer this iscomputer true else this iscomputer false 12 mp setModel cm mp repaint public boolean getiscomputer return this iscomputer public void restart int modeChess cm getModeChess if modeChess 0 cm new ChessModel modeChess MapSize cm getWidth cm getHeight public void actionPerformed ActionEvent e String arg e getActionCommand try if arg equals 类型三 UIManager setLookAndFeel com sun java swing plaf windows WindowsLookAndFeel else if arg equals 类型二 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 标准棋盘 this width 14 this height 14 cm new ChessModel 1 MapSize this width this height SwingUtilities updateComponentTreeUI this if arg equals 大号棋盘 this width 18 this height 18 cm new ChessModel 2 MapSize this width this height SwingUtilities updateComponentTreeUI this if arg equals 超大号棋盘 this width 22 this height 22 cm new ChessModel 3 MapSize this width this height 13 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 悔棋 if checkcomputer int result JOptionPane showConfirmDialog this FiveChessAppletDemo isBlack false 白方悔棋 黑方是否同意 黑方悔棋 白方是否 同意 if result 0 JOptionPane showMessageDialog this 双方同意悔棋决定 棋盘清空 请继续 restart if arg equals 认输 int result JOptionPane showConfirmDialog this 是否认输 if result 0 JOptionPane showMessageDialog this 游戏结束 FiveChessAppletDemo isBlack true 黑方认输 白方获胜 白方认输 黑方获胜 if arg equals 关于 JOptionPane showMessageDialog null 制作者 黄甜 熊婷婷 谭飘宝 汤素娟 邓 婷 n 制作时间 2015 年 6 月 25 日 课程设计制作介绍 JOptionPane PLAIN MESSAGE if arg equals 退出 System exit 0 class ChessModel 14 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 true if modeChess 1 PanelInit 14 14 modeChess if modeChess 2 PanelInit 18 18 modeChess if modeChess 3 PanelInit 22 22 modeChess private void PanelInit int width int height int modeChess this width width this height height this modeChess modeChess arrMapShow new int width 1 height 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 arrMapShow i j 1 this arrMapShow i j 2 return true return false public void readyplay int x int y if badxy x y return if chessExist x y 15 return this arrMapShow x y 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 arrMapShow x y 1 else setisOdd true this arrMapShow x y 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 arrMapShow this x this 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 16 int x temp x y temp y int x temp1 x temp y temp1 y temp for int i 1 ithis width break if this arrMapShow x temp1 y temp1 black or white num else break x temp1 x temp for int i 1 i 5 i x temp1 1 if x temp1 0 break if this arrMapShow x temp1 y temp1 black or white num else break if num 5 max temp num x temp1 x temp y temp1 y temp num 0 for int i 1 i 5 i y temp1 1 if y temp1 0 break if this arrMapShow x temp1 y temp1 black or white num else break y temp1 y temp for int i 1 ithis height break if this arrMapShow x temp1 y temp1 black or white num else break if num max temp x temp1 x temp y temp1 y temp num 0 for int i 1 i 5 i x temp1 1 y temp1 1 if y temp1 0 x temp1 0 break if this arrMapShow x temp1 y temp1 black or white num else break x temp1 x temp y temp1 y temp for int i 1 ithis height x temp1 this width break if this arrMapShow x temp1 y temp1 black or white num else break if num max temp x temp1 x temp y temp1 y temp num 0 for int i 1 i 5 i x temp1 1 y temp1 1 if y temp1this width break if this arrMapShow x temp1 y temp1 black or white num else break x temp1 x temp y temp1 y temp for int i 1 ithis 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 arrMapShow x temp1 y temp1 arrvalue num else break x temp1 x temp for int i 1 i 6 i x temp1 1 if x temp1 0 break if this arrMapShow x temp1 y temp1 arrvalue num else break if num 5 return true x temp1 x temp y temp1 y temp num 1 for int i 1 i 6 i 19 y temp1 1 if y temp1 0 break if this arrMapShow x temp1 y temp1 arrvalue num else break y temp1 y temp for int i 1 ithis height break if this arrMapShow x temp1 y temp1 arrvalue num else break if num 5 return true x temp1 x temp y temp1 y temp num 1 for int i 1 i 6 i x temp1 1 y temp1 1 if y temp1 0 x temp1 0 break if this arrMapShow x temp1 y temp1 arrvalue num else break x temp1 x temp y temp1 y temp for int i 1 ithis height x temp1 this width break if this arrMapShow x temp1 y temp1 arrvalue num else break if num 5 20 return true x temp1 x temp y temp1 y temp num 1 for int i 1 i 6 i x temp1 1 y temp1 1 if y temp1this width break if this arrMapShow x temp1 y temp1 arrvalue num else break x temp1 x temp y temp1 y temp for int i 1 ithis height x temp1 0 break if this arrMapShow x temp1 y temp1 arrvalue num else break if num 5 return true return false public void showSuccess JPanel jp JOptionPane showMessageDialog jp 你赢了 结果 JOptionPane INFORMATION MESSAGE public void showDefeat JPanel jp JOptionPane showMessageDialog jp 你输了 结果 JOptionPane INFORMATION MESSAGE class ChessWindowEvent extends WindowAdapter public void windowClosing WindowEvent e System exit 0 ChessWindowEvent public class FiveChessAppletDemo public static Boolean isBlack true 21 public static void main String args ChessFrame cf new ChessFrame cf show class MainPanel extends JPanel implements MouseListener MouseMotionListener private int width height private Che
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 大脚丫跳芭蕾教学设计
- 《财务分析的教学方法和流程》课件
- 《市场监管法规与实践》课件
- 射阳三中初一试卷及答案
- 陕西地生会考试卷及答案a卷
- 厦门二中体考试卷及答案
- 2025民间房屋买卖合同范本
- 2025商场电力供应合同模板
- 浙江国企招聘2025衢州古城文化旅游区运营管理有限公司招聘21人笔试参考题库附带答案详解
- 石棉制品在油气管道的保温应用考核试卷
- 2025年车位转让协议模板
- 数据分析能力提升培训心得体会
- 医疗器械安全知识培训
- 2025年中储粮集团河南分公司招聘(114人)笔试参考题库附带答案详解
- 2024-2025学年广东省高三上学期期末四校联考英语试题(解析版)
- 中国老年社区获得性肺炎急诊诊疗专家共识(2023版)解读
- 淄博2024年山东淄博市技师学院高层次人才招聘(第二批)笔试历年典型考点(频考版试卷)附带答案详解
- 农村自建房主体包工包料承包合同模板
- 高教社马工程伦理学(第二版)教学课件11
- 破釜沉舟成语故事课件全
- 能源行业碳排放监测与减排系统设计方案
评论
0/150
提交评论