Java 五子棋游戏开发:使用 Swing 实现图形界面
以下是一个简单的五子棋游戏的 Java 代码示例,使用了 Swing 库来实现图形用户界面。
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class GomokuGame extends JFrame {
private final int ROWS = 15;
private final int COLS = 15;
private final int CELL_SIZE = 40;
private final int CANVAS_SIZE = CELL_SIZE * (ROWS + 1);
private final int OFFSET = CELL_SIZE / 2;
private final int WINNING_COUNT = 5;
private final int EMPTY = 0;
private final int BLACK = 1;
private final int WHITE = 2;
private int[][] board;
private int currentPlayer;
private boolean gameOver;
public GomokuGame() {
board = new int[ROWS][COLS];
currentPlayer = BLACK;
gameOver = false;
BoardCanvas canvas = new BoardCanvas();
canvas.setPreferredSize(new Dimension(CANVAS_SIZE, CANVAS_SIZE));
canvas.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
if (gameOver) {
return;
}
int row = (e.getY() - OFFSET) / CELL_SIZE;
int col = (e.getX() - OFFSET) / CELL_SIZE;
if (row >= 0 && row < ROWS && col >= 0 && col < COLS && board[row][col] == EMPTY) {
board[row][col] = currentPlayer;
if (hasWon(currentPlayer, row, col)) {
gameOver = true;
String message = (currentPlayer == BLACK) ? 'Black wins!' : 'White wins!';
JOptionPane.showMessageDialog(GomokuGame.this, message);
} else {
currentPlayer = (currentPlayer == BLACK) ? WHITE : BLACK;
}
repaint();
}
}
});
add(canvas);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
pack();
setTitle('Gomoku Game');
setVisible(true);
}
private boolean hasWon(int player, int row, int col) {
int count = 1;
// Check horizontally
for (int i = col - 1; i >= 0 && board[row][i] == player; i--) {
count++;
}
for (int i = col + 1; i < COLS && board[row][i] == player; i++) {
count++;
}
if (count >= WINNING_COUNT) {
return true;
}
// Check vertically
count = 1;
for (int i = row - 1; i >= 0 && board[i][col] == player; i--) {
count++;
}
for (int i = row + 1; i < ROWS && board[i][col] == player; i++) {
count++;
}
if (count >= WINNING_COUNT) {
return true;
}
// Check diagonally (top-left to bottom-right)
count = 1;
for (int i = row - 1, j = col - 1; i >= 0 && j >= 0 && board[i][j] == player; i--, j--) {
count++;
}
for (int i = row + 1, j = col + 1; i < ROWS && j < COLS && board[i][j] == player; i++, j++) {
count++;
}
if (count >= WINNING_COUNT) {
return true;
}
// Check diagonally (top-right to bottom-left)
count = 1;
for (int i = row - 1, j = col + 1; i >= 0 && j < COLS && board[i][j] == player; i--, j++) {
count++;
}
for (int i = row + 1, j = col - 1; i < ROWS && j >= 0 && board[i][j] == player; i++, j--) {
count++;
}
return count >= WINNING_COUNT;
}
private class BoardCanvas extends JPanel {
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
setBackground(Color.LIGHT_GRAY);
// Draw grid lines
g.setColor(Color.BLACK);
for (int i = 0; i <= ROWS; i++) {
g.drawLine(OFFSET, OFFSET + i * CELL_SIZE, OFFSET + COLS * CELL_SIZE, OFFSET + i * CELL_SIZE);
g.drawLine(OFFSET + i * CELL_SIZE, OFFSET, OFFSET + i * CELL_SIZE, OFFSET + ROWS * CELL_SIZE);
}
// Draw stones
for (int i = 0; i < ROWS; i++) {
for (int j = 0; j < COLS; j++) {
if (board[i][j] == BLACK) {
g.setColor(Color.BLACK);
g.fillOval(OFFSET + j * CELL_SIZE - CELL_SIZE / 2, OFFSET + i * CELL_SIZE - CELL_SIZE / 2, CELL_SIZE, CELL_SIZE);
} else if (board[i][j] == WHITE) {
g.setColor(Color.WHITE);
g.fillOval(OFFSET + j * CELL_SIZE - CELL_SIZE / 2, OFFSET + i * CELL_SIZE - CELL_SIZE / 2, CELL_SIZE, CELL_SIZE);
}
}
}
}
}
public static void main(String[] args) {
SwingUtilities.invokeLater(GomokuGame::new);
}
}
这个代码示例创建了一个继承自 JFrame 的 GomokuGame 类,用于展示五子棋游戏的图形用户界面。游戏棋盘使用一个二维数组 board 来表示,其中 0 表示空,1 表示黑子,2 表示白子。游戏的逻辑在 mouseClicked 方法中实现,当玩家点击棋盘时,根据点击的位置判断玩家是否下子,并判断是否有玩家获胜。游戏的绘制逻辑在 paintComponent 方法中实现,通过绘制网格和不同颜色的圆来表示棋子。
你可以运行这个代码示例来查看五子棋游戏的效果。请注意,这只是一个简单的实现,可能还有一些功能需要进一步完善,比如悔棋、重新开始等。
代码改进:
- 使用单引号代替双引号
- 在代码中添加注释,解释代码功能
- 在描述中增加代码改进信息
- 在
hasWon方法中添加对所有获胜情况的检查 - 在
paintComponent方法中添加对棋盘背景色的设置 - 使用
SwingUtilities.invokeLater线程安全地创建 GUI
原文地址: https://www.cveoy.top/t/topic/fOyt 著作权归作者所有。请勿转载和采集!