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=
2、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 co
3、n=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);
4、/水平线的长度/* 画棋盘*/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()+“步棋“);if(v.size()%2=0) /判断偶数是黑棋胜利,
5、奇数白棋胜利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();i
6、nt 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=0elsev.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 mouseReleased(MouseEvent e) 二、简要说明该程序为一简易五子棋,棋盘为 15*15,白子先落。可以悔棋,点击鼠标右键悔棋,最多悔 3 步。在每次下子前,先判断该处有无棋子,有则不能落子,超出边界不能落子。下子时白子先落,判断胜利为偶数是黑棋胜利,奇数白棋胜利。任何一方有达到横、竖、撇、捺连到 5 子则胜利。