初学Java,做个简单的五子棋人机对战(比较烂)锻炼一下逻辑思维,,, 暂时保存,以后完善。。。`
package 五子棋;
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.image.BufferedImage;
import javax.swing.*;
public class 人机 extends JFrame implements MouseListener {
int x, y, X, Y, h, s, p, l, m, n, k, b = 10, c, max = 1, x1, y1;
int[][] chess = new int[25][25];// 1黑2白
int[][] bai = new int[25][25];// 判断白棋下的位置
boolean canplay = true;
public 人机() {
this.setTitle("五子棋0.1");
this.setSize(800, 800);
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setResizable(false);
this.setVisible(true);
this.repaint();
this.addMouseListener(this);
}
public void paint(Graphics g) {
// 创建一个图片缓冲区,先将图片放到缓冲区上
BufferedImage buf = new BufferedImage(800, 800, BufferedImage.TYPE_INT_RGB);
Graphics g1 = buf.createGraphics(); // 为该画布创建画笔
g1.setColor(Color.gray);
g1.fill3DRect(75, 75, 625, 625, true);// 画棋盘
for (int i = 0; i <= 25; i++) {
g1.setColor(Color.white);
g1.drawLine(75, 75 + 25 * i, 700, 75 + 25 * i);
g1.drawLine(75 + 25 * i, 75, 75 + 25 * i, 700);
}
for (int i = 0; i < 25; i++) {
for (int j = 0; j < 25; j++) {
if (chess[i][j] == 1) {
X = i * 25 + 50;
Y = j * 25 + 50;
g1.setColor(Color.black);
g1.fillOval(X, Y, 22, 22);
}
if (chess[i][j] == 2) {
X = i * 25 + 50;
Y = j * 25 + 50;
g1.setColor(Color.white);
g1.fillOval(X, Y, 22, 22);
}
}
}
g.drawImage(buf, 0, 0, this);
}
public void mousePressed(MouseEvent e) {
x = e.getX();
y = e.getY();
int k = 0, t = 0;
if (x >= 75 && x <= 700 && y >= 75 && y <= 700) {
if (canplay) {
x = (x - 50) / 25;
y = (y - 50) / 25;
if (chess[x][y] == 0) {
chess[x][y] = 1;
if