package com;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Main extends JFrame implements MouseListener, ActionListener,
Runnable {
/**
* @author 小柒
*/
private static final long serialVersionUID = 1L;
private int width;// 屏幕宽度
private int height;// 屏幕
private int color = 0;// 旗子的颜色标识 0:黑子 1:白子
private int Array[][] = new int[18][18];// 0无子 1黑(人)2白(电脑)
JButton btn1 = new JButton("重置");
JButton btn2 = new JButton("取消");
// JLabel jl = new JLabel("玩家先手");
private int xx = 215, yy = 53;// 原点
private int w = 340, h = 340, panelx = 600, panely = 490;// 画布
// 电脑坐标
private int cx, cy;
private int grade[][] = new int[18][18];
private int side = 0;
public Main() {
Thread thread = new Thread(this);
thread.start();
this.setTitle("五子棋");
this.setLayout(null);
width = Toolkit.getDefaultToolkit().getScreenSize().width;
height = Toolkit.getDefaultToolkit().getScreenSize().height;
this.setBounds((width - 400) / 2, (height - 300) / 2, panelx, panely);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.addMouseListener(this);// 加入鼠标监听
// btn1.addActionListener(this);
// btn2.addActionListener(this);
// btn1.setBounds(90, 400, 80, 30);
// btn2.setBounds(190, 400, 80, 30);
// jl.setBounds(20, 380, 80, 20);
// this.add(btn1);
// this.add(btn2);
// this.add(jl);
this.setResizable(false);
this.setVisible(true);
gameStart();
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
/*
* if (e.getSource() == btn1) { gameStart(); } if (e.getSource() ==
* btn2) { System.exit(0); }
*/
}
@Override
public void mouseClicked(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseEntered(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseExited(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mousePressed(MouseEvent e) {// 监听
// TODO Auto-generated method stub
/*
* Random random = new Random(); int x1 = Math.abs(random.nextInt()) %
* 350 + 15; int y1 = Math.abs(random.nextInt()) % 350 + 45;
*/
int x, y;
x = e.getX();
y = e.getY();
System.out.println("x=" + e.getX() + "y=" + e.getY());
if (x > 42 && x < 137 && y < 376 && y > 349) {
System.out.print("end!");
System.exit(0);
}
if ((x > 42) && (x < 137) && (y < 329) && (y > 306)) {
this.gameStart();
System.out.print("restart!");
}
if (e.getX() < xx - 5 || e.getX() > w + xx + 5 || e.getY() < yy - 5
|| e.getY() > h + yy + 5) {
return;
}
if (setDown(x, y, 0) == 0) {// 人
// setDown(x1, y1, 1);// 电脑
// 电脑判断最佳坐标
bestway();
try {
Thread.sleep(500);
} catch (InterruptedException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
// 电脑落子
setDown(cx, cy, 1);
}
}
void bestway()// 最佳位置
{
int i, j, max = 0;
for (i = 0; i < 18; i++) {
for (j = 0; j < 18; j++) {
grade[i][j] = 10;
if (Array[i][j] == 0) {
// 人
Array[i][j] = 1;
if (win(i, j, 1, 2) == 1) {
if (side == 0)
grade[i][j] += 20;
else if (side == 1)
grade[i][j] += 10;
else if (side == 2)
grade[i][j] += 5;
}
if (win(i, j, 1, 3) == 1) {
if (side == 0)
grade[i][j] += 50;
else if (side == 1)
grade[i][j] += 25;
else if (side == 2)
grade[i][j] += 5;
}
if (win(i, j, 1, 4) == 1) {
if (side == 0)
grade[i][j] += 200;
else if (side == 1)
grade[i][j] += 60;
else if (side == 2)
grade[i][j] += 5;
}
if (win(i, j, 1, 5) == 1)
grade[i][j] += 999;// 最大权值
// 电脑
Array[i][j] = 2;
if (win(i, j, 2, 2) == 1) {
if (side == 0)
grade[i][j] += 25;
else if (side == 1)
grade[i][j] += 10;
else if (side == 2)
grade[i][j] += 5;
}
if (win(i, j, 2, 3) == 1) {
if (side == 0)
grade[i][j] += 100;
else if (side == 1)
grade[i][j] += 30;
else if (side == 2)
grade[i][j] += 5;
}
if (win(i, j, 2, 4) == 1) {
if (side == 0)
grade[i][j] += 500;
else if (side == 1)
grade[i][j] += 100;
else if (side == 2)
grade[i][j] += 5;
}
if (win(i, j, 2, 5) == 1)
grade[i][j] += 998;// 第二大权值
Array[i][j] = 0;
}
if (max < grade[i][j]) {
max = grade[i][j];
this.cx = i;
this.cy = j;
}
}
}
}
@Override
public void paint(Graphics g) {// 画棋盘
super.paint(g);
// g.setColor(Color.lightGray);
// g.fill3DRect(20, 50, 340, 340, true);
Image img = Toolkit.getDefaultToolkit().getImage("chess.jpg");
g.drawImage(img, 0, 0, panelx, panely, this);
g.setColor(Color.black);
for (int i = 0; i < 18; i++) {
g.drawLine(xx, yy + 20 * i, w + xx, yy + 20 * i);
g.drawLine(xx + 20 * i, yy, xx + 20 * i, h + yy);
}
}
@Override
public void mouseReleased(MouseEvent e) {
// TODO Auto-generated method stub
}
public int setDown(int x, int y, int type) {// 画棋子
// type computer 1 person 0
int oX = xx, oY = yy;// 原点
if (type == 0) {
x -= oX;
y -= oY;
if (x % 20 > 10)
x += 20;
if (y % 20 > 10)
y += 20;
x = (x / 20) * 20;
y = (y / 20) * 20;
}
if (type == 1) {
x *= 20;
y *= 20;
}
if (Array[x / 20][y / 20] != 0)// 警告
{
// jl.setText("此处有子");
return 1;
}
Graphics g = getGraphics();
// 抗锯齿
((Graphics2D) g).setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
/*
* if(color==0) { g.setColor(Color.white); jl.setText("黑子"); } else {
* g.setColor(Color.black); jl.setText("白子"); }
*/
if (type == 1)// 电脑
{
g.setColor(Color.white);
// jl.setText("黑子");
} else if (type == 0) {
g.setColor(Color.black);
// jl.setText("白子");
}
Array[x / 20][y / 20] = color + 1;
g.fillOval((x - 10) + oX, (y - 10) + oY, 20, 20);
color = (color + 1) % 2;
if (win(x / 20, y / 20, (color + 1) % 2 + 1, 5) == 1)// 胜利
{
if (color == 1)
JOptionPane.showMessageDialog(null, "黑方胜利了");
else
JOptionPane.showMessageDialog(null, "白方胜利了");
gameStart();
}
return 0;
}
public int win(int x, int y, int type, int n) {// 输赢判断 n为连子数
int flag = 0, sum = 0;
int i, temp_side = 0;// 连子被挡数;
// 横
if (flag == 0) {
temp_side = 0;
for (i = 0; i < n; i++) {// 右边
if (x + i > 17)// 防数组越界
break;
if (Array[x + i][y] == type)
sum++;
if (Array[x + i][y] == (type + 2) % 2 + 1) {
temp_side++;
break;
}
if (Array[x + i][y] == 0)
break;
}
for (i = 0; i < n; i++) {// 左边
if (x - i < 0)
break;
if (Array[x - i][y] == type)
sum++;
if (Array[x - i][y] == (type + 2) % 2 + 1) {
temp_side++;
break;
}
if (Array[x - i][y] == 0)
break;
}
if (sum > n) {
flag = 1;
this.side = temp_side;
} else
sum = 0;// 归零
}
// 竖
if (flag == 0) {
temp_side = 0;
for (i = 0; i < n; i++) {// 下边
if (y + i > 17)// 防数组越界
break;
if (Array[x][y + i] == type)
sum++;
if (Array[x][y + i] == (type + 2) % 2 + 1) {
temp_side++;
break;
}
if (Array[x][y + i] == 0)
break;
}
for (i = 0; i < n; i++) {// 上边
if (y - i < 0)
break;
if (Array[x][y - i] == type)
sum++;
if (Array[x][y - i] == (type + 2) % 2 + 1) {
temp_side++;
break;
}
if (Array[x][y - i] == 0)
break;
- 1
- 2
前往页