Java五子棋全代码.doc

上传人:豆**** 文档编号:23966881 上传时间:2022-07-03 格式:DOC 页数:60 大小:198.50KB
返回 下载 相关 举报
Java五子棋全代码.doc_第1页
第1页 / 共60页
Java五子棋全代码.doc_第2页
第2页 / 共60页
点击查看更多>>
资源描述

《Java五子棋全代码.doc》由会员分享,可在线阅读,更多相关《Java五子棋全代码.doc(60页珍藏版)》请在得力文库 - 分享文档赚钱的网站上搜索。

1、Four short words sum up what has lifted most successful individuals above the crowd: a little bit more.-author-dateJava五子棋全代码Java五子棋全代码作者:商洛学院-杨小军子类代码package org.wuziqi;import java.awt.Color;import java.awt.Font;import java.awt.Graphics;import java.awt.Toolkit;import java.awt.event.MouseEvent;import

2、 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;public class FiveChessFrame extends JFrame implements MouseListener,Runnable int width=Toolkit.getDef

3、aultToolkit().getScreenSize().width; /取得屏幕的宽度 int height=Toolkit.getDefaultToolkit().getScreenSize().height; /取得屏幕的高度 BufferedImage bgimage=null; /保存棋子坐标 int x = 0; int y = 0; /保存之前下过的全部棋子的坐标 /其中的数据内容为0:此点为无子;1为黑子,2为白子 int allChess=new int1920; /标识当前应该是黑棋还是白棋下棋 boolean isBlack=true; /标识当前游戏是否可以继续 bo

4、olean canPlay=true; /保存显示提示信息 String message =黑方先行; /倒计时,保存最大时间值(秒) int maxTime=0; /做倒计时的线程类 Thread t=new Thread(this); /保存黑方与白方的剩余时间 int blackTime=0; int whiteTime=0; / String blackMessage=无限制; String whiteMessage=无限制; public FiveChessFrame() this.setTitle(我的五子棋游戏作者:杨小军); this.setSize(500,500); thi

