package 五子棋;
import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JFrame;
public class test {
public static void main(String[] args) {
JFrame frame = new JFrame("五子棋小游戏");
frame.setSize(600, 600);
frame.setResizable(false);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
chessBoard cb = new chessBoard();
frame.setBackground(Color.LIGHT_GRAY);
frame.add(cb);
frame.setVisible(true);
Graphics g = cb.getGraphics();
cb.paint(g);
cb.chessClick();
}
}
package 五子棋;
import java.awt.Color;
import java.awt.Component;
import java.awt.Container;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Toolkit;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
public class chessBoard extends JPanel {
private int size = 50;
private int length = 500;
private int x;
private int y;
private int tx;
private int ty;
private int lx;
private int ly;
int[][] location = new int[12][12];
Boolean isBlack = true;
public void paint(Graphics g) {
g.setColor(Color.BLACK);
for (int i = 0; i < 11; i++) {
g.drawLine(50 + (size * i), 50, 50 + (size * i), length + 50);
}
for (int j = 0; j < 11; j++) {
g.drawLine(50, 50 + (size * j), length + 50, 50 + (size * j));
}
}
public void chessClick() {
this.addMouseListener(new MouseAdapter() {
@Override
public void mousePressed(MouseEvent e) {
super.mouseClicked(e);
x = e.getX();
y = e.getY();
tx = returnX();
ty = returnY();
lx = tx / size;
ly = ty / size;
chessChanged();
}
});
}
private int returnY() {
if (((y - 50) % size) < 25) {
return ((y - 50) / size) * size + 50;
} else {
return ((y - 50) / size + 1) * size + 50;
}
}
private int returnX() {
if (((x - 50) % size) < 25) {
return ((x - 50) / size) * size + 50;
} else {
return ((x - 50) / size + 1) * size + 50;
}
}
public void chessChanged() {
Color white = Color.WHITE;
Color black = Color.BLACK;
Graphics g = getGraphics();
if (isBlack) {
location[lx][ly] = 1;
g.setColor(black);
g.fillOval(tx - 10, ty - 10, 20, 20);
isBlack = false;
} else {
location[lx][ly] = 2;
g.setColor(white);
g.fillOval(tx - 10, ty - 10, 20, 20);
isBlack = true;
}
if (this.isWin()) {
if (isBlack) {
JOptionPane.showMessageDialog(this, "游戏结束,白方胜利");
} else {
JOptionPane.showMessageDialog(this, "游戏结束,黑方胜利");
}
}
}
private boolean isWin() {
Boolean flag = false;
int color = location[lx][ly];
int count = checkCount(1, 0, color);
if (count >= 5) {
flag = true;
} else {
count = checkCount(0, 1, color);
if (count >= 5) {
flag = true;
} else {
count = checkCount(1, 1, color);
if (count >= 5) {
flag = true;
} else {
count = checkCount(1, -1, color);
if (count >= 5) {
flag = true;
}
}
}
}
return flag;
}
private int checkCount(int xChange, int yChange, int color) {
int number = 1;
int tempX = xChange;
int tempY = yChange;
while (lx + xChange <= 11 && ly + yChange <= 11 && lx + xChange > 0 && ly + yChange > 0
&& color == location[lx + xChange][ly + yChange]) {
number++;
if (xChange != 0)
xChange++;
if (yChange != 0) {
if (yChange > 0)
yChange++;
else {
yChange--;
}
}
}
xChange = tempX;
yChange = tempY;
while (lx - xChange <= 11 && ly - yChange <= 11 && lx - xChange > 0 && ly - yChange > 0
&& color == location[lx - xChange][ly - yChange]) {
number++;
if (xChange != 0)
xChange++;
if (yChange != 0) {
if (yChange > 0)
yChange++;
else {
yChange--;
}
}
}
return number;
}
}