人工智能实验大作业_第1页
人工智能实验大作业_第2页
人工智能实验大作业_第3页
人工智能实验大作业_第4页
人工智能实验大作业_第5页
已阅读5页,还剩21页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、? 人工智能 ?实验大作业实验题目: 五子棋 专业信息与计算科学 年级091001 姓名郭军伟 学号081001106 指导老师时华 日 期 2021-12-5 实验题目 人工智能之五子棋一、实验目的: 1学习并了解机器博弈相关技术; 2掌握机器博弈的分析方法。二、实验内容: 1实现五子棋游戏功能; 2插入棋盘图片作为自己的棋盘; 3显示战况的对话框; 三、实验原理: 假设电脑用白棋,人用黑棋。在前两步时由于不会有赢的趋势,所以白子在黑子的周围随机下棋。第三步以后,电脑要扫描整个棋局,构建博弈树,然后用深度优先算法分析并评估自己是否占优势。如果自己占优势,那么进攻,否那么,那么防守。当然在整个

2、下棋过程中,会检查是否已经五子连,即是否有人电脑获胜。4、 实验源程序: (一)import jv.wt.Grphics;import jv.wt.Point;import jv.util.Stck;import jvx.swing.ImgeIcon;/* * To chnge this templte, choose Tools | Templtes * nd open the templte in the editor. */* * guojunwei.jv * * Creted on 2021-12-05, 13:24:08 */* * * uthor dministrtor */pub