5、s.setLocation(width-500)/2,(height-500)/2); this.setResizable(false); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); /为窗体加入监听器 this.addMouseListener(this); this.setVisible(true); t.start(); t.suspend(); /刷新屏幕防止开始游戏时出现屏幕加载延时过长 this.repaint(); try bgimage = ImageIO.read(new File(D:/wuziqi.jpg);

6、catch (IOException e) / TODO Auto-generated catch block e.printStackTrace(); public void paint(Graphics g) /双缓冲技术防止屏幕闪烁 BufferedImage bi= new BufferedImage(500,500,BufferedImage.TYPE_INT_ARGB); Graphics g2=bi.createGraphics(); g2.drawImage(bgimage, 0, 20,this); g2.setFont(new Font(宋体,Font.BOLD,20);

7、g2.setColor(Color.RED); g2.drawString(木易小舟, 10, 60); g2.setColor(Color.BLACK); g2.drawString(游戏信息:+message, 100, 60); g2.setFont(new Font(黑体,0,14); g2.drawString(黑方时间:+blackMessage, 30, 475); g2.drawString(白方时间:+whiteMessage, 260, 475); /绘制棋盘 for(int i=0;i19;i+) g2.drawLine(10,70+20*i,370,70+20*i);

8、g2.drawLine(10+20*i,70,10+20*i,430); g2.fillOval(69,128,4,4); g2.fillOval(308,128,4,4); g2.fillOval(68,388,4,4); g2.fillOval(308,388,4,4); g2.fillOval(189,248,4,4); g2.fillOval(189,128,4,4); g2.fillOval(189,388,4,4); g2.fillOval(308,248,4,4); g2.fillOval(68,248,4,4); /g.fillOval(x,y,10,10); for(int

9、i=0;i19;i+) for(int j=0;j=10&x=75&y=408&e.getX()=70&e.getY()=97) /点击开始游戏按钮 int result=JOptionPane.showConfirmDialog(this, 是否重新开始游戏?); if(result=0) /现在从新开始游戏 /1,将棋盘清空/allChessij全部数据归0 /2,将游戏信息显示到开始位置 /3,将下一步下棋的人改为黑方 for(int i=0;i19;i+) for(int j=0;j0) blackMessage=maxTime/3600+:+ (maxTime/60-maxTime/

10、3600*60)+: +(maxTime-maxTime/60*60); whiteMessage=maxTime/3600+:+ (maxTime/60-maxTime/3600*60)+: +(maxTime-maxTime/60*60); t.resume(); else blackMessage=无限制; whiteMessage=无限制; this.canPlay=true; this.repaint();/重新刷新一下页面 if(e.getX()=406&e.getX()=122&e.getY()=149) /点击游戏设置按钮 String input= JOptionPane.s

11、howInputDialog(请输入游戏的最大时间(单位:分钟),如果输入0表示没有时间限制); try maxTime=Integer.parseInt(input)*60; if(maxTime0) JOptionPane.showMessageDialog(this, 请输入正确信息,不充许输入负数); if(maxTime=0) int reslut=JOptionPane.showConfirmDialog(this, 设置完成,是否开始新的游戏); for(int i=0;i19;i+) for(int j=0;j0) int reslut=JOptionPane.showConf

12、irmDialog(this, 设置完成,是否开始新的游戏); for(int i=0;i19;i+) for(int j=0;j=408&e.getX()=174&e.getY()=408&e.getX()=275&e.getY()=408&e.getX()=327&e.getY()=408&e.getX()=379&e.getY()=5) flag=true; / int i2=1; int count2=1; while(color=allChessxy+i2) count2+; i2+; i2=1; while(color=allChessxy-i2) count2+; i2+; if

13、(count2=5) flag=true; /斜方向判断(右上方+左下方) int i3=1; int count3=1; while(color=allChessx+i3y-i3) count3+; i3+; i3=1; while(color=allChessx-i3y+i3) count3+; i3+; if(count3=5) flag=true; /左上方+右下方判断 int i4=1; int count4=1; while(color=allChessx+i4y+i4) count4+; i4+; i4=1; while(color=allChessx-i4y-i4) count

14、4+; i4+; if(count4=5) flag=true; */ count=this.checkCount(1,0,color); if(count=5) flag=true; else count=this.checkCount(0,1,color); if(count=5) flag=true; else count=this.checkCount(1,-1,color); if(count=5) flag=true; else count=this.checkCount(1,1,color); if(count=5) flag=true; return flag; / priva

15、te int checkCount(int xChange,int yChange,int color) int count=1; int tempX=xChange; int tempY=yChange; while(x+xChange=0&x+xChange=0 &y+yChange0) yChange+; else yChange-; xChange=tempX; yChange=tempY; while(x-xChange=0&x-xChange=0 &y-yChange0) yChange+; else yChange-; return count; Override public void run() / TODO Auto-generated method stub if(maxTime0) while(true) if(isBlack) blackTime-; if(blackTime=0) JOptionPane.showMessageDialog(this, 黑方超时,游戏结束); else whiteTime-; if(whiteTime=0) JOptionPane.showMessageDialog(this, 白方超时,游戏结束); blackMessage=b

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 教育专区 > 小学资料

本站为文档C TO C交易模式,本站只提供存储空间、用户上传的文档直接被用户下载,本站只是中间服务平台,本站所有文档下载所得的收益归上传人(含作者)所有。本站仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。若文档所含内容侵犯了您的版权或隐私,请立即通知得利文库网,我们立即给予删除!客服QQ:136780468 微信:18945177775 电话:18904686070

工信部备案号:黑ICP备15003705号-8 |  经营许可证:黑B2-20190332号 |   黑公网安备:91230400333293403D

© 2020-2023 www.deliwenku.com 得利文库. All Rights Reserved 黑龙江转换宝科技有限公司 

黑龙江省互联网违法和不良信息举报
举报电话:0468-3380021 邮箱:hgswwxb@163.com