当前位置:网站首页>五子棋人机对战实现
五子棋人机对战实现
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,每次落子对棋局形式都有不同的影响,故棋盘每处的权值是动态变化的,所以每次落子后都要重新搜索计算 新一轮的权值。


边栏推荐
- [number recognition] recognize 0-9 numbers based on Hopfield neural network with matlab code
- 四次挥手的Socket交互流程
- 2022年土木,建筑与环境工程国际会议(ICCAEE 2022)
- 总投资600亿!富士康半导体高端封测项目正式落户青岛
- ZCMU--1720: 死亡如风,我要装逼
- QT with OpenGL(Shadow Mapping)(平行光篇)
- 采用汇顶屏下光学指纹方案,三星Galaxy A71 5G上市
- Record the errors about formatc in R language
- Lua基础语法学习
- Join hands with Changjiang storage, jiangbolong launches the world's smallest expansion card
猜你喜欢

Character stream learning 14.3
![[signal processing] weak signal detection in communication system based on the characteristics of high-order statistics with matlab code](/img/a7/8f0d929457d285adc8020c7f5d33ea.png)
[signal processing] weak signal detection in communication system based on the characteristics of high-order statistics with matlab code

怎么使用C# Winform实现复制文件显示进度

Lua basic grammar learning
![[image detection] realize the detection of nostrils and pupil circles based on combined separation filter matlab source code](/img/6a/92ace81b798810299a2fec0ce6ae70.png)
[image detection] realize the detection of nostrils and pupil circles based on combined separation filter matlab source code

NDK 系列(6):说一下注册 JNI 函数的方式和时机

Interviewer: let's talk about the specific process of network data transmission

After returning to mixlab for three days, "creative team" cured my spiritual internal friction

远程调试 idea配置remote debug、在远程服务器的程序中,添加JVM启动参数-Xdebug

2022年土木,建筑与环境工程国际会议(ICCAEE 2022)
随机推荐
Record the errors about formatc in R language
How to use xshell Free Edition
2019年全球十大半导体厂商:英特尔重回第一,苹果逆势大涨
[number recognition] recognize 0-9 numbers based on Hopfield neural network with matlab code
Redis 哈希Hash底层数据结构
Redis的分布式锁
Redis distributed lock
Software test function test full set of common interview questions [function test] interview summary 4-2
Nail alarm tool
Application of user portrait in precise push of wechat official account of scientific journals
[C language] address book (dynamic version)
Starfish Os X MetaBell战略合作,元宇宙商业生态更进一步
怎么使用C# Winform实现复制文件显示进度
Pentium fast system call learning
Under the epidemic, TSMC's growth in the first quarter exceeded expectations, with 7Nm accounting for 35%! Second quarter or record high
The print version of imeta | international standard ISSN is officially confirmed, and the application for dual ISSN is completed
JUC工具包学习
一加将在2020年释放ODM订单,发力中低端市场
小程序容器技术超有料,可以让移动研发效率大幅提升
Error:svn: E155010: ‘/Users/.../Desktop/wrokspace/xxx‘ is scheduled for addition, but is missing