五子棋源程序.docx

上传人:牧羊曲112 文档编号:3236845 上传时间:2023-03-12 格式:DOCX 页数:16 大小:41.55KB
返回 下载 相关 举报
五子棋源程序.docx_第1页
第1页 / 共16页
五子棋源程序.docx_第2页
第2页 / 共16页
五子棋源程序.docx_第3页
第3页 / 共16页
五子棋源程序.docx_第4页
第4页 / 共16页
五子棋源程序.docx_第5页
第5页 / 共16页
亲,该文档总共16页,到这儿已超出免费预览范围,如果喜欢就下载吧!
资源描述

《五子棋源程序.docx》由会员分享,可在线阅读,更多相关《五子棋源程序.docx(16页珍藏版)》请在三一办公上搜索。

1、五子棋源程序源程序包括五个类: 类autoplay: package com.game.frame; public class AutoPlay/实现电脑可以自动下棋 int x,y;/X统计玩家的连起来的棋子数 /Y电脑玩家的连起来的棋子数 public void autoPlay(int chesspad,int a,int b) int randomNumber=(int)(Math.random*8)+1;/随即选择一个方向 switch(randomNumber)/通过a,b的值 执行不同的运算,最后返回不同的 /值。达到执行不同的操作 case(1):/左上 if(a-1=0 &

2、b-1=0 & chesspada-1b-1=0) x=a-1;y=b-1;break; else if(a-2=0 & b-2=0 & chesspada-2b-2=0) x=a-2;y=b-2;break; else case(2):/向左 if(a-1=0 & chesspada-1b=0) x=a-1;y=b;break; else if(a-2=0 & chesspada-2b=0) x=a-2;y=b;break; else case(3):/左下 if(a-1=0 & b+1=0 & b+2=18 & chesspada-2b+2=0) x=a-2;y=b+2;break; el

3、se case(4):/向下 if(b+1=18 & chesspadab+1=0) x=a;y=b+1;break; else if( b+2=18 & chesspadab+2=0 ) x=a;y=b+2;break; else case(5):/右下 if(a+1=18 & b+1=18 & chesspada+1b+1=0 ) x=a+1;y=b+1;break; else if(a+2=18 & b+2=18 & chesspada+2b+2=0) x=a+2;y=b+2;break; else case(6):/向右 if(a+1=18 & chesspada+1b=0) x=a+

4、1;y=b;break; else if(a+2=18 & chesspada+2b=0) x=a+2;y=b;break; else case(7):/右上 if( a+1=0 & chesspada+1b-1=0) x=a+1;y=b-1;break; else if( a+2=0 & chesspada+2b-2=0) x=a+2;y=b-2;break; else case(8):/向上 if(b-1=0 & chesspadab-1=0) x=a;y=b-1;break; else if(b-2=0 & chesspadab-2=0) x=a;y=b-2;break; else 类e

5、valuate: package com.game.frame; public class Evaluate/电脑估算估值点的坐标 int max_x,max_y,max; public void evaluate(int shape) int i=0,j=0; for(i=0;i=18;i+) for(j=0;j=18;j+) switch(shapeij0) /电脑根据传进来的三维数组决定该怎么行棋 case 5: shapeij4=200; break; case 4: switch(shapeij1) case 4: shapeij4=150+shapeij2+shapeij3; br

6、eak; case 3: shapeij4=100+shapeij2+shapeij3; break; default: shapeij4=50+shapeij2+shapeij3; break; case 3: switch(shapeij1) case 3: shapeij4=75+shapeij2+shapeij3; break; default: shapeij4=20+shapeij2+shapeij3; break; case 2: shapeij4=10+shapeij1+shapeij2+shapeij3; break; case 1: shapeij4=shapeij0+sh

7、apeij1+shapeij2+shapeij3; default : shapeij4=0; int x=0,y=0; max=0; for(x=0;x=18;x+) for(y=0;y=18;y+) if(maxshapexy4) /周围有同色棋子 max=shapexy4; max_x=x;max_y=y;/获得该点位置 主类fivechess: package com.game.frame; import java.awt.Color; import java.awt.Font; import java.awt.Graphics; import java.awt.Toolkit; im

8、port java.awt.event.MouseEvent; import java.awt.event.MouseListener; import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; import javax.imageio.ImageIO; import javax.swing.JFrame; import javax.swing.JOptionPane; import com.game.frame.AutoPlay; import com.game.frame.Ev

9、aluate; import com.game.frame.Scan; import com.game.frame.Sort; public class FiveChess extends JFrame implements MouseListener / 取得屏幕的宽度 int width = Toolkit.getDefaultToolkit.getScreenSize.width; / 取得屏幕的高度 int height = Toolkit.getDefaultToolkit.getScreenSize.height; / 背景图片 BufferedImage bgImage = nu

