子棋源程序.doc

上传人:scccc 文档编号:13388880 上传时间:2021-12-24 格式:DOC 页数:13 大小:84KB
返回 下载 相关 举报
子棋源程序.doc_第1页
第1页 / 共13页
子棋源程序.doc_第2页
第2页 / 共13页
子棋源程序.doc_第3页
第3页 / 共13页
亲,该文档总共13页,到这儿已超出免费预览范围,如果喜欢就下载吧!
资源描述

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

1、源程序包括五个类:类 autoplay : package class AutoPlayetScreenSize().width;etScreenSize().height;();String imagePath = "" ;try imagePath = ("")+"/bin/image/" ;bgImage = (new File("", "/"); catch (IOException e) / TODO Auto-generated catch block();();/ 刷新屏幕/棋盘界

2、面public void paint(Graphics g) / 双缓冲技术防止屏幕闪烁BufferedImage bi = new BufferedImage(500, 500,JGraphics g2 = ();J/ 绘制背景 (bgImage, 1, 20, this);/输出步数情况(new Font(” 宋体",18);(”这是第 ” + step+"步! ", 60, 470); /输出先后信息(new Font("黑体",20); (message, 130, 60);/ 绘制棋盘for (int i = 0; i < 19

3、; i+) (10, 70 + 20 * i, 370, 70 + 20 * i);(10 + 20 * i, 70, 10 + 20 * i, 430);/ 标注点位(67, 127, 6, 6);(307, 127, 6, 6);(307, 367, 6, 6);(67, 367, 6, 6); (307, 247, 6, 6); (187, 127, 6, 6);(67, 247, 6, 6);(187, 367, 6, 6);(187, 247, 6, 6);/ 绘制全部棋子 (每一个位置的 )for (int i = 0; i < 19; i+) for (int j = 0;

4、 j < 19; j+) if (allChessij = 1) / 绘制黑子int tempX = i * 20 + 10;int tempY = j * 20 + 70; (tempX - 9, tempY - 9, 18, 18);if (allChessij = 2) / 绘制白子int tempX = i * 20 + 10;int tempY = j * 20 + 70;J(tempX - 9, tempY - 9, 18, 18);J(tempX - 9, tempY - 9, 18, 18); (bi, 0, 0, this);/计算棋子的精确落位public void

5、mouseClicked(MouseEvent e) int x1,y1;x1=();y1=();/在棋盘外if ()<10 | ()>370 | ()<70 | ()>430)return;/取整,让棋子落在交叉线上if (x1-10)%20>10)x1+=20;if(y1-70)%20>10)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

6、 e) public void mouseExited(MouseEvent e) /点击不同坐标的功能键public void mousePressed(MouseEvent e) "X:"+()+">"+"Y:"+();/ 点击 开始游戏 按钮if () >= 400 && () <= 470 && () >= 70&& () <= 100) int result = (this, " 是否确定开始? "); if (result =

7、 0) Game_start_csh(); intGame_Start=1;/ 点击 游戏重置 按钮if () >= 400 && () <= 470 && () >= 120&& () <= 150) Game_start_csh();/ 点击 游戏说明 按钮if () >= 400 && () <= 470 && () >= 170&& () <= 200) (this,"这个一个单机五子棋游戏程序,黑白(人机)双方轮流下棋,当某一方连到

8、 五子时,游戏结束! ");/ 点击 认输 按钮if () >= 400 && () <= 470 && () >= 270&& () <= 300) if(intGame_Start=1)int result = (this, " 是否确认认输 ?");if (result = 0) (this, " 您已经认输 ,游戏结束! "); Game_start_csh();else(this, "游戏还未开始或已经结束,不能认输,请重新开始下一局!");/

9、 点击 关于 按钮if () >= 400 && () <= 470 && () >= 320&& () <= 350) (this," 本游戏由黄洪华制作,有相关问题可以拨打进行质询! ");/ 点击 退出 按钮if () >= 400 && () <= 470 && () >= 370 && () <= 400) int result = (this, " 是否确认退出游戏? "); if(result =

10、0)(0);();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;h<5;h+) ijh=0;ijh=0;public String Get_qizi_color(int x)/ 得到棋子的颜色if (x=0)return "

11、五子连珠白棋 "elsereturn " 五子连珠黑棋 "public void set_Qizi(int x,int y) / 落子if (intGame_Start=0) / 判断游戏未开始return;if (allChessx/20y/20!=0)return;if(intGame_Start=1&&allChessx/20y/20=0)step+;Graphics g=getGraphics();if (color_Qizi=0)/ 判断黑子还是白子 :是黑子 allChessx/20y/20=color_Qizi+1; color_Qi

12、zi=1;Judge(x/20,y/20);if(color_Qizi=1 && step>2)(allChess,1);(allChess,2);/不要会出现乱走 -> 实现人与电脑有次序的下棋if!=0|!=0)if> && 4>3)(*20+10)-9,(*20+70)-9, 18, 18);/ 防守 allChess=color_Qizi+1; color_Qizi=0;Judge,;for(int i=0;i<=18;i+)for(int j=0;j<=18;j+)for(int h=0;h<5;h+)ijh=

