import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
class Grid // 格子类
{
private int x,y;//x,y坐标
private Polygon po ;
static public final int he = 24;//格子大小
public boolean flag = false;//是否刷新的标志
private int had ;
public Grid(int x,int y,int had)
{
this.x = x;
this.y = y;
this.had = had;
po = new Polygon();
po.addPoint(x,y);
po.addPoint(x+he,y);
po.addPoint(x,y +he);
po.addPoint(x + he,y + he);
}
public int getX()
{
return x;
}
public int getY()
{
return y;
}
public Polygon getPo()
{
return po;
}
public int getHad()
{
return had;
}
public void setHad(int had)
{
this.had = had;
}
}
class MainFrame extends JFrame implements MouseListener
{
Grid [][] pan = new Grid[19][19];//19x19的盘
boolean alt = false;//标志轮流
int startX = 30,startY = 54;//起始点30,30
ArrayList al = new ArrayList();
Grid last;
public MainFrame()
{
for(int i =0; i < 19;i++)
for(int j = 0;j < 19;j++)
pan[i][j] = new Grid(i *Grid.he + startX, j *Grid.he +startY,0);
this.setSize(516,542);
this.addMouseListener(this);
this.setResizable(false);
this.setVisible(true);
}
private boolean isInside(int x,int y,Grid gr)
{
if(x > gr.getX() && x <gr.getX() + Grid.he)
if(y> gr.getY() && y< gr.getY() + Grid.he)
return true;
return false;
}
public void mouseClicked(MouseEvent e)
{
int xPoint,yPoint;
xPoint = e.getX();
yPoint = e.getY();
for(int i = 0 ;i < 19 ;i++)
for(int j = 0 ;j < 19; j++)
{
if(pan[i][j].getHad() == 0 && isInside(xPoint,yPoint,pan[i][j]) )//在范围内而且没有子
{
pan[i][j].flag = true;
if(alt == false )
{
pan[i][j].setHad(1);
alt = true;
}
else
{
pan[i][j].setHad(2);
alt = false;
}
al.add(pan[i][j]);
last = pan[i][j];
repaint();
if(isWin(i,j))
{
JOptionPane.showMessageDialog(null,"win");
System.exit(1);
}
return;
}
}
}
private boolean isWin(int i,int j)//判断是否胜利
{
int t = last.getHad();
int count = 0;
int m,n;
for(m = i,n = j;m >= 0 &&pan[m][n].getHad() == t;m --);//向上走到不一样的点
for(m ++;m < 19 &&pan[m][n].getHad() == t ;m ++,count ++);
if(count == 5)
return true;
count = 0;
for(m = i, n =j;n >= 0 && pan[m][n].getHad() == t;n --);//向左走
for(n ++;n < 19 && pan[m][n].getHad() == t;n++,count ++);
if(count == 5)
return true;
count = 0;
for(m = i,n = j;m >= 0 && n >= 0 && pan[m][n].getHad() == t;m --,n --);//向左上走
for(m ++,n ++;m < 19 && n < 19 && pan[m][n].getHad() == t; m ++,n ++,count ++);
if(count == 5)
return true;
count = 0;
for(m = i,n = j; m >= 0 && n < 19 && pan[m][n].getHad() == t;m --,n ++);//向右上走
for(m ++,n --;m <19 && n >= 0 && pan[m][n].getHad() == t; m ++,n --,count ++);
if(count == 5)
return true;
return false;
}
public void mouseEntered(MouseEvent e) {}
public void mouseExited(MouseEvent e){}
public void mousePressed(MouseEvent e){}
public void mouseReleased(MouseEvent e){}
public void paint(Graphics g)
{
super.paint(g);
g.setColor(Color.BLACK);
for(int i = 0;i <= 19;i ++)
{
g.drawLine( startX,i * Grid.he + startY,Grid.he * 19 + startX, i * Grid.he + startY);
}
for(int i = 0;i <=19;i ++)
{
g.drawLine( i * Grid.he + startX,startY,i * Grid.he + startX,Grid.he * 19 + startY );
}
ListIterator it = al.listIterator();
while(it.hasNext())
{
Grid temp = (Grid)it.next();
if(temp.getHad() == 1)
g.setColor(Color.red);
else
g.setColor(Color.black);
g.fillOval(temp.getX(),temp.getY(),Grid.he,Grid.he);
}
}
}
public class MainClass {
public static void main(String[] args) {
MainFrame mf = new MainFrame();
}
}