10、ll; / 保存之前下过的全部棋子的坐标 / 其中数据内容 0: 表示这个点并没有棋子, 1: 表示这个点是黑子,个点是白子 int allChess = new int1919; / 标识当前应该黑棋还是白棋下下一步 0:表示黑子,1:表示白子 int color_Qizi=0; String message=玩家执黑子先下; /游戏开始标志 0:未开始 1:游戏中 int intGame_Start=0; /判定棋子步数 int step=0; /p表示人,c表示电脑 Scan scanp=new Scan;/person Scan scanc=new Scan;/computer Aut

11、oPlay autoPlay=new AutoPlay; Evaluate evaluatep=new Evaluate; Evaluate evaluatec=new Evaluate; 2:表示这 Sort sort=new Sort; public static void main(String args) new FiveChess; public FiveChess Game_start_csh; /设置窗体标题 this.setTitle(黄洪华的五子棋 V1.0); / 设置窗体大小 this.setSize(500, 500); / 设置窗体出现位置 this.setLocat

12、ion(width - 500) / 2, (height - 500) / 2); / 将窗体设置为大小不可改变 this.setResizable(false); / 将窗体的关闭方式设置为默认关闭后程序结束 this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); / 为窗体加入监听器 this.addMouseListener(this); / 将窗体显示出来 this.setVisible(true); / 刷新屏幕,防止开始游戏时出现无法显示的情况. this.repaint; String imagePath = ; try ima

13、gePath = System.getProperty(user.dir)+/bin/image/background.jpg ; bgImage = ImageIO.read(new File(imagePath.replaceAll(, /); catch (IOException e) / TODO Auto-generated catch block e.printStackTrace; this.repaint;/ 刷新屏幕 /棋盘界面 public void paint(Graphics g) / 双缓冲技术防止屏幕闪烁 BufferedImage bi = new Buffere

14、dImage(500, 500, BufferedImage.TYPE_INT_RGB); Graphics g2 = bi.createGraphics; g2.setColor(Color.BLACK); / 绘制背景 g2.drawImage(bgImage, 1, 20, this); /输出步数情况 g2.setFont(new Font(宋体, Font.BOLD, 18); g2.drawString(这是第 + step+步!, 60, 470); /输出先后信息 g2.setFont(new Font(黑体, Font.BOLD, 20); g2.drawString(mes

15、sage, 130, 60); / 绘制棋盘 for (int i = 0; i 19; i+) g2.drawLine(10, 70 + 20 * i, 370, 70 + 20 * i); g2.drawLine(10 + 20 * i, 70, 10 + 20 * i, 430); / 标注点位 g2.fillOval(67, 127, 6, 6); g2.fillOval(307, 127, 6, 6); g2.fillOval(307, 367, 6, 6); g2.fillOval(67, 367, 6, 6); g2.fillOval(307, 247, 6, 6); g2.fi

16、llOval(187, 127, 6, 6); g2.fillOval(67, 247, 6, 6); g2.fillOval(187, 367, 6, 6); g2.fillOval(187, 247, 6, 6); / 绘制全部棋子(每一个位置的) for (int i = 0; i 19; i+) for (int j = 0; j 19; j+) if (allChessij = 1) / 绘制黑子 int tempX = i * 20 + 10; int tempY = j * 20 + 70; g2.fillOval(tempX - 9, tempY - 9, 18, 18); i

17、f (allChessij = 2) / 绘制白子 int tempX = i * 20 + 10; int tempY = j * 20 + 70; g2.setColor(Color.WHITE); g2.fillOval(tempX - 9, tempY - 9, 18, 18); g2.setColor(Color.BLACK); g2.drawOval(tempX - 9, tempY - 9, 18, 18); g.drawImage(bi, 0, 0, this); /计算棋子的精确落位 public void mouseClicked(MouseEvent e) int x1,

18、y1; x1=e.getX; y1=e.getY; /在棋盘外 if (e.getX370 | e.getY430) return; /取整,让棋子落在交叉线上 if (x1-10)%2010) x1+=20; if(y1-70)%2010) y1+=20; x1=(x1-10)/20*20; y1=(y1-70)/20*20; set_Qizi(x1,y1); public void mouseEntered(MouseEvent e) public void mouseReleased(MouseEvent e) public void mouseExited(MouseEvent e)

19、/点击不同坐标的功能键 public void mousePressed(MouseEvent e) System.out.println(X:+e.getX+-+Y:+e.getY); / 点击 开始游戏 按钮 if (e.getX = 400 & e.getX = 70 & e.getY = 400 & e.getX = 120 & e.getY = 400 & e.getX = 170 & e.getY = 400 & e.getX = 270 & e.getY = 400 & e.getX = 320 & e.getY = 400 & e.getX = 370 & e.getY = 4

20、00) int result = JOptionPane.showConfirmDialog(this, 是否确认退出游戏?); if(result = 0) System.exit(0); this.repaint; public void Game_start_csh /游戏开始初始化 intGame_Start=0; step=0; color_Qizi=0; for (int i=0;i=18 ;i+ ) for (int j=0;j=18 ;j+ ) allChessij=0; for(int i=0;i=18;i+) for(int j=0;j=18;j+) for(int h=0

21、;h2) scanp.scan(allChess,1); scanc.scan(allChess,2); sort.sort(scanp.shape);/不要会出现乱走-实现人与电脑有次序的下棋 sort.sort(scanc.shape); evaluatep.evaluate(scanp.shape); evaluatec.evaluate(scanc.shape); if(evaluatec.max!=0|evaluatep.max!=0) if(evaluatep.maxevaluatec.max scanp.shapeevaluatep.max_xevaluatep.max_y43)

22、 & g.fillOval(evaluatep.max_x)*20+10)-9,(evaluatep.max_y)*20+70)-9, 18, 18);/防守 allChessevaluatep.max_xevaluatep.max_y=color_Qizi+1; color_Qizi=0; Judge(evaluatep.max_x,evaluatep.max_y); for(int i=0;i=18;i+) for(int j=0;j=18;j+) for(int h=0;h5;h+) scanp.shapeijh=0; scanc.shapeijh=0; else g.fillOval(

23、evaluatec.max_x)*20+10)-9,(evaluatec.max_y)*20+70)-9,18,18);/进攻 allChessevaluatec.max_xevaluatec.max_y=color_Qizi+1; color_Qizi=0; Judge(evaluatec.max_x,evaluatec.max_y); for(int i=0;i=18;i+)/0-(-1) for(int j=0;j=18;j+) for(int h=0;h5;h+) scanp.shapeijh=0; scanc.shapeijh=0; else autoPlay.autoPlay(al

24、lChess,x/20,y/20); /随即选择一个方向下棋 g.fillOval(autoPlay.x)*20+10)-9,(autoPlay.y)*20+70)-9,18,18); allChessautoPlay.xautoPlay.y=color_Qizi+1; color_Qizi=0; Judge(autoPlay.x,autoPlay.y); if(color_Qizi=1 & step=2) if(allChess99=0) g.fillOval(190-9, 250-9, 18, 18); allChess99=color_Qizi+1; color_Qizi=0; else