3、lic clss guojunwei extends jvx.swing.Jrme /* Cretes new orm guojunwei */ public guojunwei() initComponents(); initBord(); this.setResizble(lse); buttonGroup.dd(jRdioButton1); buttonGroup.dd(jRdioButton2); chessmn = new ImgeIconnew ImgeIcon(getClss().getResource("/resource/黑.PNG"), new Imge

4、Icon(getClss().getResource("/resource/白.PNG"); /* This method is clled rom within the constructor to * initilize the orm. * WRNING: Do NOT modiy this code. The content o this method is * lwys regenerted by the orm Editor. */ SuppressWrnings("unchecked") / <editor-old deultstte

5、="collpsed" desc="Generted Code">/GEN-BEGIN:initComponents privte void initComponents() buttonGroup = new jvx.swing.ButtonGroup(); jLbel1 = new jvx.swing.JLbel(); jPnel1 = new jvx.swing.JPnel(); jRdioButton1 = new jvx.swing.JRdioButton(); jRdioButton2 = new jvx.swing.JRdioButt

6、on(); jButton1 = new jvx.swing.JButton(); jButton2 = new jvx.swing.JButton(); jTextield1 = new jvx.swing.JTextield(); jButton3 = new jvx.swing.JButton(); setDeultCloseOpertion(jvx.swing.WindowConstnts.EXIT_ON_CLOSE); jLbel1.setIcon(new jvx.swing.ImgeIcon(getClss().getResource("/resource/棋盘.PNG&

7、quot;); / NOI18N jLbel1.ddMouseListener(new jv.wt.event.Mousedpter() public void mouseClicked(jv.wt.event.MouseEvent evt) jLbel1MouseClicked(evt); ); jRdioButton1.setText("玩家执黑先下"); jRdioButton2.setText("电脑执黑先下"); jButton1.setText("开始游戏"); jButton1.ddctionListener(new j

8、v.wt.event.ctionListener() public void ctionPerormed(jv.wt.event.ctionEvent evt) jButton1ctionPerormed(evt); ); jButton2.setText("再来一局"); jButton2.ddctionListener(new jv.wt.event.ctionListener() public void ctionPerormed(jv.wt.event.ctionEvent evt) jButton2ctionPerormed(evt); ); jTextield1

9、.setont(new jv.wt.ont("宋体", 0, 18); jTextield1.setText("游戏未开始"); jButton3.setText("悔棋"); jButton3.ddctionListener(new jv.wt.event.ctionListener() public void ctionPerormed(jv.wt.event.ctionEvent evt) jButton3ctionPerormed(evt); ); jvx.swing.GroupLyout jPnel1Lyout = new

10、jvx.swing.GroupLyout(jPnel1); jPnel1.setLyout(jPnel1Lyout); jPnel1Lyout.setHorizontlGroup( jPnel1Lyout.cretePrllelGroup(jvx.swing.GroupLyout.lignment.LEDING) .ddGroup(jPnel1Lyout.creteSequentilGroup() .ddContinerGp() .ddGroup(jPnel1Lyout.cretePrllelGroup(jvx.swing.GroupLyout.lignment.LEDING) .ddComp

11、onent(jTextield1, jvx.swing.GroupLyout.DEULT_SIZE, 145, Short.MX_VLUE) .ddGroup(jPnel1Lyout.cretePrllelGroup(jvx.swing.GroupLyout.lignment.TRILING, lse) .ddComponent(jRdioButton2) .ddComponent(jRdioButton1) .ddComponent(jButton1, jvx.swing.GroupLyout.DEULT_SIZE, 89, Short.MX_VLUE) .ddComponent(jButt

12、on2, jvx.swing.GroupLyout.DEULT_SIZE, jvx.swing.GroupLyout.DEULT_SIZE, Short.MX_VLUE) .ddComponent(jButton3, jvx.swing.GroupLyout.PREERRED_SIZE, 92, jvx.swing.GroupLyout.PREERRED_SIZE) .ddContinerGp() ); jPnel1Lyout.setVerticlGroup( jPnel1Lyout.cretePrllelGroup(jvx.swing.GroupLyout.lignment.LEDING)

13、.ddGroup(jPnel1Lyout.creteSequentilGroup() .ddGp(18, 18, 18) .ddComponent(jRdioButton1) .ddPreerredGp(jvx.swing.LyoutStyle ponentPlcement.RELTED) .ddComponent(jRdioButton2) .ddGp(18, 18, 18) .ddComponent(jButton1) .ddGp(18, 18, 18) .ddComponent(jButton2) .ddGp(69, 69, 69) .ddComponent(jTextield1, jv

14、x.swing.GroupLyout.PREERRED_SIZE, 72, jvx.swing.GroupLyout.PREERRED_SIZE) .ddGp(35, 35, 35) .ddComponent(jButton3) .ddContinerGp(228, Short.MX_VLUE) ); jvx.swing.GroupLyout lyout = new jvx.swing.GroupLyout(getContentPne(); getContentPne().setLyout(lyout); lyout.setHorizontlGroup( lyout.cretePrllelGr

15、oup(jvx.swing.GroupLyout.lignment.LEDING) .ddGroup(lyout.creteSequentilGroup() .ddComponent(jLbel1) .ddPreerredGp(jvx.swing.LyoutStyle ponentPlcement.RELTED) .ddComponent(jPnel1, jvx.swing.GroupLyout.DEULT_SIZE, jvx.swing.GroupLyout.DEULT_SIZE, Short.MX_VLUE) ); lyout.setVerticlGroup( lyout.cretePrl

16、lelGroup(jvx.swing.GroupLyout.lignment.LEDING) .ddGroup(jvx.swing.GroupLyout.lignment.TRILING, lyout.creteSequentilGroup() .ddGroup(lyout.cretePrllelGroup(jvx.swing.GroupLyout.lignment.TRILING) .ddComponent(jPnel1, jvx.swing.GroupLyout.lignment.LEDING, jvx.swing.GroupLyout.DEULT_SIZE, jvx.swing.Grou

17、pLyout.DEULT_SIZE, Short.MX_VLUE) .ddComponent(jLbel1, jvx.swing.GroupLyout.DEULT_SIZE, jvx.swing.GroupLyout.DEULT_SIZE, Short.MX_VLUE) .ddContinerGp() ); pck(); / </editor-old>/GEN-END:initComponents intelligence intel = new intelligence(); Stck<Point> bck = new Stck<Point>(); pri

18、vte void jLbel1MouseClicked(jv.wt.event.MouseEvent evt) /GEN-IRST:event_jLbel1MouseClicked i (gmeStrted = lse) return; /玩家下子 int r = (evt.getX() - 30) / 36; int c = (evt.getY() - 30) / 36; System.out.println(r + "," + c); bck.dd(new Point(r, c); lg = lse; i (bordrc = 0) i (irst = 1) turn +

19、= 1; else i (irst = 2) turn -= 1; chessmnturn.pintIcon(jLbel1, jLbel1.getGrphics(), r * 36 + 20, c * 36 + 30); bordrc = 2; count+; else return; /判断胜负 i (judgeWin(new Point(r, c) gmeStrted = lse; return; /计算机智能下子 Point p = intel pPly(); bck.dd(p); lg = true; i (irst = 1) turn -= 1; else i (irst = 2)

20、turn += 1; chessmnturn.pintIcon(jLbel1, jLbel1.getGrphics(), p.x * 36 + 20, p.y * 36 + 30); /判断胜负 i (judgeWin(p) gmeStrted = lse; return; /GEN-LST:event_jLbel1MouseClicked privte void initBord() or (int r = 0; r < BORD_SIZE; r+) or (int c = 0; c < BORD_SIZE; c+) bordrc = 0; /重新开始按钮 privte void

21、 jButton2ctionPerormed(jv.wt.event.ctionEvent evt) /GEN-IRST:event_jButton2ctionPerormed initBord(); repint(); gmeStrted = lse; jButton1.setEnbled(true); intel.initTble(); turn = 1; irst = 0; /GEN-LST:event_jButton2ctionPerormed /开始按钮 privte void jButton1ctionPerormed(jv.wt.event.ctionEvent evt) /GE

22、N-IRST:event_jButton1ctionPerormed count = 0; this.initBord(); i (jRdioButton1.isSelected() = true) irst = 2; gmeStrted = true; jTextield1.setText("游戏已开始!"); jButton1.setEnbled(lse); else i (jRdioButton2.isSelected() = true) irst = 1; gmeStrted = true; jTextield1.setText("游戏已开始!"

23、); jButton1.setEnbled(lse); Point p = intel.onirst(); bck.dd(p); turn -= 1; chessmnturn.pintIcon(jLbel1, jLbel1.getGrphics(), p.x * 36 + 20, p.y * 36 + 30); else return; /GEN-LST:event_jButton1ctionPerormed /悔棋 privte void jButton3ctionPerormed(jv.wt.event.ctionEvent evt) /GEN-IRST:event_jButton3cti

24、onPerormed i (lg = true&&!bck.empty() Point p = bck.pop(); Point p2 = bck.pop(); bordp.xp.y = 0; bordp2.xp2.y = 0; gmeStrted = true; jTextield1.setText("游戏已开始!"); repint(); /GEN-LST:event_jButton3ctionPerormed /* * prm rgs the commnd line rguments */ public sttic void min(String rg

25、s) jv.wt.EventQueue.invokeLter(new Runnble() public void run() new gobngDlg().setVisible(true); ); Override public void pint(Grphics g) super.pint(g); pintChessmn(); Override public void updte(Grphics g) repint(); public void pintChessmn() i (gmeStrted = true) or (int r = 0; r < BORD_SIZE; r+) or

26、 (int c = 0; c < BORD_SIZE; c+) i (irst = 2) i (bordrc = 2) chessmn0.pintIcon(jLbel1, jLbel1.getGrphics(), r * 36 + 20, c * 36 + 30); else i (bordrc = 1) chessmn1.pintIcon(jLbel1, jLbel1.getGrphics(), r * 36 + 20, c * 36 + 30); else i (irst = 1) i (bordrc = 2) chessmn1.pintIcon(jLbel1, jLbel1.get

27、Grphics(), r * 36 + 20, c * 36 + 30); else i (bordrc = 1) chessmn0.pintIcon(jLbel1, jLbel1.getGrphics(), r * 36 + 20, c * 36 + 30); boolen lg = lse; /判断是否可以悔棋 int irst = 0; int turn = 1; int count = 0; boolen gmeStrted = lse; ImgeIcon chessmn; sttic int BORD_SIZE = 15; sttic int bord = new intBORD_S

28、IZEBORD_SIZE; / Vribles declrtion - do not modiy/GEN-BEGIN:vribles privte jvx.swing.ButtonGroup buttonGroup; privte jvx.swing.JButton jButton1; privte jvx.swing.JButton jButton2; privte jvx.swing.JButton jButton3; privte jvx.swing.JLbel jLbel1; privte jvx.swing.JPnel jPnel1; privte jvx.swing.JRdioBu

29、tton jRdioButton1; privte jvx.swing.JRdioButton jRdioButton2; privte jvx.swing.JTextield jTextield1; / End o vribles declrtion/GEN-END:vribles /胜负判断 privte boolen judgeWin(Point p) /判断是否平局 boolen dogll = true; or (int r = 0; r < 15; r+) or (int c = 0; c < 15; c+) i (bordrc = 0) dogll = lse; i

30、(dogll = true) jTextield1.setText("平局! 共" + count + "轮"); /lg表示是否可以断定赢/输 boolen lg = lse; /joinEle:将每一个横/竖/左斜/右斜行中的元素连接起来得到的一个字符串 String joinEle = "" int x = p.x; int y = p.y; /进行横行扫描 or (int i = 0; i < BORD_SIZE; i+) joinEle += bordiy; i (joinEle.contins("22222

31、2") jTextield1.setText("长连禁手!你输了 共" + count + "轮"); else i (joinEle.contins("22222") jTextield1.setText("您赢啦! 共" + count + "轮"); lg = true; return lg; else i (joinEle.contins("11111") jTextield1.setText("您输啦! 共" + count + &qu

32、ot;轮"); lg = true; return lg; /进行竖行扫描 joinEle = "" or (int j = 0; j < BORD_SIZE; j+) joinEle += bordxj; i (joinEle.contins("222222") jTextield1.setText("长连禁手!你输了 共" + count + "轮"); i (joinEle.contins("22222") jTextield1.setText("您赢啦! 共&q

33、uot; + count + "轮"); lg = true; return lg; else i (joinEle.contins("11111") jTextield1.setText("您输啦! 共" + count + "轮"); lg = true; return lg; /进行左斜行扫描 joinEle = "" i (x + y) <= 14) int j = x + y; or (int i = 0; j >= 0 && i < BORD_SIZ

34、E; i+, j-) joinEle += bordij; else int i = x + y - 14; or (int j = 14; j >= 0 && i < BORD_SIZE; i+, j-) joinEle += bordij; i (joinEle.contins("222222") jTextield1.setText("长连禁手!你输了 共" + count + "轮"); i (joinEle.contins("22222") jTextield1.setText

35、("您赢啦! 共" + count + "轮"); lg = true; return lg; else i (joinEle.contins("11111") jTextield1.setText("您输啦! 共" + count + "轮"); lg = true; return lg; /进行右斜行扫描 joinEle = "" i (y >= x) int j = y - x; or (int i = 0; j < BORD_SIZE &&

36、 i < BORD_SIZE; i+, j+) joinEle += bordij; else int i = x - y; or (int j = 0; i < BORD_SIZE && j < BORD_SIZE; i+, j+) joinEle += bordij; i (joinEle.contins("222222") jTextield1.setText("长连禁手!你输了 共" + count + "轮"); i (joinEle.contins("22222") j

37、Textield1.setText("您赢啦! 共" + count + "轮"); lg = true; return lg; else i (joinEle.contins("11111") jTextield1.setText("您输啦! 共" + count + "轮"); lg = true; /最后这个return可省略 /确保该方法有返回值如果上面条件都不满足时 return lg; 二import jv.wt.Point;import jv.util.Enumertion;imp

38、ort jv.util.Hshtble;import jv.util.Stck;/* * To chnge this templte, choose Tools | Templtes * nd open the templte in the editor. */* * * uthor dministrtor */public clss intelligence intelligence() setMyTypeTble(); setEnemyTypeTble(); void initTble() or (int r = 0; r < 15; r+) or (int c = 0; c <

39、; 15; c+) computerrc = 0; plyerrc = 0; /搜索算法 /构造棋型表,每种棋型对应的分数 /int左边空白格左边己方子数右边己方子数右边空白格 MyTble int MyTble = new int2552;/己方棋型表 int EnemyTble = new int2552;/己方棋型表/先手,后手L,冲C,活H,死S/S1=S2=S3=S4=4/C1=LC1<H1=LH1<C2=H2<C2<H2<H2<C3<C4<H3<H4 /己方放子后,即后手 void setMyTypeTble() or (int m = 0; m < 2; m+) or (int i = 0; i < 5; i+) or (int j = 0; j < 5; j+) or (int n = 0; n < 2; n+) /死子,两边都无空格且无己方子 i (m + n = 0 && i + j = 0) MyTblemij

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论