用Java写一个简单的五子棋游戏并讲解思路
时间: 2024-03-09 13:49:01 浏览: 21
好的,下面是一个简单的五子棋游戏的Java代码实现及其思路讲解。
思路:
1. 创建一个棋盘数组来存储棋子的位置和状态。
2. 玩家通过鼠标点击来放置棋子,放置棋子时需要判断该位置是否已经有棋子,若有则不能放置,否则放置棋子并判断胜负。
3. 判断胜负时需要判断横向、纵向、斜向是否有相同颜色的棋子连成五个,若有则该颜色的玩家获胜。
4. 当有一方获胜或者棋盘已满时游戏结束。
代码实现:
```
import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
public class Gomoku extends JFrame {
private final int BOARD_SIZE = 15; // 棋盘大小
private final int CELL_SIZE = 40; // 格子大小
private final int MARGIN_SIZE = 20; // 边距大小
private final int PANEL_SIZE = MARGIN_SIZE * 2 + CELL_SIZE * (BOARD_SIZE - 1); // 面板大小
private final int WIN_COUNT = 5; // 获胜所需连续棋子数
private int[][] board; // 棋盘数组,用于存储棋子的位置和状态
private int turn; // 当前玩家,1 表示黑方,2 表示白方
private boolean gameOver; // 游戏是否结束
public Gomoku() {
super("五子棋游戏");
setSize(PANEL_SIZE, PANEL_SIZE); // 设置窗口大小
setLocationRelativeTo(null); // 窗口居中
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
board = new int[BOARD_SIZE][BOARD_SIZE]; // 初始化棋盘数组
turn = 1; // 黑方先手
gameOver = false; // 游戏开始
addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
if (gameOver) { // 游戏结束,不再接受点击事件
return;
}
int row = (e.getY() - MARGIN_SIZE + CELL_SIZE / 2) / CELL_SIZE;
int col = (e.getX() - MARGIN_SIZE + CELL_SIZE / 2) / CELL_SIZE;
if (row < 0 || row >= BOARD_SIZE || col < 0 || col >= BOARD_SIZE) { // 点击位置不在棋盘范围内
return;
}
if (board[row][col] != 0) { // 点击位置已有棋子
return;
}
board[row][col] = turn; // 放置棋子
repaint(); // 重绘面板
if (checkWin(row, col)) { // 判断胜负
JOptionPane.showMessageDialog(Gomoku.this, (turn == 1 ? "黑方" : "白方") + "获胜!");
gameOver = true; // 游戏结束
return;
}
turn = 3 - turn; // 交换玩家
}
});
setVisible(true); // 显示窗口
}
private boolean checkWin(int row, int col) {
int count = 1; // 连续棋子数
int color = board[row][col]; // 当前棋子的颜色
int r, c;
// 横向
for (c = col - 1; c >= 0 && board[row][c] == color; c--) {
count++;
}
for (c = col + 1; c < BOARD_SIZE && board[row][c] == color; c++) {
count++;
}
if (count >= WIN_COUNT) {
return true;
}
// 纵向
count = 1;
for (r = row - 1; r >= 0 && board[r][col] == color; r--) {
count++;
}
for (r = row + 1; r < BOARD_SIZE && board[r][col] == color; r++) {
count++;
}
if (count >= WIN_COUNT) {
return true;
}
// 左上到右下斜向
count = 1;
for (r = row - 1, c = col - 1; r >= 0 && c >= 0 && board[r][c] == color; r--, c--) {
count++;
}
for (r = row + 1, c = col + 1; r < BOARD_SIZE && c < BOARD_SIZE && board[r][c] == color; r++, c++) {
count++;
}
if (count >= WIN_COUNT) {
return true;
}
// 右上到左下斜向
count = 1;
for (r = row - 1, c = col + 1; r >= 0 && c < BOARD_SIZE && board[r][c] == color; r--, c++) {
count++;
}
for (r = row + 1, c = col - 1; r < BOARD_SIZE && c >= 0 && board[r][c] == color; r++, c--) {
count++;
}
if (count >= WIN_COUNT) {
return true;
}
return false;
}
@Override
public void paint(Graphics g) {
super.paint(g);
g.setColor(Color.BLACK);
// 绘制棋盘
for (int i = 0; i < BOARD_SIZE; i++) {
g.drawLine(MARGIN_SIZE, MARGIN_SIZE + i * CELL_SIZE, PANEL_SIZE - MARGIN_SIZE, MARGIN_SIZE + i * CELL_SIZE);
g.drawLine(MARGIN_SIZE + i * CELL_SIZE, MARGIN_SIZE, MARGIN_SIZE + i * CELL_SIZE, PANEL_SIZE - MARGIN_SIZE);
}
// 绘制棋子
for (int i = 0; i < BOARD_SIZE; i++) {
for (int j = 0; j < BOARD_SIZE; j++) {
if (board[i][j] == 1) { // 黑方棋子
g.setColor(Color.BLACK);
g.fillOval(MARGIN_SIZE + j * CELL_SIZE - CELL_SIZE / 2, MARGIN_SIZE + i * CELL_SIZE - CELL_SIZE / 2, CELL_SIZE, CELL_SIZE);
} else if (board[i][j] == 2) { // 白方棋子
g.setColor(Color.WHITE);
g.fillOval(MARGIN_SIZE + j * CELL_SIZE - CELL_SIZE / 2, MARGIN_SIZE + i * CELL_SIZE - CELL_SIZE / 2, CELL_SIZE, CELL_SIZE);
}
}
}
}
public static void main(String[] args) {
new Gomoku();
}
}
```
运行程序后,即可开始游戏。玩家通过鼠标点击来放置棋子,放置棋子时需要判断该位置是否已经有棋子,若有则不能放置,否则放置棋子并判断胜负。判断胜负时需要判断横向、纵向、斜向是否有相同颜色的棋子连成五个,若有则该颜色的玩家获胜。当有一方获胜或者棋盘已满时游戏结束。