人工智能实验1 五子棋游戏 .doc
《人工智能实验1 五子棋游戏 .doc》由会员分享,可在线阅读,更多相关《人工智能实验1 五子棋游戏 .doc(17页珍藏版)》请在三一办公上搜索。
1、人工智能实验报告班级:计研-12班学号:2012312120105姓名:孔德星实验一 五子棋游戏1实验目的(1)了解机器博弈相关技术;(2)掌握机器博弈的分析方法。2实验内容(1)实现简单的五子棋游戏;(2)插入一个下载的棋盘图片作为自己的棋盘;(3)插入一段音乐实现背景音乐的播放;(4)当有输赢出现时弹出一个显示战况的对话框;(5)在菜单栏上添加一个战况的标题,当单击时弹出显示当时战况的对话框。3 实验报告要求(1)简述实验原理及方法,并请给出程序设计流程图。A、实验原理:假设电脑用白棋,人用黑棋。在前两步时由于不会有赢的趋势,所以白子在黑子的周围随机下棋。第三步以后,电脑要扫描整个棋局,构
2、建博弈树,然后用深度优先算法分析并评估自己是否占优势。如果自己占优势,则进攻,否则,则防守。当然在整个下棋过程中,会检查是否已经五子连,即是否有人(电脑)获胜。B、流程图:(2)源程序清单:本程序用Java语言编写,共有七个类。A、主界面类package fiveChess;import java.awt.Color;import java.awt.Graphics;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.awt.event.InputEvent;import java
3、.awt.event.MouseEvent;import java.awt.event.MouseListener;import java.awt.event.WindowAdapter;import java.awt.event.WindowEvent;import javax.swing.ImageIcon;import javax.swing.JButton;import javax.swing.JFrame;import javax.swing.JMenu;import javax.swing.JMenuBar;import javax.swing.JMenuItem;import j
4、avax.swing.JOptionPane;import javax.swing.JPanel;import javax.swing.JTextField;public class Main extends JPanel implements ActionListener,MouseListenerint allChess = new int1919;JButton zhankuang;int x = 0;/步数计算int y = 0;int i = 0;int win1 = 0;/记录黑棋赢的局数int win2 = 0;/记录白棋赢的局数int 棋子颜色=1;Scan scanp=new
5、 Scan();/定义两个扫描棋盘的类,一个扫描白字,一个扫描黑子Scan scanc=new Scan(); AutoPlay autoPlay=new AutoPlay();/定义一个音乐器播放对象Evaluate evaluatep=new Evaluate();/定义两个评估棋盘的类,一个评估白字,一个评估黑子Evaluate evaluatec=new Evaluate();Sort sort=new Sort();JTextField jf = new JTextField(10);static JMenuBar bar0 = new JMenuBar();public Main(
6、)JMenu menu0 = new JMenu(游戏选项);JMenuItem item0 = new JMenuItem(重新开始游戏);item0.setActionCommand(Restart);item0.addActionListener(this);JMenuItem item1 = new JMenuItem(退出游戏);item1.setActionCommand(Exit);item1.addActionListener(this);menu0.add(item0);menu0.add(item1);JMenu menu1 = new JMenu(声音选项);JMenuI
7、tem item2 = new JMenuItem(音乐开);item2.setActionCommand(ON);item2.addActionListener(this);JMenuItem item3 = new JMenuItem(音乐关);item3.setActionCommand(OFF);item3.addActionListener(this);menu1.add(item2);menu1.add(item3);zhankuang = new JButton(游戏战况);/游戏战况标签zhankuang.setActionCommand(ZHANKUANG);zhankuan
8、g.addActionListener(this);bar0.add(menu0);bar0.add(menu1);bar0.add(zhankuang);bar0.add(jf);jf.setEditable(false);for(int i=0;i19;i+)for(int j=0;j19;j+)allChessij=0; /初始化所有的位置都是没有棋子的,1代表有黑棋子,-1代表有白棋子。 for(int i=0;i19;i+) for(int j=0;j19;j+) for(int h=0;h5;h+) scanp.shapeijh=0; scanc.shapeijh=0; this.
9、addMouseListener(this);public void actionPerformed(ActionEvent e) Music back = new Music();if(e.getActionCommand().equals(Exit)System.exit(0);else if(e.getActionCommand().equals(Restart)addMouseListener(this);this.repaint();i=0;jf.setText();zhankuang.setText(游戏战况); for(int i=0;i19;i+) for(int j=0;j1
10、9;j+) allChessij=0; for(int i=0;i19;i+) for(int j=0;j19;j+) for(int h=0;h5;h+) scanp.shapeijh=0; scanc.shapeijh=0; else if(e.getActionCommand().equals(ON)back.pt.start();else if(e.getActionCommand().equals(OFF)back.pt.stop();else if(e.getActionCommand().equals(ZHANKUANG)JOptionPane.showMessageDialog
11、(null, 黑棋:白棋 +win1+:+win2);public void mouseClicked(MouseEvent arg0) public void mouseEntered(MouseEvent arg0) public void mouseExited(MouseEvent arg0) public void mousePressed(MouseEvent e) Graphics g =this.getGraphics();int a=0,b=0; if(e.getModifiers()=InputEvent.BUTTON1_MASK) x = e.getX(); y = e.
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 人工智能实验1 五子棋游戏 人工智能 实验 五子棋 游戏

链接地址:https://www.31ppt.com/p-2390418.html