java编写的简单五子棋

上传人:小** 文档编号:55617858 上传时间:2018-10-03 格式:DOC 页数:5 大小:43KB
返回 下载 相关 举报
java编写的简单五子棋_第1页
第1页 / 共5页
java编写的简单五子棋_第2页
第2页 / 共5页
java编写的简单五子棋_第3页
第3页 / 共5页
java编写的简单五子棋_第4页
第4页 / 共5页
java编写的简单五子棋_第5页
第5页 / 共5页
亲,该文档总共5页,全部预览完了,如果喜欢就下载吧!
资源描述

《java编写的简单五子棋》由会员分享,可在线阅读,更多相关《java编写的简单五子棋(5页珍藏版)》请在金锄头文库上搜索。

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 MouseListener public static void main(String args) wuziqi d=new wuziqi(); Vector v=new Vector(); Vec

2、tor 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)

3、;/关闭按钮 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); /垂直线的

4、长度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;i=4|cv=4|ci1=4|ci2=4) System.out.println(v.size()+“步棋

5、“); if(v.size()%2=0) /判断偶数是黑棋胜利,奇数白棋胜利 JOptionPane.showMessageDialog(null,“恭喜你 黑棋赢了“); else JOptionPane.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

6、(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 else v.remove(v.lastElement(); this.repaint(); else whitecount+; if(whitecount3) JOptionPane.showMessageDialog(this, “白棋已经悔了三步“); else v.remove(v.lastElement(); th

7、is.repaint(); public void mouseEntered(MouseEvent e) public void mouseExited(MouseEvent e) public void mousePressed(MouseEvent e) public void mouseReleased(MouseEvent e) 二、简要说明 该程序为一简易五子棋,棋盘为 15*15,白子先落。可以悔棋,点击鼠标右键悔棋, 最多悔 3 步。在每次下子前,先判断该处有无棋子,有则不能落子,超出边界不能落子。 下子时白子先落,判断胜利为偶数是黑棋胜利,奇数白棋胜利。任何一方有达到横、竖、 撇、捺连到 5 子则胜利。

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

当前位置:首页 > 商业/管理/HR > 管理学资料

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号