当前位置:网站首页>五子棋人机对战实现
五子棋人机对战实现
2022-07-27 21:02:00 【biyezuopin】
五子棋人机对战实现
项目构思:
- 我们将五子棋游戏的实现分为两部分实现
- 可视化界面(GUI)
- 人机对战算法实现
- 可视化界面我们选择了 Java.swing 包内的 API 和 Graphics 类里的绘图工具进行开发
- 人机对战的算法实现我们参考了 Fucking Algorithm 算法库,主要采取局部暴力搜索算法。
环境部署
- 编译语言:Java
- 编译器:Eclipse
项目实现:
可视化界面
① 用 JFrame 和 JPeal 设置整个下棋界面和右侧控制界面。

② 用 JButton 添加开始新游戏,认输,悔棋的功能,并将按 钮添加到右侧控制界面上去。

③ 增加一个选择棋盘样式的 JComboBox 选项按钮,并在选择框前面用 JLabel 加上功能只是功能指示文字。

④ 将所有的按键都添加到 ButtonListener 里边来控制完成相关操作。

⑤ 将上述按键在 ButtonListener 中通过重写 actionPerformed 函数赋予其相应功能用 if else、e.getActionCommand().equals()和 getSelectedItem().equals 来判断所操作的按键是进行的那个功能。其中在“开始新游戏”这个选项功能里边还用 JOptionPane.showInputDialog()和 JOptionPane.showMessageDiaog()来可视化了模式输入选择框来选择要进行“人人对战”还是“人机对战”并且有防止错误输入的循环输入输入模式。
⑥ 通过 frameListener 类来构建一个游戏并通过 paint 函数来将右边的画面呈现。其中在 paint 类中会根据相关变量来选择所要呈现的棋盘类型,最终将所选棋盘呈现。
人机对战算法实现
algorithmA 的参数为棋子颜色(1 为黑子,2 为白子),用于实现需求:能够随机指定黑方和白方。

先计算不同落子位置权值(基于 map 权值表以及 unoinWeight 赋权计算)赋值方案(基于棋谱),此处主要以先手开局棋谱为例

代码实现赋值(基于 map 权值表以及 unoinWeight 赋权计算)


搜索方法:局部暴力搜索法,基于此棋子位置,分别向上、下、左、右、左上、右下、右上、左下八向搜索,根据落子此处位置处于不同的活眠状态追加权值,若此处已有棋子(即 gf.isAvail[i][j]==0)则跳过此向搜索。

对存储权值的数组 weightArray 内的元素进行判断

⑥ 落子后要注意重置权值数组 weightArray,每次落子对棋局形式都有不同的影响,故棋盘每处的权值是动态变化的,所以每次落子后都要重新搜索计算 新一轮的权值。


边栏推荐
- Join hands with Changjiang storage, jiangbolong launches the world's smallest expansion card
- Under the epidemic, TSMC's growth in the first quarter exceeded expectations, with 7Nm accounting for 35%! Second quarter or record high
- JUC工具包学习
- 用3dmax做折扇的思路方法与步骤
- Character stream learning 14.3
- Ideas, methods and steps of making folding fans with 3DMAX
- Redis hash underlying data structure
- Redis 哈希Hash底层数据结构
- iMeta | 国际标准刊号ISSN印刷版正式确认,双ISSN申请完成
- [number recognition] handwritten number recognition based on knowledge base with matlab code
猜你喜欢

远程调试 idea配置remote debug、在远程服务器的程序中,添加JVM启动参数-Xdebug
![[GNN report] Tang Jian, Montreal, Canada: Geometric deep learning for drug discovery](/img/ef/aa490aeff5a0690257cd6eca7d5e28.png)
[GNN report] Tang Jian, Montreal, Canada: Geometric deep learning for drug discovery

用3dmax做折扇的思路方法与步骤

Nature review: preferential effects in the formation of microbial communities
Blood spitting finishing nanny level series tutorial - playing Fiddler bag capturing tutorial (5) - detailed explanation of fiddler monitoring panel
![[image defogging] image defogging based on dark channel and non-mean filtering with matlab code](/img/44/6120682f9571f6ad35f8b9249b7fea.png)
[image defogging] image defogging based on dark channel and non-mean filtering with matlab code

Date的使用

Spark 离线开发框架设计与实现

主数据管理理论与实践

小程序容器技术超有料,可以让移动研发效率大幅提升
随机推荐
史上最简明的 Tcpdump 入门指南,看这一篇就够了
【JS 逆向百例】某公共资源交易网,公告 URL 参数逆向分析
请求合并哪家强——接口请求合并的3种技巧,性能直接爆表
Common Taylor expansion
[signal processing] weak signal detection in communication system based on the characteristics of high-order statistics with matlab code
reduce错误示范
[JS reverse hundred examples] a public resource trading network, reverse analysis of announcement URL parameters
数据中台的那些“经验与陷阱”
Which one is better to request to merge -- three skills of interface request merging, and the performance directly explodes the table
华为鸿蒙 3 正式发布,这个安全功能解决了一大痛点
他山之石 | 蚂蚁超大规模知识图谱构建及应用
File & recursion 14.1
What are the advantages of Tita's OKR system over other similar products or shared documents?
Redis 哈希Hash底层数据结构
面试官:说一下网络数据传输的具体流程
技术认证 | 图扑软件携手华为云再创合作共赢新局面
The technology of applet container is very promising, which can greatly improve the efficiency of mobile R & D
QT with OpenGL(Shadow Mapping)(平行光篇)
2022/7/26
[GNN report] Tang Jian, Montreal, Canada: Geometric deep learning for drug discovery