import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.event.MouseEvent;
public class ChessBoard extends JPanel implements MouseListener {
public static int 边界距离 = 30;
public static int 间距 = 35;
public static int ROWS = 4;
public static int COLS = 4;
Point[] chessList = new Point[(ROWS + 1) * (COLS + 1)];
boolean isBlack = true;
boolean gameOver = false;
int chessCount;
int xIndex, yIndex;
public ChessBoard() {
setBackground(Color.PINK);
addMouseListener(this);
addMouseMotionListener(new MouseMotionListener() {
public void mouseMoved(MouseEvent e) {
int x1 = (e.getX() - 边界距离 + 间距 / 2) / 间距;
int y1 = (e.getY() - 边界距离 + 间距 / 2) / 间距;
if (x1 < 0 || x1 > ROWS || y1 < 0 || y1 > COLS || gameOver|| findChess(x1, y1)) {
setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
}
}
public void mouseDragged(MouseEvent e) {
}
});
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
for (int i = 0; i <= ROWS; i++) {
g.drawLine(边界距离, 边界距离 + i * 间距, 边界距离 + COLS* 间距, 边界距离 + i * 间距);
}
for (int i = 0; i <= COLS; i++) {
g.drawLine(边界距离 + i * 间距, 边界距离, 边界距离 + i * 间距,边界距离 + ROWS * 间距);
}
for (int i = 0; i < chessCount; i++) {
int xPos = chessList[i].getX() * 间距 + 边界距离;
int yPos = chessList[i].getY() * 间距 + 边界距离;
g.setColor(chessList[i].getColor());
g.fillOval(xPos - Point.ZHIJING / 2, yPos - Point.ZHIJING / 2,Point.ZHIJING, Point.ZHIJING);
if (i == chessCount - 1) {
g.setColor(Color.red);
g.drawRect(xPos - Point.ZHIJING / 2,yPos - Point.ZHIJING / 2, Point.ZHIJING,Point.ZHIJING);
}
}
}
public void mousePressed(MouseEvent e) {
if (gameOver)
return;
String colorName = isBlack ? "黑棋" : "白棋";
xIndex = (e.getX() - 边界距离 + 间距 / 2) / 间距;
yIndex = (e.getY() - 边界距离 + 间距 / 2) / 间距;
if (xIndex < 0 || xIndex > ROWS || yIndex < 0 || yIndex > COLS)
return;
if (findChess(xIndex, yIndex))
return;
Point ch = new Point(xIndex, yIndex, isBlack ? Color.black : Color.white);
chessList[chessCount++] = ch;
repaint();
if (isWin()) {
String msg = String.format("恭喜,%s赢~", colorName);
JOptionPane.showMessageDialog(this, msg);
gameOver = true;
} else if (chessCount == (COLS + 1) * (ROWS + 1)) {
String msg = String.format("平局");
JOptionPane.showMessageDialog(this, msg);
gameOver = true;
}
isBlack = !isBlack;
}
public void mouseClicked(MouseEvent e) {
}
public void mouseReleased(MouseEvent e) {
}
public void mouseEntered(MouseEvent e) {
}
public void mouseExited(MouseEvent e) {
}
private boolean findChess(int x, int y) {
for (Point c : chessList) {
if (c != null && c.getX() == x && c.getY() == y)
return true;
}
return false;
}
private boolean isWin() {
int continueCount = 1;
for (int x = xIndex - 1; x >= 0; x--) {
Color c = isBlack ? Color.black : Color.white;
if (getChess(x, yIndex, c) != null) {
continueCount++;
} else
break;
}
for (int x = xIndex + 1; x <= ROWS; x++) {
Color c = isBlack ? Color.black : Color.white;
if (getChess(x, yIndex, c) != null) {
continueCount++;
} else
break;
}
if (continueCount >= 5) {
return true;
} else
continueCount = 1;
for (int y = yIndex - 1; y >= 0; y--) {
Color c = isBlack ? Color.black : Color.white;
if (getChess(xIndex, y, c) != null) {
continueCount++;
} else
break;
}
for (int y = yIndex + 1; y <= ROWS; y++) {
Color c = isBlack ? Color.black : Color.white;
if (getChess(xIndex, y, c) != null) {
continueCount++;
} else
break;
}
if (continueCount >= 5) {
return true;
} else
continueCount = 1;
for (int x = xIndex + 1, y = yIndex - 1; y >= 0 && x <= COLS; x++, y--) {
Color c = isBlack ? Color.black : Color.white;
if (getChess(x, y, c) != null) {
continueCount++;
} else
break;
}
for (int x = xIndex - 1, y = yIndex + 1; y <= ROWS && x >= 0; x--, y++) {
Color c = isBlack ? Color.black : Color.white;
if (getChess(x, y, c) != null) {
continueCount++;
} else
break;
}
if (continueCount >= 5) {
return true;
} else
continueCount = 1;
for (int x = xIndex - 1, y = yIndex - 1; y >= 0 && x >= 0; x--, y--) {
Color c = isBlack ? Color.black : Color.white;
if (getChess(x, y, c) != null) {
continueCount++;
} else
break;
}
for (int x = xIndex + 1, y = yIndex + 1; y <= ROWS && x <= COLS; x++, y++) {
Color c = isBlack ? Color.black : Color.white;
if (getChess(x, y, c) != null) {
continueCount++;
} else
break;
}
if (continueCount >= 5) {
return true;
} else
continueCount = 1;
return false;
}
private Point getChess(int xIndex, int yIndex, Color color) {
for (Point c : chessList) {
if (c != null && c.getX() == xIndex && c.getY() == yIndex&& c.getColor() == color)
return c;
}
return null;
}
public void restartGame() {
for (int i = 0; i < chessList.length; i++)
chessList[i] = null;
isBlack = true;
gameOver = false;
chessCount = 0;
repaint();
}
public void goback() {
if (chessCount == 0)
return;
chessList[chessCount - 1] = null;
chessCount--;
if (chessCount > 0) {
xIndex = chessList[chessCount - 1].getX();
yIndex = chessList[chessCount - 1].getY();
}
isBlack = !isBlack;
repaint();
}
public Dimension getPreferredSize() {
return new Dimension(边界距离 * 2 + 间距 * COLS, 边界距离 * 2+ 间距 * ROWS);
}
}
import java.awt.*;
public class Point {
private int x;
private int y;
private Color color;
public static int ZHIJING = 32;
public Point(int x,int y,Color c){
this.x=x;
this.y=y;
color=c;
}
public int getX(){
return x;
}
public int getY(){
return y;
}
public Color getColor(){
return color;
}
}
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
public class StartChessJFrame extends JFrame {
private ChessBoard chessBoard;// 面板
private Panel toolbar;// 工具条面板
private Button startButton;
private Button backButton;
private Button exitButton;
public StartChessJFrame() {
setTitle("五子棋");
chessBoard = new ChessBoard();
MyItemListener lis = new MyItemListener();// 初始化按钮事件监听器内部类
toolbar = new Panel();
startButton = new Button("重新开始");
backButton = new Button("悔棋");
exitButton = new Button("退出");
toolbar.setLayout(new GridLayout(0, 1));// 布局
toolbar.add(backButton);
toolbar.add(startButton);
toolbar.add(exitButton);
startButton.addActionListener(lis);
backButton.addActionListener(lis);
exitButton.addActionListener(lis);// 按钮注册监听事件
add(toolbar, BorderLayout.WEST);
add(chessBoard);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
pack();
}
private class MyItemListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
Object obj = e.getSource();// 获取事件源
if (obj == startButton) {
chessBoard.restartGame();
} else if (obj == exitButton) {
System.exit(0);// 结束
} else if (obj == backButton) {
if (!chessBoard.gameOver)// 游戏结束时不能撤销
chessBoard.goback();
}
}
}
public static void main(String[] args) {
StartChessJFrame f = new StartChessJFrame();// 创建主框架
f.setVisible(true);// 显示
}
}