13、0;ijh=0;else(*20+10)-9,(*20+70)-9,18,18);/ 进攻 allChess=color_Qizi+1; color_Qizi=0;Judge,;for(int i=0;i<=18;i+)/0-(-1)for(int j=0;j<=18;j+)for(int h=0;h<5;h+)ijh=0;ijh=0;else(allChess,x/20,y/20); /随即选择一个方向下棋(*20+10)-9,(*20+70)-9,18,18);allChess=color_Qizi+1;color_Qizi=0;Judge,;if(color_Qizi=

14、1 && step<=2)if(allChess99=0) (190-9, 250-9, 18, 18); allChess99=color_Qizi+1; color_Qizi=0;else if(allChess88=0) (170-9, 230-9, 18, 18); allChess88=color_Qizi+1; color_Qizi=0;else if(allChess89=0) (170-9, 250-9, 18, 18); allChess89=color_Qizi+1; color_Qizi=0;else if(allChess98=0) (190-9,

15、 230-9, 18, 18); allChess98=color_Qizi+1; color_Qizi=0;private void Judge(int a, int b) / 判断总的输赢if (Game_win_1(a,b) / 判断输赢(this, " 游戏结束 ,"+ Get_qizi_color(color_Qizi) + " 获胜! "); intGame_Start=0;if (Game_win_2(a,b) / 判断输赢(this, " 游戏结束 ,"+ Get_qizi_color(color_Qizi) + &q

16、uot; 获胜! "); intGame_Start=0;if (Game_win_3(a,b) / 判断输赢(this, " 游戏结束 ,"+ Get_qizi_color(color_Qizi) + " 获胜! "); intGame_Start=0;if (Game_win_4(a,b) / 判断输赢(this, " 游戏结束 ,"+ Get_qizi_color(color_Qizi) + " 获胜! "); intGame_Start=0;();public boolean Game_win_1

17、(int x,int y) / 判断输赢 横int x1,y1,t=1;x1=x;y1=y;for (int i=1;i<5 ;i+ )if (x1>18)break;if (x+i<=18 && allChessx1+iy1=allChessxy)t+=1;elsebreak;for (int i=1;i<5 ;i+ )if (x1<0) break;if(x-i>=0 && allChessx1-iy1=allChessxy) t+=1;else break;if (t>4)return true;elseretur

18、n false;public boolean Game_win_2(int x,int y) / 判断输赢 竖 int x1,y1,t=1;x1=x;y1=y;for (int i=1;i<5 ;i+ )if (x1>18) break;if (y+i<=18 && allChessx1y1+i=allChessxy ) t+=1;else break;for (int i=1;i<5 ;i+ )if (x1<0) break;if(y-i>=0 && allChessx1y1-i=allChessxy) t+=1;else

19、break;if (t>4)return true;elsereturn false;public boolean Game_win_3(int x,int y) / 判断输赢 左斜 int x1,y1,t=1;x1=x;y1=y;for (int i=1;i<5 ;i+ ) if (x1>18)break;if (x+i<=18 && y-i>0&& allChessx1+iy1-i=allChessxy) t+=1;elsebreak;for (int i=1;i<5 ;i+ )if (x1<=0) break;if

20、(x-i>0 && y+i <=18 && allChessx1-iy1+i=allChessxy) t+=1;elsebreak;if (t>4)return true;elsereturn false;public boolean Game_win_4(int x,int y) / 判断输赢 右斜int x1,y1,t=1;x1=x;y1=y;for (int i=1;i<5 ;i+ )if (x1>18)break;if (x+i<=18 && y+i<=18 && allChess

21、x1+iy1+i=allChessxy) t+=1;elsebreak;for (int i=1;i<5 ;i+ )if (x1<=0)break;if(x-i>0 && y-i>0 && allChessx1-iy1-i=allChessxy)t+=1;elsebreak;if (t>4)return true;elsereturn false;类 scan:package class Scanint shape=new int19195;void scan(int chesspad,int colour)/ 查看八方向上相邻同色

22、棋子个数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 &am

23、p;& 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)/左上s

24、hapeij3+;m=i;n=j;while(m+1<=18 && n+1<=18 && chesspad+m+n=colour)/ 右 下shapeij3+;类 sort :package public class Sort / 实现人与电脑有次序的下棋public void sort(int shape)int temp;for(int i=0;i<=18;i+)for(int j=0;j<=18;j+)for(int h=1;h<=4;h+)for(int w=3;w>=h;w-)if(shapeijw-1<shapeijw) /如果前者小于后者,则交换 temp=shapeijw-1; shapeijw-1=shapeijw; shapeijw=temp;

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

当前位置:首页 > 社会民生


经营许可证编号:宁ICP备18001539号-1