25、 if(allChess88=0) g.fillOval(170-9, 230-9, 18, 18); allChess88=color_Qizi+1; color_Qizi=0; else if(allChess89=0) g.fillOval(170-9, 250-9, 18, 18); allChess89=color_Qizi+1; color_Qizi=0; else if(allChess98=0) g.fillOval(190-9, 230-9, 18, 18); allChess98=color_Qizi+1; color_Qizi=0; private void Judge(

26、int a, int b) /判断总的输赢 if (Game_win_1(a,b) /判断输赢 JOptionPane.showMessageDialog(this, 游戏结束, + Get_qizi_color(color_Qizi) + 获胜!); intGame_Start=0; if (Game_win_2(a,b) /判断输赢 JOptionPane.showMessageDialog(this, 游戏结束, + Get_qizi_color(color_Qizi) + 获胜!); intGame_Start=0; if (Game_win_3(a,b) /判断输赢 JOptionP

27、ane.showMessageDialog(this, 游戏结束, + Get_qizi_color(color_Qizi) + 获胜!); intGame_Start=0; if (Game_win_4(a,b) /判断输赢 JOptionPane.showMessageDialog(this, 游戏结束, + Get_qizi_color(color_Qizi) + 获胜!); intGame_Start=0; this.repaint; public boolean Game_win_1(int x,int y) /判断输赢 横 int x1,y1,t=1; x1=x; y1=y; fo

28、r (int i=1;i18) break; if (x+i=18 & allChessx1+iy1=allChessxy) t+=1; else break; for (int i=1;i5 ;i+ ) if (x1=0 & allChessx1-iy1=allChessxy) t+=1; else break; if (t4) return true; else return false; public boolean Game_win_2(int x,int y) /判断输赢 竖 int x1,y1,t=1; x1=x; y1=y; for (int i=1;i18) break; if

29、 (y+i=18 & allChessx1y1+i=allChessxy ) t+=1; else break; for (int i=1;i5 ;i+ ) if (x1=0 & allChessx1y1-i=allChessxy) t+=1; else break; if (t4) return true; else return false; public boolean Game_win_3(int x,int y) /判断输赢 左斜 int x1,y1,t=1; x1=x; y1=y; for (int i=1;i18) break; if (x+i0& allChessx1+iy1-i=allChessxy) t+=1; else break; for (int i=1;i5 ;i+ ) if (x10 & y+i 4) return true; else return false

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 生活休闲 > 在线阅读


备案号:宁ICP备20000045号-2

经营许可证:宁B2-20210002

宁公网安备 64010402000987号