欢迎来到三一办公! | 帮助中心 三一办公31ppt.com(应用文档模板下载平台)
三一办公
全部分类
  • 办公文档>
  • PPT模板>
  • 建筑/施工/环境>
  • 毕业设计>
  • 工程图纸>
  • 教育教学>
  • 素材源码>
  • 生活休闲>
  • 临时分类>
  • ImageVerifierCode 换一换
    首页 三一办公 > 资源分类 > DOC文档下载  

    人工智能实验1 五子棋游戏 .doc

    • 资源ID:2390418       资源大小:186KB        全文页数:17页
    • 资源格式: DOC        下载积分:8金币
    快捷下载 游客一键下载
    会员登录下载
    三方登录下载: 微信开放平台登录 QQ登录  
    下载资源需要8金币
    邮箱/手机:
    温馨提示:
    用户名和密码都是您填写的邮箱或者手机号,方便查询和重复下载(系统自动生成)
    支付方式: 支付宝    微信支付   
    验证码:   换一换

    加入VIP免费专享
     
    账号:
    密码:
    验证码:   换一换
      忘记密码?
        
    友情提示
    2、PDF文件下载后,可能会被浏览器默认打开,此种情况可以点击浏览器菜单,保存网页到桌面,就可以正常下载了。
    3、本站不支持迅雷下载,请使用电脑自带的IE浏览器,或者360浏览器、谷歌浏览器下载即可。
    4、本站资源下载后的文档和图纸-无水印,预览文档经过压缩,下载后原文更清晰。
    5、试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓。

    人工智能实验1 五子棋游戏 .doc

    人工智能实验报告班级:计研-12班学号:2012312120105姓名:孔德星实验一 五子棋游戏1实验目的(1)了解机器博弈相关技术;(2)掌握机器博弈的分析方法。2实验内容(1)实现简单的五子棋游戏;(2)插入一个下载的棋盘图片作为自己的棋盘;(3)插入一段音乐实现背景音乐的播放;(4)当有输赢出现时弹出一个显示战况的对话框;(5)在菜单栏上添加一个战况的标题,当单击时弹出显示当时战况的对话框。3 实验报告要求(1)简述实验原理及方法,并请给出程序设计流程图。A、实验原理:假设电脑用白棋,人用黑棋。在前两步时由于不会有赢的趋势,所以白子在黑子的周围随机下棋。第三步以后,电脑要扫描整个棋局,构建博弈树,然后用深度优先算法分析并评估自己是否占优势。如果自己占优势,则进攻,否则,则防守。当然在整个下棋过程中,会检查是否已经五子连,即是否有人(电脑)获胜。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.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 javax.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 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()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("声音选项");JMenuItem 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");zhankuang.addActionListener(this);bar0.add(menu0);bar0.add(menu1);bar0.add(zhankuang);bar0.add(jf);jf.setEditable(false);for(int i=0;i<19;i+)for(int j=0;j<19;j+)allChessij=0; /初始化所有的位置都是没有棋子的,1代表有黑棋子,-1代表有白棋子。 for(int i=0;i<19;i+) for(int j=0;j<19;j+) for(int h=0;h<5;h+) scanp.shapeijh=0; scanc.shapeijh=0; this.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;i<19;i+) for(int j=0;j<19;j+) allChessij=0; for(int i=0;i<19;i+) for(int j=0;j<19;j+) for(int h=0;h<5;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(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.getY(); jf.setText("这是第"+i+"步"); a=(x+5)/20; b=(y+5)/20; /当鼠标点击的位置不在棋盘时 if(x+5)/20<2|(y+5)/20<2|(x-5)/20>19|(y-5)/20>19) return ; else if(allChessb-2a-2 = 0&&棋子颜色=1) g.setColor(Color.black); g.fillOval(a*20-5,b*20-5,10,10); i+; 棋子颜色=棋子颜色*(-1); allChessb-2a-2=1; if (Judge.judge(allChess,1) jf.setText("黑棋赢!"); win1+; zhankuang.setText("游戏战况"); 棋子颜色=2; JOptionPane.showMessageDialog(null, "恭喜您,您赢了!n当前战况是 黑棋:白棋 "+win1+":"+win2); removeMouseListener(this); return; if(i<=2&&棋子颜色=-1) autoPlay.autoPlay(allChess,b-2,a-2); g.setColor(Color.white); g.fillOval(autoPlay.y+2)*20-5,(autoPlay.x+2)*20-5,10,10); 棋子颜色=棋子颜色*(-1); allChessautoPlay.xautoPlay.y=-1; if (Judge.judge(allChess,-1) jf.setText("白棋赢!");JOptionPane.showMessageDialog(null, "很遗憾,电脑赢了!n当前战况是 黑棋:白棋 "+win1+":"+win2); zhankuang.setText("游戏战况"); win2+; 棋子颜色=2; removeMouseListener(this); return; else zhankuang.setText("请黑棋下子"); if(i>2&&棋子颜色=-1) scanp.scan(allChess,1); scanc.scan(allChess,-1); sort.sort(scanp.shape); sort.sort(scanc.shape); evaluatep.evaluate(scanp.shape); evaluatec.evaluate(scanc.shape); 棋子颜色=棋子颜色*(-1); if(evaluatep.max>evaluatec.max) g.setColor(Color.white);g.fillOval(evaluatep.max_y+2)*20-5,(evaluatep.max_x+2)*20-5,10,10); allChessevaluatep.max_xevaluatep.max_y=-1; zhankuang.setText("请黑棋下子"); for(int i=0;i<19;i+) for(int j=0;j<19;j+) for(int h=0;h<5;h+) scanp.shapeijh=0; scanc.shapeijh=0; else g.setColor(Color.white);g.fillOval(evaluatec.max_y+2)*20-5,(evaluatec.max_x+2)*20-5,10,10); allChessevaluatec.max_xevaluatec.max_y=-1; if (Judge.judge(allChess,-1) jf.setText("白棋赢!");JOptionPane.showMessageDialog(null, "很遗憾,电脑赢了!n当前战况是 黑棋:白棋 "+win1+":"+win2); 棋子颜色=2; win2+; zhankuang.setText("游戏战况"); removeMouseListener(this); return; else zhankuang.setText("请黑棋下子"); for(int i=0;i<19;i+) for(int j=0;j<19;j+) for(int h=0;h<5;h+) scanp.shapeijh=0; scanc.shapeijh=0; public void mouseReleased(MouseEvent arg0) private ImageIcon background=new ImageIcon("fiveChess/五子棋.jpg");public void paintComponent(Graphics g)super.paintComponent(g);g.drawImage(background.getImage(),0,0,background.getIconWidth(),background.getIconHeight(),this);static JFrame getMainFrame ()JFrame main = new JFrame();main.setContentPane(new Main();main.setJMenuBar(bar0);main.setTitle("连珠五子棋");main.setLocation(100, 200);main.setSize(440, 440);main.setVisible(true); main.addWindowListener(new WindowAdapter() public void windowClosing(WindowEvent e) System.exit(0); );return main;public static void main(String args)getMainFrame();B、前两步自动播放的类package fiveChess;public class AutoPlay int x,y; void autoPlay(int chesspad,int a,int b) int randomNumber=(int)(Math.random()*8)+1; switch(randomNumber) case(1): if(chesspada-1b-1=0) x=a-1;y=b-1; else if(chesspada-2b-2=0) x=a-2;y=b-2; else x=a-3;y=b-3; break; case(2): if(chesspada-1b=0) x=a-1;y=b; else if(chesspada-2b=0) x=a-2;y=b; else x=a-3;y=b; break; case(3): if(chesspada-1b+1=0) x=a-1;y=b+1; else if(chesspada-2b+2=0) x=a-2;y=b+2; else x=a-3;y=b+3; break; case(4): if(chesspadab+1=0) x=a;y=b+1; else if(chesspadab+2=0) x=a;y=b+2; else x=a;y=b+3; break; case(5): if(chesspada+1b+1=0) x=a+1;y=b+1; else if(chesspada+2b+2=0) x=a+2;y=b+2; else x=a+3;y=b+3; break; case(6): if(chesspada+1b=0) x=a+1;y=b; else if(chesspada+2b=0) x=a+2;y=b; else x=a+3;y=b; break; case(7): if(chesspada+1b-1=0) x=a+1;y=b-1; else if(chesspada+2b-2=0) x=a+2;y=b-2; else x=a+3;y=b-3; break; case(8): if(chesspadab-1=0) x=a;y=b-1; else if(chesspadab-2=0) x=a;y=b-2; elsex=a;y=b+3; break; C、扫描棋盘的类package fiveChess;public class Scanint shape=new int19195; void scan(int chesspad,int colour) int i,j; for(i=0;i<=18;i+) for(j=0;j<=18;j+) if(chesspadij=0) int m=i,n=j; while(n-1>=0&&chesspadm-n=colour) shapeij0+; n=j;while(n+1<=18&&chesspadm+n=colour) shapeij0+; n=j;while(m-1>=0&&chesspad-mn=colour) shapeij1+; m=i;while(m+1<=18&&chesspad+mn=colour) shapeij1+; m=i;while(m-1>=0&&n+1<=18&&chesspad-m+n=colour) shapeij2+; m=i;n=j; while(m+1<=18&&n-1>=0&&chesspad+m-n=colour) shapeij2+; m=i;n=j; while(m-1>=0&&n-1>=0&&chesspad-m-n=colour) shapeij3+; m=i;n=j; while(m+1<=18&&n+1<=18&&chesspad+m+n=colour)shapeij3+; D、评估棋盘的类package fiveChess;public class Evaluate int max_x,max_y,max; public void evaluate(int shape) int i=0,j=0; for(i=0;i<19;i+) for(j=0;j<19;j+) switch(shapeij0) case 5: shapeij4=200; break; case 4: switch(shapeij1) case 4: shapeij4=150+shapeij2+ shapeij3; break; case 3: shapeij4=100+ shapeij2+ shapeij3; break; default: shapeij4=50+ shapeij2+ shapeij3; break; case 3: switch(shapeij1) case 3: shapeij4=75+

    注意事项

    本文(人工智能实验1 五子棋游戏 .doc)为本站会员(laozhun)主动上传,三一办公仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知三一办公(点击联系客服),我们立即给予删除!

    温馨提示:如果因为网速或其他原因下载失败请重新下载,重复下载不扣分。




    备案号:宁ICP备20000045号-2

    经营许可证:宁B2-20210002

    宁公网安备 64010402000987号

    三一办公
    收起
    展开