import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.geom.*;
import java.util.*;
import java.awt.*;
import javax.imageio.ImageIO;
import java.io.File;
import java.io.IOException;
import javax.swing.event.*;
import java.awt.event.*;
public class Fivezi {
JFrame frame = new JFrame();
JFrame tishi = new JFrame();// 提示输赢窗体
JLabel tishil = new JLabel();
JDialog tishid=new JDialog(frame,true);
JPanel tiship = new JPanel();
Button chonglai = new Button();
Button quxiao = new Button();
Button qingkong = new Button();
// Panel_ a = new Panel_();
Cmp b = new Cmp();
public int count = 1;// 步数
public ArrayList<Line2D> line = new ArrayList<Line2D>();// 绘制棋盘
int x = 70;// 棋盘左上角点坐标
int y = 70;
int xx = 24;// 棋盘线间的间隔
public ArrayList<Rectangle2D> zi = new ArrayList<Rectangle2D>();// 用于确定所点方块
Integer yanse[] = new Integer[500];
Qizi qq = new Qizi();
w current = new w();
// 棋子类
public class Qizi {
public Integer qizi[][] = new Integer[15][15];
public Rectangle2D qizi1[][] = new Rectangle2D[15][15];
Qizi() {
for (int i = 0; i < 15; i++) {
for (int j = 0; j < 15; j++) {
qizi1[i][j] = new Rectangle2D.Double(x - xx / 2 + xx * i, y
- xx / 2 + xx * j, xx, xx);
qizi[i][j] = 0;
}
}
}
}
public class w {
public int x;
public int y;
w() {
x = 0;
y = 0;
}
}
// 左侧按钮类
/*
* public class Panel_ extends Panel { Panel_() { qingkong.setSize(50, 20);
* qingkong.setLabel("重新开始"); setSize(200, 500); //
* setBackground(Color.BLUE); qingkong.setLocation(20, 20);
*
*
* }
*
* }
*/
// 下棋界面
public class Cmp extends JPanel implements MouseListener {
Cmp() {
setSize(500, 500);
setBackground(Color.black);
addMouseListener(this);
qingkong.setSize(50, 20);
qingkong.setLabel("重新开始");
qingkong.setLocation(20, 20);
add(qingkong);
}
// 绘制五子棋
public void paint(Graphics g) {
Graphics2D g2 = (Graphics2D) g;
String twilight = "aaa.jpg";
File abbc = new File("aaa.jpg");
Toolkit toolkit = Toolkit.getDefaultToolkit();
Image image1 = toolkit.getImage("aaa.jpg");
g2.drawImage(image1, 0, 0, null);
for (Line2D r : line)
g2.draw(r);
weizhirect(g);
}
// 找到鼠标所点矩形
public w find(Point2D p) {
w temp = new w();
for (int i = 0; i < 15; i++) {
for (int j = 0; j < 15; j++) {
if (qq.qizi1[i][j].contains(p)) {
temp.x = i;
temp.y = j;
return temp;
}
}
}
temp.x = -1;
temp.y = -1;
return temp;
}
// 鼠标事件
public void mouseEntered(MouseEvent e) {
}
// 鼠标点击时调用
public void mouseClicked(MouseEvent e) {
current = find(e.getPoint());
if (current.x >= 0 && current.y >= 0) {
if (qq.qizi[current.x][current.y] == 0) {
if (count % 2 == 0) {
qq.qizi[current.x][current.y] = 2;
qiZishezhi();
int count11 = 1;// 横
int count12 = 1;// 竖
int count13 = 1;// 左协
int count14 = 1;// 右~用于判定四个方向是否有超过5的。
int flag11 = 1;
int flag12 = 1;
int flag13 = 1;
int flag14 = 1;
int flag15 = 1;
int flag16 = 1;
int flag17 = 1;
int flag18 = 1;
for (int i = 1; i < 15; i++) {
if (current.x + i < 15) {
if (qq.qizi[current.x + i][current.y] != 2)
flag11 = 0;
if (qq.qizi[current.x + i][current.y] == 2
&& flag11 == 1)
count11++;
}
if (current.x - i >= 0) {
if (qq.qizi[current.x - i][current.y] != 2)
flag12 = 0;
if (qq.qizi[current.x - i][current.y] == 2
&& flag12 == 1)
count11++;
}
if (current.y + i < 15) {
if (qq.qizi[current.x][current.y + i] != 2)
flag13 = 0;
if (qq.qizi[current.x][current.y + i] == 2
&& flag13 == 1)
count12++;
}
if (current.y - i >= 0) {
if (qq.qizi[current.x][current.y - i] != 2)
flag14 = 0;
if (qq.qizi[current.x][current.y - i] == 2
&& flag14 == 1)
count12++;
}
if ((current.y - i >= 0) && (current.x - i >= 0)) {
if (qq.qizi[current.x - i][current.y - i] != 2)
flag15 = 0;
if (qq.qizi[current.x - i][current.y - i] == 2
&& flag15 == 1)
count13++;
}
if ((current.y + i < 15) && (current.x + i < 15)) {
if (qq.qizi[current.x + i][current.y + i] != 2)
flag16 = 0;
if (qq.qizi[current.x + i][current.y + i] == 2
&& flag16 == 1)
count13++;
}
if ((current.y + i < 15) && (current.x - i >= 0)) {
if (qq.qizi[current.x - i][current.y + i] != 2)
flag17 = 0;
if (qq.qizi[current.x - i][current.y + i] == 2
&& flag17 == 1)
count14++;
}
if ((current.y - i >= 0) && (current.x + i < 15)) {
if (qq.qizi[current.x + i][current.y - i] != 2)
flag18 = 0;
if (qq.qizi[current.x + i][current.y - i] == 2
&& flag18 == 1)
count14++;
}
}
// 判定输赢
if (count11 >= 5 || count12 >= 5 || count13 >= 5
|| count14 >= 5) {
tishil.setText("白棋胜!");
tiship.add(tishil);
tishid.add(tiship);
tishid.setVisible(true);
// tishi.add(tiship);
//tishi.setAlwaysOnTop(true);
// tishi.setVisible(true);
}
count++;
} else if (count % 2 == 1) {
qq.qizi[current.x][current.y] = 1;
qiZishezhi();
int count21 = 1;// 横
int count22 = 1;// 竖
int count23 = 1;// 左协
int count24 = 1;// 右~用于判定四个方向是否有超过5的。
int flag11 = 1;
int flag12 = 1;
int flag13 = 1;
int flag14 = 1;
int flag15 = 1;
int flag16 = 1;
int flag17 = 1;
int flag18 = 1;
for (int i = 1; i < 15; i++) {
if (current.x + i < 15) {
if (qq.qizi[current.x + i][current.y] != 1)
flag11 = 0;
if (qq.qizi[current.x + i][current.y] == 1
&& flag11 == 1)
count21++;
}
if (current.x - i >= 0) {
if (qq.qizi[current.x - i][current.y] != 1)
flag12 = 0;
if (qq.qizi[current.x - i][current.y] == 1
&& flag12 == 1)
count21++;
}
if (current.y + i < 15) {
if (qq.qizi[current.x][current.y + i] != 1)
flag13 = 0;
if (qq.qizi[current.x][current.y + i] == 1
&& flag13 == 1)
count22++;
}
if (current.y - i >= 0) {
if (qq.qizi[current.x][current.y - i] != 1)
flag14 = 0;
if (qq.qizi[current.x][current.y - i] == 1
&& flag14 == 1)
count22++;
}
if ((current.y - i >= 0) && (current.x - i >= 0)) {
if (qq.qizi[current.x - i][current.y - i] != 1)
flag15 = 0;
if (qq.qizi[current.x - i][current.y - i] == 1
&& flag15 == 1)
count23++;
}
if ((current.y + i < 15) && (current.x + i < 15)) {
if (qq.qizi[current.x + i][current.y + i] != 1)
flag16 = 0;
if (qq.qizi[current.x + i][current.y + i] == 1
&& flag16 == 1)
count23++;
}
if ((current.y + i < 15) && (current.x - i >= 0)) {
if (qq.qizi[current.x - i][current.y + i] != 1)
flag17 = 0;
if (qq.qizi[current.x - i][current.y + i] == 1
&& flag17 == 1)
count24++;
}
if ((current.y - i >= 0) && (current.x + i < 15)) {
if (qq.qizi[current.x + i][current.y - i] != 1)
flag18 = 0;
if (qq.qizi[current.x + i][current.y - i] == 1