免费预览已结束,剩余1页可下载查看
下载本文档
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
Java程序设计五子棋一、源码import java.awt.*;import java.awt.event.MouseListener;import java.awt.event.MouseEvent;import java.util.Vector;import javax.swing.*;public class wuziqi extends JFrame implements MouseListenerpublic static void main(String args)wuziqi d=new wuziqi();Vector v=new Vector();Vector white=new Vector();Vector black=new Vector();JButton btnstart =new JButton(开始);JButton btnstop =new JButton(停止);JToolBar tool=new JToolBar();boolean b; /用来判断 白棋还是黑棋 int blackcount,whitecount; /计算悔棋 /* * 构造方法 */public wuziqi()super(五子棋);this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);/关闭按钮Container con=this.getContentPane();this.addMouseListener(this);/添加监听tool.add(btnstart);/添加按钮tool.add(btnstop);this.setSize(550,500);/设置窗体大小this.setVisible(true); int w=20; /间距大小 是双数 int px=100,py=100; /棋盘的坐标 int pxw=(px+w), pyw=(py+w); int width=w*16,height=w*16; int vline=(width+px); /垂直线的长度 int hline=(height+py); /水平线的长度 /* * 画棋盘 */public void paint(Graphics g)g.clearRect(0, 0, this.getWidth(), this.getHeight(); /清除面板g.setColor(Color.BLACK); /设置网格颜色g.drawRect(px, py, width, height); /网格大小g.drawString(简易五子棋, 110, 70);for(int i=0;i15;i+)g.drawLine(pxw+i*w,py,pxw+i*w,hline);/每条横线和竖线g.drawLine(px,pyw+i*w,vline,pyw+i*w);for(int x=0;xv.size();x+)String str=(String)v.get(x);String tmp=str.split(-);int a=Integer.parseInt(tmp0);int b=Integer.parseInt(tmp1);a=a*w+px;b=b*w+py;if(x%2=0)g.setColor(Color.CYAN);elseg.setColor(Color.BLACK);g.fillArc(a-w/2, b-w/2, w, w,0,360);public void updeta(Graphics g)this.paint(g);public void victory(int x,int y,Vector contain) /判断胜利的方法int cv=0; /计算垂直的变量int ch=0; /计算水平的变量int ci1=0; /计算斜面的变量1int ci2=0; /计算斜面的变量2for(int i=1;i5;i+)if(contain.contains(x+i)+-+y)ch+;elsebreak;System.out.println(前面执行了+ch+次);for(int i=1;i5;i+)if(contain.contains(x-i)+-+y)ch+;elsebreak;System.out.println(后面执行了+ch+次);for(int i=1;i5;i+)if(contain.contains(x+-+(y+i)cv+;elsebreak;for(int i=1;i5;i+)if(contain.contains(x+-+(y-i)cv+;elsebreak;for(int i=1;i5;i+)if(contain.contains(x+i)+-+(y+i)ci1+;elsebreak;for(int i=1;i5;i+)if(contain.contains(x-i)+-+(y-i)ci1+;elsebreak;for(int i=1;i5;i+)if(contain.contains(x-i)+-+(y+i)ci2+;elsebreak;for(int i=1;i=4|cv=4|ci1=4|ci2=4)System.out.println(v.size()+步棋);if(v.size()%2=0) /判断偶数是黑棋胜利,奇数白棋胜利JOptionPane.showMessageDialog(null,恭喜你 黑棋赢了);elseJOptionPane.showMessageDialog(null,恭喜你 白棋赢了);this.v.clear();this.black.clear();this.white.clear();this.repaint();System.out.println(ch+ +cv+ +ci1+ +ci2);public void mouseClicked(MouseEvent e) if(e.getButton()=e.BUTTON1)int x=e.getX();int y=e.getY();x=(x-x%w)+(x%ww/2?w:0);y=(y-y%w)+(y%ww/2?w:0);x=(x-px)/w;y=(y-py)/w;if(x=0&y=0&x=16&y3)JOptionPane.showMessageDialog(this, 黑棋已经悔了三步);elsev.remove(v.lastElement();this.repaint();elsewhitecount+;if(whitecount3)JOptionPane.showMessageDialog(this, 白棋已经悔了三步);elsev.remove(v.lastElement();this.repaint();public void mouseEntered(MouseEvent e) public void mouseExited(MouseEvent e) public void mousePressed(MouseEvent e) public void mouseR
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- Unit5 Wild animals 单元模拟卷-2025-2026学年译林版八年级英语上册(解析卷)
- 2026年工程项目成本管理协议
- 上海市宝山中学2025年化学高一上期中经典试题含解析
- 上海实验学校2026届高二生物第一学期期末联考试题含解析
- 上海兴伟学院《建筑设备(暖)》2024-2025学年第一学期期末试卷
- 重庆市合川区2025-2026学年化学高一上期中统考试题含解析
- 陕西省育才中学2025年高一物理第一学期期末监测模拟试题含解析
- 四川省南充市示范名校2026届化学高二第一学期期末复习检测试题含解析
- 新疆乌鲁木齐市第四中学2025-2026学年高一物理第一学期期末教学质量检测试题含解析
- 叙事护理:慢性病管理中的叙事支持
- 2025年官方消防考试试题及答案
- 农民财务与税收知识培训课件
- 行政领导学-形考任务二-国开-参考资料
- 椎管造影术课件
- 水利项目部安全培训记录课件
- 中药农业课题申报书模板
- 美白祛斑课件
- GB/T 46015-2025适老家具设计指南
- 仁爱科普版(2024)八年级上册英语单词表 背诵版
- 【《基于S71200PLC的物料自动配比系统设计》11000字】
- 手术室一科一品成果汇报
评论
0/150
提交评论