|
|
|
|
@ -1,487 +1,487 @@
|
|
|
|
|
package wuziqi.window;
|
|
|
|
|
|
|
|
|
|
import javax.swing.*;
|
|
|
|
|
import java.awt.*;
|
|
|
|
|
import java.awt.event.ActionEvent;
|
|
|
|
|
import java.awt.event.ActionListener;
|
|
|
|
|
import java.awt.event.MouseEvent;
|
|
|
|
|
import java.awt.event.MouseListener;
|
|
|
|
|
|
|
|
|
|
public class pen extends JPanel //画笔
|
|
|
|
|
{
|
|
|
|
|
int ref = 1;
|
|
|
|
|
int a[][]=new int[16][16];
|
|
|
|
|
boolean cor=true;
|
|
|
|
|
boolean change=false;
|
|
|
|
|
int gx=-1;int gy=-1;
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void setLayout(LayoutManager mgr) {
|
|
|
|
|
super.setLayout(null);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
JButton jb1;
|
|
|
|
|
JButton jb2;
|
|
|
|
|
JButton jb3;
|
|
|
|
|
public void setJb(JButton jb1) {
|
|
|
|
|
this.jb1 = jb1;
|
|
|
|
|
add(jb1);
|
|
|
|
|
}
|
|
|
|
|
/*
|
|
|
|
|
* 画棋盘
|
|
|
|
|
* 根据数组中的数据来将棋子画到棋盘中
|
|
|
|
|
* */
|
|
|
|
|
@Override
|
|
|
|
|
public void paintComponent(Graphics g)
|
|
|
|
|
{
|
|
|
|
|
super.paintComponent(g);
|
|
|
|
|
Graphics2D g2d = (Graphics2D) g;
|
|
|
|
|
g2d.setColor(Color.BLACK);
|
|
|
|
|
for (int i = 0; i <= 15; i++) {
|
|
|
|
|
g2d.drawLine(50, 50 + i * 30, 500, 50 + i * 30);
|
|
|
|
|
g2d.drawLine(50 + i * 30, 50, 50 + i * 30, 500);
|
|
|
|
|
}
|
|
|
|
|
Stroke stroke = new BasicStroke(3);
|
|
|
|
|
g2d.setStroke(stroke);
|
|
|
|
|
for (int i = 0; i < 16; i++)
|
|
|
|
|
for (int j = 0; j < 16; j++) {
|
|
|
|
|
if (a[i][j] == 1) {
|
|
|
|
|
g2d.setColor(Color.BLACK);
|
|
|
|
|
g2d.fillOval(50+j*30-10,50+i*30-10, 20, 20);
|
|
|
|
|
} else if (a[i][j] == 2) {
|
|
|
|
|
g2d.setColor(Color.WHITE);
|
|
|
|
|
g2d.fillOval(50 + j * 30 - 10, 50 + i * 30 - 10, 20, 20);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
g2d.setColor(Color.LIGHT_GRAY);
|
|
|
|
|
g2d.fillRect(0,0,550,50);
|
|
|
|
|
g2d.setColor(Color.BLACK);
|
|
|
|
|
g2d.setFont(new Font("楷体", 20, 25));
|
|
|
|
|
g2d.drawString("五子棋小游戏",250,25);
|
|
|
|
|
/**********************游戏栏********************************/
|
|
|
|
|
g2d.setColor(Color.CYAN);//设置画笔颜色
|
|
|
|
|
g2d.fillRect(550, 350, 200, 150);//绘制游戏状态区域
|
|
|
|
|
g2d.setColor(Color.black);//设置画笔颜色
|
|
|
|
|
g2d.setFont(new Font("黑体", 10, 20));//设置字体
|
|
|
|
|
g2d.drawString("游戏信息", 610, 380);//绘制字符
|
|
|
|
|
if(ref==1)
|
|
|
|
|
{
|
|
|
|
|
g2d.drawString("轮到黑子",610, 410);
|
|
|
|
|
}
|
|
|
|
|
else if(ref==2)
|
|
|
|
|
{
|
|
|
|
|
g2d.drawString("轮到白子",610, 410);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for(int i=0;i<16;i++)
|
|
|
|
|
{
|
|
|
|
|
for (int j=0;j<16;j++)
|
|
|
|
|
{
|
|
|
|
|
System.out.print(a[i][j]);
|
|
|
|
|
}
|
|
|
|
|
System.out.println();
|
|
|
|
|
}
|
|
|
|
|
System.out.println();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private class MyMouse implements MouseListener
|
|
|
|
|
/*根据鼠标点击的位置来将黑子和白子的数据的存储到二维数组中
|
|
|
|
|
* 黑子的值为1
|
|
|
|
|
* 白子的值为2
|
|
|
|
|
* 根据二维数组中的数据来判断此次的棋子的值是否可以插入到二维数组中
|
|
|
|
|
* */
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void mouseClicked(MouseEvent e) {
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
@Override
|
|
|
|
|
public void mousePressed(MouseEvent e) {
|
|
|
|
|
if (cor)//开始
|
|
|
|
|
{
|
|
|
|
|
int x = e.getX();
|
|
|
|
|
int y = e.getY();
|
|
|
|
|
x = x - 50;
|
|
|
|
|
y = y - 50;
|
|
|
|
|
int m = x / 30;
|
|
|
|
|
int n = y / 30;
|
|
|
|
|
int m1 = x % 30;
|
|
|
|
|
int n1 = y % 30;
|
|
|
|
|
if (ref == 1) {
|
|
|
|
|
if (x >= 0 && y >= 0 && x <= 450 && y <= 450) {
|
|
|
|
|
if (n1 > 15) {
|
|
|
|
|
if (m1 > 15) {
|
|
|
|
|
if(a[n + 1][m + 1]==0) {
|
|
|
|
|
a[n + 1][m + 1] = 1;//黑子为1
|
|
|
|
|
gx=n+1;gy=m+1;
|
|
|
|
|
// System.out.println(gx+" "+gy+"第一个");
|
|
|
|
|
ref++;
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
if(a[n+1][m]==0){
|
|
|
|
|
a[n + 1][m] = 1;
|
|
|
|
|
gx=n+1;gy=m;
|
|
|
|
|
// System.out.println(gx+" "+gy+"第二个");
|
|
|
|
|
ref++;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
if (m1 > 15) {
|
|
|
|
|
if(a[n][m + 1] ==0) {
|
|
|
|
|
a[n][m + 1] = 1;//黑子为1
|
|
|
|
|
gx=n;gy=m+1;
|
|
|
|
|
// System.out.println(gx+" "+gy+"第三个");
|
|
|
|
|
ref++;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
if(a[n][m]==0){
|
|
|
|
|
a[n][m] = 1;
|
|
|
|
|
gx=n;gy=m;
|
|
|
|
|
// System.out.println(gx+" "+gy+"第四个");
|
|
|
|
|
ref++;}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (ref == 2) {
|
|
|
|
|
if (x >= 0 && y >= 0 && x <= 450 && y <= 450) {
|
|
|
|
|
if (n1 > 15) {
|
|
|
|
|
if (m1 > 15) {
|
|
|
|
|
if(a[n+1][m+1]==0){
|
|
|
|
|
a[n + 1][m + 1] = 2;//白子为2\
|
|
|
|
|
gx=n+1;gy=m+1;
|
|
|
|
|
// System.out.println(gx+" "+gy+"第五个");
|
|
|
|
|
ref--;
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
if(a[n+1][m]==0){
|
|
|
|
|
a[n + 1][m] = 2;
|
|
|
|
|
gx=n+1;gy=m;
|
|
|
|
|
// System.out.println(gx+" "+gy+"第六个");
|
|
|
|
|
ref--;}
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
if (m1 > 15) {
|
|
|
|
|
if(a[n][m+1]==0){
|
|
|
|
|
a[n][m + 1] = 2;//黑子为2
|
|
|
|
|
gx=n;gy=m+1;
|
|
|
|
|
// System.out.println(gx+" "+gy+"第七个");
|
|
|
|
|
ref--;}
|
|
|
|
|
} else {
|
|
|
|
|
if(a[n][m]==0){
|
|
|
|
|
a[n][m] = 2;
|
|
|
|
|
gx=n;gy=m;
|
|
|
|
|
// System.out.println(gx+" "+gy+"第八个");
|
|
|
|
|
ref--;}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if(x >= 0 && y >= 0 && x <= 450 && y <= 450){
|
|
|
|
|
|
|
|
|
|
if(gx!=-1&&gy!=-1){
|
|
|
|
|
repaint();
|
|
|
|
|
if(judgment(gx,gy)==1)
|
|
|
|
|
{
|
|
|
|
|
JOptionPane.showMessageDialog(null, "黑方赢了");//消息窗口
|
|
|
|
|
cor=false;
|
|
|
|
|
|
|
|
|
|
}else if(judgment(gx,gy)==2) {
|
|
|
|
|
JOptionPane.showMessageDialog(null, "白方赢了");
|
|
|
|
|
cor=false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void mouseReleased(MouseEvent e) {
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void mouseEntered(MouseEvent e) {
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void mouseExited(MouseEvent e) {
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
public int judgment(int m,int n)//判断黑子赢还是白子赢
|
|
|
|
|
{
|
|
|
|
|
int mm=m;int nn=n;int key=a[m][n];int count=1;
|
|
|
|
|
/****************************************************************/
|
|
|
|
|
//从上下两个方向来判断
|
|
|
|
|
while (mm>0)
|
|
|
|
|
{
|
|
|
|
|
mm--;
|
|
|
|
|
if(a[mm][nn]==key)
|
|
|
|
|
{
|
|
|
|
|
count++;
|
|
|
|
|
}
|
|
|
|
|
if(count>=5)
|
|
|
|
|
{
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
if(a[mm][nn]!=key)
|
|
|
|
|
{
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
if(count>=5)
|
|
|
|
|
{
|
|
|
|
|
return key;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
mm=m;
|
|
|
|
|
while (mm<15)
|
|
|
|
|
{
|
|
|
|
|
mm++;
|
|
|
|
|
if(a[mm][nn]==key)
|
|
|
|
|
{
|
|
|
|
|
count++;
|
|
|
|
|
}
|
|
|
|
|
if(count>=5)
|
|
|
|
|
{
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
if (a[mm][nn]!=key)
|
|
|
|
|
{
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if(count>=5)
|
|
|
|
|
{
|
|
|
|
|
return key;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
count=1;
|
|
|
|
|
mm=m;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
/*********************************************************************/
|
|
|
|
|
//从左右两个方向来判断
|
|
|
|
|
while (nn>0)
|
|
|
|
|
{
|
|
|
|
|
nn--;
|
|
|
|
|
if(a[mm][nn]==key)
|
|
|
|
|
{
|
|
|
|
|
count++;
|
|
|
|
|
}
|
|
|
|
|
if(count>=5)
|
|
|
|
|
{
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
if(a[mm][nn]!=key)
|
|
|
|
|
{
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
if(count>=5)
|
|
|
|
|
{
|
|
|
|
|
return key;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
nn=n;
|
|
|
|
|
while (nn<15)
|
|
|
|
|
{
|
|
|
|
|
nn++;
|
|
|
|
|
if(a[mm][nn]==key)
|
|
|
|
|
{
|
|
|
|
|
count++;
|
|
|
|
|
}
|
|
|
|
|
if(count>=5)
|
|
|
|
|
{
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
if (a[mm][nn]!=key)
|
|
|
|
|
{
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if(count>=5)
|
|
|
|
|
{
|
|
|
|
|
return key;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
count=1;
|
|
|
|
|
nn=n;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
/**********************************************************************/
|
|
|
|
|
//从左上到右下来判断
|
|
|
|
|
while (mm>0&&mm<15&&nn>0&&nn<15)
|
|
|
|
|
{
|
|
|
|
|
mm--;nn--;
|
|
|
|
|
if(a[mm][nn]==key)
|
|
|
|
|
{
|
|
|
|
|
count++;
|
|
|
|
|
}
|
|
|
|
|
if(count>=5)
|
|
|
|
|
{
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
if(a[mm][nn]!=key)
|
|
|
|
|
{
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if(count>=5)
|
|
|
|
|
{
|
|
|
|
|
return key;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
mm=m;nn=n;
|
|
|
|
|
while (mm>0&&mm<15&&nn>0&&nn<15)
|
|
|
|
|
{
|
|
|
|
|
mm++;nn++;
|
|
|
|
|
if(a[mm][nn]==key)
|
|
|
|
|
{
|
|
|
|
|
count++;
|
|
|
|
|
}
|
|
|
|
|
if(count>=5)
|
|
|
|
|
{
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
if(a[mm][nn]!=key)
|
|
|
|
|
{
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if(count>=5)
|
|
|
|
|
return key;
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
mm=m;
|
|
|
|
|
nn=n;
|
|
|
|
|
count=1;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
/*************************************************************************/
|
|
|
|
|
//从右上到左下方向查找
|
|
|
|
|
while (mm>0&&mm<15&&nn>0&&nn<15)
|
|
|
|
|
{
|
|
|
|
|
mm--;nn++;
|
|
|
|
|
if(a[mm][nn]==key)
|
|
|
|
|
{
|
|
|
|
|
count++;
|
|
|
|
|
}
|
|
|
|
|
if(count>=5)
|
|
|
|
|
{
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
if(a[mm][nn]!=key)
|
|
|
|
|
{
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if(count>=5)
|
|
|
|
|
{
|
|
|
|
|
return key;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
mm=m;nn=n;
|
|
|
|
|
while (mm>0&&mm<15&&nn>0&&nn<15)
|
|
|
|
|
{
|
|
|
|
|
mm++;nn--;
|
|
|
|
|
if(a[mm][nn]==key)
|
|
|
|
|
{
|
|
|
|
|
count++;
|
|
|
|
|
}
|
|
|
|
|
if(count>=5)
|
|
|
|
|
{
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
if(a[mm][nn]!=key)
|
|
|
|
|
{
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if(count>=5)
|
|
|
|
|
return key;
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
mm=m;
|
|
|
|
|
nn=n;
|
|
|
|
|
count=1;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
//悔棋事件
|
|
|
|
|
public class regret implements ActionListener
|
|
|
|
|
{
|
|
|
|
|
@Override
|
|
|
|
|
public void actionPerformed(ActionEvent e) {
|
|
|
|
|
if(gx>=0&&gy>=0&&gx<16&gy<16&&a[gx][gy]!=0){
|
|
|
|
|
a[gx][gy]=0;
|
|
|
|
|
repaint();
|
|
|
|
|
if(ref==1)
|
|
|
|
|
{
|
|
|
|
|
ref++;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
ref--;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
for(int i=0;i<16;i++)
|
|
|
|
|
{
|
|
|
|
|
for (int j=0;j<16;j++)
|
|
|
|
|
{
|
|
|
|
|
System.out.print(a[i][j]);
|
|
|
|
|
}
|
|
|
|
|
System.out.println();
|
|
|
|
|
}
|
|
|
|
|
System.out.println();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
//认输事件
|
|
|
|
|
private class concede implements ActionListener{
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void actionPerformed(ActionEvent e) {
|
|
|
|
|
if (ref == 1) {
|
|
|
|
|
JOptionPane.showMessageDialog(null, "白方赢了");
|
|
|
|
|
cor=false;
|
|
|
|
|
}
|
|
|
|
|
if (ref == 2)
|
|
|
|
|
{
|
|
|
|
|
JOptionPane.showMessageDialog(null, "黑方赢了");
|
|
|
|
|
cor=false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
pen()
|
|
|
|
|
{
|
|
|
|
|
addMouseListener(new MyMouse());
|
|
|
|
|
setBackground(Color.LIGHT_GRAY);
|
|
|
|
|
Font font = new Font("华文行楷", Font.BOLD, 16);
|
|
|
|
|
jb2=new JButton("悔棋");
|
|
|
|
|
jb3=new JButton("认输");
|
|
|
|
|
jb2.setFont(font);
|
|
|
|
|
jb3.setFont(font);
|
|
|
|
|
jb2.addActionListener(new regret());
|
|
|
|
|
jb3.addActionListener(new concede());
|
|
|
|
|
jb2.setBounds(600,175,100,50);
|
|
|
|
|
jb3.setBounds(600,300,100,50);
|
|
|
|
|
add(jb2);
|
|
|
|
|
add(jb3);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
package wuziqi.window;
|
|
|
|
|
|
|
|
|
|
import javax.swing.*;
|
|
|
|
|
import java.awt.*;
|
|
|
|
|
import java.awt.event.ActionEvent;
|
|
|
|
|
import java.awt.event.ActionListener;
|
|
|
|
|
import java.awt.event.MouseEvent;
|
|
|
|
|
import java.awt.event.MouseListener;
|
|
|
|
|
|
|
|
|
|
public class pen extends JPanel //画笔
|
|
|
|
|
{
|
|
|
|
|
int ref = 1;//黑色为1,白色为2
|
|
|
|
|
int a[][]=new int[16][16];
|
|
|
|
|
boolean cor=true;
|
|
|
|
|
boolean change=false;
|
|
|
|
|
int gx=-1;int gy=-1;
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void setLayout(LayoutManager mgr) {
|
|
|
|
|
super.setLayout(null);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
JButton jb1;
|
|
|
|
|
JButton jb2;
|
|
|
|
|
JButton jb3;
|
|
|
|
|
public void setJb(JButton jb1) {
|
|
|
|
|
this.jb1 = jb1;
|
|
|
|
|
add(jb1);
|
|
|
|
|
}
|
|
|
|
|
/*
|
|
|
|
|
* 画棋盘
|
|
|
|
|
* 根据数组中的数据来将棋子画到棋盘中
|
|
|
|
|
* */
|
|
|
|
|
@Override
|
|
|
|
|
public void paintComponent(Graphics g)
|
|
|
|
|
{
|
|
|
|
|
super.paintComponent(g);
|
|
|
|
|
Graphics2D g2d = (Graphics2D) g;
|
|
|
|
|
g2d.setColor(Color.BLACK);
|
|
|
|
|
for (int i = 0; i <= 15; i++) {
|
|
|
|
|
g2d.drawLine(50, 50 + i * 30, 500, 50 + i * 30);
|
|
|
|
|
g2d.drawLine(50 + i * 30, 50, 50 + i * 30, 500);
|
|
|
|
|
}
|
|
|
|
|
Stroke stroke = new BasicStroke(3);
|
|
|
|
|
g2d.setStroke(stroke);
|
|
|
|
|
for (int i = 0; i < 16; i++)
|
|
|
|
|
for (int j = 0; j < 16; j++) {
|
|
|
|
|
if (a[i][j] == 1) {
|
|
|
|
|
g2d.setColor(Color.BLACK);
|
|
|
|
|
g2d.fillOval(50+j*30-10,50+i*30-10, 20, 20);
|
|
|
|
|
} else if (a[i][j] == 2) {
|
|
|
|
|
g2d.setColor(Color.WHITE);
|
|
|
|
|
g2d.fillOval(50 + j * 30 - 10, 50 + i * 30 - 10, 20, 20);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
g2d.setColor(Color.LIGHT_GRAY);
|
|
|
|
|
g2d.fillRect(0,0,550,50);
|
|
|
|
|
g2d.setColor(Color.BLACK);
|
|
|
|
|
g2d.setFont(new Font("楷体", 20, 25));
|
|
|
|
|
g2d.drawString("五子棋小游戏",250,25);
|
|
|
|
|
/**********************游戏栏********************************/
|
|
|
|
|
g2d.setColor(Color.CYAN);//设置画笔颜色
|
|
|
|
|
g2d.fillRect(550, 350, 200, 150);//绘制游戏状态区域
|
|
|
|
|
g2d.setColor(Color.black);//设置画笔颜色
|
|
|
|
|
g2d.setFont(new Font("黑体", 10, 20));//设置字体
|
|
|
|
|
g2d.drawString("游戏信息", 610, 380);//绘制字符
|
|
|
|
|
if(ref==1)
|
|
|
|
|
{
|
|
|
|
|
g2d.drawString("轮到黑子",610, 410);
|
|
|
|
|
}
|
|
|
|
|
else if(ref==2)
|
|
|
|
|
{
|
|
|
|
|
g2d.drawString("轮到白子",610, 410);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for(int i=0;i<16;i++)
|
|
|
|
|
{
|
|
|
|
|
for (int j=0;j<16;j++)
|
|
|
|
|
{
|
|
|
|
|
System.out.print(a[i][j]);
|
|
|
|
|
}
|
|
|
|
|
System.out.println();
|
|
|
|
|
}
|
|
|
|
|
System.out.println();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private class MyMouse implements MouseListener
|
|
|
|
|
/*根据鼠标点击的位置来将黑子和白子的数据的存储到二维数组中
|
|
|
|
|
* 黑子的值为1
|
|
|
|
|
* 白子的值为2
|
|
|
|
|
* 根据二维数组中的数据来判断此次的棋子的值是否可以插入到二维数组中
|
|
|
|
|
* */
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void mouseClicked(MouseEvent e) {
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
@Override
|
|
|
|
|
public void mousePressed(MouseEvent e) {
|
|
|
|
|
if (cor)//开始
|
|
|
|
|
{
|
|
|
|
|
int x = e.getX();
|
|
|
|
|
int y = e.getY();
|
|
|
|
|
x = x - 50;
|
|
|
|
|
y = y - 50;
|
|
|
|
|
int m = x / 30;
|
|
|
|
|
int n = y / 30;
|
|
|
|
|
int m1 = x % 30;
|
|
|
|
|
int n1 = y % 30;
|
|
|
|
|
if (ref == 1) {
|
|
|
|
|
if (x >= 0 && y >= 0 && x <= 450 && y <= 450) {
|
|
|
|
|
if (n1 > 15) {
|
|
|
|
|
if (m1 > 15) {
|
|
|
|
|
if(a[n + 1][m + 1]==0) {
|
|
|
|
|
a[n + 1][m + 1] = 1;//黑子为1
|
|
|
|
|
gx=n+1;gy=m+1;
|
|
|
|
|
// System.out.println(gx+" "+gy+"第一个");
|
|
|
|
|
ref++;
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
if(a[n+1][m]==0){
|
|
|
|
|
a[n + 1][m] = 1;
|
|
|
|
|
gx=n+1;gy=m;
|
|
|
|
|
// System.out.println(gx+" "+gy+"第二个");
|
|
|
|
|
ref++;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
if (m1 > 15) {
|
|
|
|
|
if(a[n][m + 1] ==0) {
|
|
|
|
|
a[n][m + 1] = 1;//黑子为1
|
|
|
|
|
gx=n;gy=m+1;
|
|
|
|
|
// System.out.println(gx+" "+gy+"第三个");
|
|
|
|
|
ref++;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
if(a[n][m]==0){
|
|
|
|
|
a[n][m] = 1;
|
|
|
|
|
gx=n;gy=m;
|
|
|
|
|
// System.out.println(gx+" "+gy+"第四个");
|
|
|
|
|
ref++;}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (ref == 2) {
|
|
|
|
|
if (x >= 0 && y >= 0 && x <= 450 && y <= 450) {
|
|
|
|
|
if (n1 > 15) {
|
|
|
|
|
if (m1 > 15) {
|
|
|
|
|
if(a[n+1][m+1]==0){
|
|
|
|
|
a[n + 1][m + 1] = 2;//白子为2\
|
|
|
|
|
gx=n+1;gy=m+1;
|
|
|
|
|
// System.out.println(gx+" "+gy+"第五个");
|
|
|
|
|
ref--;
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
if(a[n+1][m]==0){
|
|
|
|
|
a[n + 1][m] = 2;
|
|
|
|
|
gx=n+1;gy=m;
|
|
|
|
|
// System.out.println(gx+" "+gy+"第六个");
|
|
|
|
|
ref--;}
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
if (m1 > 15) {
|
|
|
|
|
if(a[n][m+1]==0){
|
|
|
|
|
a[n][m + 1] = 2;//黑子为2
|
|
|
|
|
gx=n;gy=m+1;
|
|
|
|
|
// System.out.println(gx+" "+gy+"第七个");
|
|
|
|
|
ref--;}
|
|
|
|
|
} else {
|
|
|
|
|
if(a[n][m]==0){
|
|
|
|
|
a[n][m] = 2;
|
|
|
|
|
gx=n;gy=m;
|
|
|
|
|
// System.out.println(gx+" "+gy+"第八个");
|
|
|
|
|
ref--;}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if(x >= 0 && y >= 0 && x <= 450 && y <= 450){
|
|
|
|
|
|
|
|
|
|
if(gx!=-1&&gy!=-1){
|
|
|
|
|
repaint();
|
|
|
|
|
if(judgment(gx,gy)==1)
|
|
|
|
|
{
|
|
|
|
|
JOptionPane.showMessageDialog(null, "黑方赢了");//消息窗口
|
|
|
|
|
cor=false;
|
|
|
|
|
|
|
|
|
|
}else if(judgment(gx,gy)==2) {
|
|
|
|
|
JOptionPane.showMessageDialog(null, "白方赢了");
|
|
|
|
|
cor=false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void mouseReleased(MouseEvent e) {
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void mouseEntered(MouseEvent e) {
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void mouseExited(MouseEvent e) {
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
public int judgment(int m,int n)//判断黑子赢还是白子赢
|
|
|
|
|
{
|
|
|
|
|
int mm=m;int nn=n;int key=a[m][n];int count=1;
|
|
|
|
|
/****************************************************************/
|
|
|
|
|
//从上下两个方向来判断
|
|
|
|
|
while (mm>0)
|
|
|
|
|
{
|
|
|
|
|
mm--;
|
|
|
|
|
if(a[mm][nn]==key)
|
|
|
|
|
{
|
|
|
|
|
count++;
|
|
|
|
|
}
|
|
|
|
|
if(count>=5)
|
|
|
|
|
{
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
if(a[mm][nn]!=key)
|
|
|
|
|
{
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
if(count>=5)
|
|
|
|
|
{
|
|
|
|
|
return key;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
mm=m;
|
|
|
|
|
while (mm<15)
|
|
|
|
|
{
|
|
|
|
|
mm++;
|
|
|
|
|
if(a[mm][nn]==key)
|
|
|
|
|
{
|
|
|
|
|
count++;
|
|
|
|
|
}
|
|
|
|
|
if(count>=5)
|
|
|
|
|
{
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
if (a[mm][nn]!=key)
|
|
|
|
|
{
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if(count>=5)
|
|
|
|
|
{
|
|
|
|
|
return key;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
count=1;
|
|
|
|
|
mm=m;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
/*********************************************************************/
|
|
|
|
|
//从左右两个方向来判断
|
|
|
|
|
while (nn>0)
|
|
|
|
|
{
|
|
|
|
|
nn--;
|
|
|
|
|
if(a[mm][nn]==key)
|
|
|
|
|
{
|
|
|
|
|
count++;
|
|
|
|
|
}
|
|
|
|
|
if(count>=5)
|
|
|
|
|
{
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
if(a[mm][nn]!=key)
|
|
|
|
|
{
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
if(count>=5)
|
|
|
|
|
{
|
|
|
|
|
return key;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
nn=n;
|
|
|
|
|
while (nn<15)
|
|
|
|
|
{
|
|
|
|
|
nn++;
|
|
|
|
|
if(a[mm][nn]==key)
|
|
|
|
|
{
|
|
|
|
|
count++;
|
|
|
|
|
}
|
|
|
|
|
if(count>=5)
|
|
|
|
|
{
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
if (a[mm][nn]!=key)
|
|
|
|
|
{
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if(count>=5)
|
|
|
|
|
{
|
|
|
|
|
return key;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
count=1;
|
|
|
|
|
nn=n;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
/**********************************************************************/
|
|
|
|
|
//从左上到右下来判断
|
|
|
|
|
while (mm>0&&mm<15&&nn>0&&nn<15)
|
|
|
|
|
{
|
|
|
|
|
mm--;nn--;
|
|
|
|
|
if(a[mm][nn]==key)
|
|
|
|
|
{
|
|
|
|
|
count++;
|
|
|
|
|
}
|
|
|
|
|
if(count>=5)
|
|
|
|
|
{
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
if(a[mm][nn]!=key)
|
|
|
|
|
{
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if(count>=5)
|
|
|
|
|
{
|
|
|
|
|
return key;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
mm=m;nn=n;
|
|
|
|
|
while (mm>0&&mm<15&&nn>0&&nn<15)
|
|
|
|
|
{
|
|
|
|
|
mm++;nn++;
|
|
|
|
|
if(a[mm][nn]==key)
|
|
|
|
|
{
|
|
|
|
|
count++;
|
|
|
|
|
}
|
|
|
|
|
if(count>=5)
|
|
|
|
|
{
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
if(a[mm][nn]!=key)
|
|
|
|
|
{
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if(count>=5)
|
|
|
|
|
return key;
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
mm=m;
|
|
|
|
|
nn=n;
|
|
|
|
|
count=1;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
/*************************************************************************/
|
|
|
|
|
//从右上到左下方向查找
|
|
|
|
|
while (mm>0&&mm<15&&nn>0&&nn<15)
|
|
|
|
|
{
|
|
|
|
|
mm--;nn++;
|
|
|
|
|
if(a[mm][nn]==key)
|
|
|
|
|
{
|
|
|
|
|
count++;
|
|
|
|
|
}
|
|
|
|
|
if(count>=5)
|
|
|
|
|
{
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
if(a[mm][nn]!=key)
|
|
|
|
|
{
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if(count>=5)
|
|
|
|
|
{
|
|
|
|
|
return key;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
mm=m;nn=n;
|
|
|
|
|
while (mm>0&&mm<15&&nn>0&&nn<15)
|
|
|
|
|
{
|
|
|
|
|
mm++;nn--;
|
|
|
|
|
if(a[mm][nn]==key)
|
|
|
|
|
{
|
|
|
|
|
count++;
|
|
|
|
|
}
|
|
|
|
|
if(count>=5)
|
|
|
|
|
{
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
if(a[mm][nn]!=key)
|
|
|
|
|
{
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if(count>=5)
|
|
|
|
|
return key;
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
mm=m;
|
|
|
|
|
nn=n;
|
|
|
|
|
count=1;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
//悔棋事件
|
|
|
|
|
public class regret implements ActionListener
|
|
|
|
|
{
|
|
|
|
|
@Override
|
|
|
|
|
public void actionPerformed(ActionEvent e) {
|
|
|
|
|
if(gx>=0&&gy>=0&&gx<16&gy<16&&a[gx][gy]!=0){
|
|
|
|
|
a[gx][gy]=0;
|
|
|
|
|
repaint();
|
|
|
|
|
if(ref==1)
|
|
|
|
|
{
|
|
|
|
|
ref++;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
ref--;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
for(int i=0;i<16;i++)
|
|
|
|
|
{
|
|
|
|
|
for (int j=0;j<16;j++)
|
|
|
|
|
{
|
|
|
|
|
System.out.print(a[i][j]);
|
|
|
|
|
}
|
|
|
|
|
System.out.println();
|
|
|
|
|
}
|
|
|
|
|
System.out.println();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
//认输事件
|
|
|
|
|
private class concede implements ActionListener{
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void actionPerformed(ActionEvent e) {
|
|
|
|
|
if (ref == 1) {
|
|
|
|
|
JOptionPane.showMessageDialog(null, "白方赢了");
|
|
|
|
|
cor=false;
|
|
|
|
|
}
|
|
|
|
|
if (ref == 2)
|
|
|
|
|
{
|
|
|
|
|
JOptionPane.showMessageDialog(null, "黑方赢了");
|
|
|
|
|
cor=false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
pen()
|
|
|
|
|
{
|
|
|
|
|
addMouseListener(new MyMouse());
|
|
|
|
|
setBackground(Color.LIGHT_GRAY);
|
|
|
|
|
Font font = new Font("华文行楷", Font.BOLD, 16);
|
|
|
|
|
jb2=new JButton("悔棋");
|
|
|
|
|
jb3=new JButton("认输");
|
|
|
|
|
jb2.setFont(font);
|
|
|
|
|
jb3.setFont(font);
|
|
|
|
|
jb2.addActionListener(new regret());
|
|
|
|
|
jb3.addActionListener(new concede());
|
|
|
|
|
jb2.setBounds(600,175,100,50);
|
|
|
|
|
jb3.setBounds(600,300,100,50);
|
|
|
|
|
add(jb2);
|
|
|
|
|
add(jb3);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|