五子棋java代码实现
时间: 2024-05-06 15:15:33 浏览: 92
java实现的五子棋代码
以下是五子棋的 Java 代码实现,包括游戏逻辑和界面显示。
```java
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class FiveChess extends JPanel {
private static final long serialVersionUID = 1L;
// 棋盘格子的大小
private static final int GRID_SIZE = 30;
// 棋盘的行数和列数
private static final int ROWS = 15;
private static final int COLS = 15;
// 棋盘的横坐标和纵坐标的起点
private static final int X_OFFSET = 50;
private static final int Y_OFFSET = 50;
// 黑子和白子的半径
private static final int BLACK_RADIUS = GRID_SIZE / 2 - 2;
private static final int WHITE_RADIUS = GRID_SIZE / 2 - 2;
// 黑子和白子的颜色
private static final Color BLACK_COLOR = Color.BLACK;
private static final Color WHITE_COLOR = Color.WHITE;
// 当前下棋方
private boolean isBlack = true;
// 记录棋盘上的棋子
private int[][] chessBoard = new int[ROWS][COLS];
// 游戏是否结束
private boolean gameOver = false;
public FiveChess() {
addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
if (gameOver) {
return;
}
int x = e.getX() - X_OFFSET;
int y = e.getY() - Y_OFFSET;
int col = x / GRID_SIZE;
int row = y / GRID_SIZE;
if (col >= 0 && col < COLS && row >= 0 && row < ROWS && chessBoard[row][col] == 0) {
chessBoard[row][col] = isBlack ? 1 : 2;
isBlack = !isBlack;
repaint();
// 判断游戏是否结束
if (isWin(row, col)) {
gameOver = true;
String message = (chessBoard[row][col] == 1) ? "黑方" : "白方";
message += "获胜!";
JFrame frame = (JFrame) getRootPane().getParent();
frame.setTitle(message);
}
}
}
});
}
// 判断是否获胜
private boolean isWin(int row, int col) {
int color = chessBoard[row][col];
int count = 1;
// 横向
for (int i = col - 1; i >= 0 && chessBoard[row][i] == color; i--) {
count++;
}
for (int i = col + 1; i < COLS && chessBoard[row][i] == color; i++) {
count++;
}
if (count >= 5) {
return true;
}
// 纵向
count = 1;
for (int i = row - 1; i >= 0 && chessBoard[i][col] == color; i--) {
count++;
}
for (int i = row + 1; i < ROWS && chessBoard[i][col] == color; i++) {
count++;
}
if (count >= 5) {
return true;
}
// 左上-右下
count = 1;
for (int i = row - 1, j = col - 1; i >= 0 && j >= 0 && chessBoard[i][j] == color; i--, j--) {
count++;
}
for (int i = row + 1, j = col + 1; i < ROWS && j < COLS && chessBoard[i][j] == color; i++, j++) {
count++;
}
if (count >= 5) {
return true;
}
// 右上-左下
count = 1;
for (int i = row - 1, j = col + 1; i >= 0 && j < COLS && chessBoard[i][j] == color; i--, j++) {
count++;
}
for (int i = row + 1, j = col - 1; i < ROWS && j >= 0 && chessBoard[i][j] == color; i++, j--) {
count++;
}
if (count >= 5) {
return true;
}
return false;
}
protected void paintComponent(Graphics g) {
super.paintComponent(g);
// 画棋盘
for (int i = 0; i < ROWS; i++) {
g.drawLine(X_OFFSET, Y_OFFSET + i * GRID_SIZE, X_OFFSET + (COLS - 1) * GRID_SIZE, Y_OFFSET + i * GRID_SIZE);
}
for (int i = 0; i < COLS; i++) {
g.drawLine(X_OFFSET + i * GRID_SIZE, Y_OFFSET, X_OFFSET + i * GRID_SIZE, Y_OFFSET + (ROWS - 1) * GRID_SIZE);
}
// 画棋子
for (int i = 0; i < ROWS; i++) {
for (int j = 0; j < COLS; j++) {
if (chessBoard[i][j] == 1) {
g.setColor(BLACK_COLOR);
g.fillOval(X_OFFSET + j * GRID_SIZE - BLACK_RADIUS, Y_OFFSET + i * GRID_SIZE - BLACK_RADIUS, BLACK_RADIUS * 2, BLACK_RADIUS * 2);
} else if (chessBoard[i][j] == 2) {
g.setColor(WHITE_COLOR);
g.fillOval(X_OFFSET + j * GRID_SIZE - WHITE_RADIUS, Y_OFFSET + i * GRID_SIZE - WHITE_RADIUS, WHITE_RADIUS * 2, WHITE_RADIUS * 2);
}
}
}
}
public static void main(String[] args) {
JFrame frame = new JFrame("五子棋");
frame.add(new FiveChess());
frame.setSize(X_OFFSET * 2 + GRID_SIZE * (COLS - 1), Y_OFFSET * 2 + GRID_SIZE * (ROWS - 1));
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
```
这个程序使用 Java Swing 组件库,可以在桌面上运行。如果想在网页上实现五子棋,可以使用 HTML5 和 JavaScript 技术。
阅读全文