用Java编写五子棋小游戏
时间: 2023-09-09 22:05:43 浏览: 119
基于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 GobangGame extends JFrame {
private static final long serialVersionUID = 1L;
private final int MARGIN = 30; // 棋盘边距
private final int GRID_SPAN = 35; // 网格间距
private final int ROWS = 15; // 棋盘行数
private final int COLS = 15; // 棋盘列数
private final int MAX_STEPS = ROWS * COLS; // 最大步数
private int[][] chessBoard = new int[ROWS][COLS]; // 棋盘数组
private boolean isBlack = true; // 黑棋先手
private boolean gameOver = false; // 游戏结束标志
private int xIndex = -1, yIndex = -1; // 当前下子的位置
public GobangGame() {
setTitle("Java五子棋小游戏");
setSize(600, 600);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setResizable(false);
ChessBoard chessBoardPanel = new ChessBoard();
add(chessBoardPanel);
setVisible(true);
}
// 重写paint方法,绘制棋盘和棋子
public void paint(Graphics g) {
super.paint(g);
// 绘制棋盘
for (int i = 0; i < ROWS; i++) {
g.drawLine(MARGIN, MARGIN + i * GRID_SPAN, MARGIN + (COLS - 1) * GRID_SPAN, MARGIN + i * GRID_SPAN);
g.drawLine(MARGIN + i * GRID_SPAN, MARGIN, MARGIN + i * GRID_SPAN, MARGIN + (ROWS - 1) * GRID_SPAN);
}
// 绘制棋子
for (int i = 0; i < ROWS; i++) {
for (int j = 0; j < COLS; j++) {
if (chessBoard[i][j] == 1) {
g.setColor(Color.BLACK);
g.fillOval(MARGIN + i * GRID_SPAN - 15, MARGIN + j * GRID_SPAN - 15, 30, 30);
} else if (chessBoard[i][j] == 2) {
g.setColor(Color.WHITE);
g.fillOval(MARGIN + i * GRID_SPAN - 15, MARGIN + j * GRID_SPAN - 15, 30, 30);
}
}
}
}
// 判断是否胜利
private boolean checkWin(int[][] arr, int x, int y) {
int count = 1;
// 水平方向
for (int i = x - 1; i >= 0; i--) {
if (arr[i][y] == arr[x][y]) {
count++;
} else {
break;
}
}
for (int i = x + 1; i < ROWS; i++) {
if (arr[i][y] == arr[x][y]) {
count++;
} else {
break;
}
}
if (count >= 5) {
return true;
}
// 垂直方向
count = 1;
for (int i = y - 1; i >= 0; i--) {
if (arr[x][i] == arr[x][y]) {
count++;
} else {
break;
}
}
for (int i = y + 1; i < COLS; i++) {
if (arr[x][i] == arr[x][y]) {
count++;
} else {
break;
}
}
if (count >= 5) {
return true;
}
// 左上到右下方向
count = 1;
for (int i = x - 1, j = y - 1; i >= 0 && j >= 0; i--, j--) {
if (arr[i][j] == arr[x][y]) {
count++;
} else {
break;
}
}
for (int i = x + 1, j = y + 1; i < ROWS && j < COLS; i++, j++) {
if (arr[i][j] == arr[x][y]) {
count++;
} else {
break;
}
}
if (count >= 5) {
return true;
}
// 右上到左下方向
count = 1;
for (int i = x - 1, j = y + 1; i >= 0 && j < COLS; i--, j++) {
if (arr[i][j] == arr[x][y]) {
count++;
} else {
break;
}
}
for (int i = x + 1, j = y - 1; i < ROWS && j >= 0; i++, j--) {
if (arr[i][j] == arr[x][y]) {
count++;
} else {
break;
}
}
if (count >= 5) {
return true;
}
return false;
}
// 内部类,表示棋盘面板
class ChessBoard extends JPanel {
private static final long serialVersionUID = 1L;
public ChessBoard() {
setBackground(new Color(255, 216, 185));
addMouseListener(new ChessBoardListener());
}
// 重写paintComponent方法,绘制棋盘和棋子
public void paintComponent(Graphics g) {
super.paintComponent(g);
// 绘制棋盘
for (int i = 0; i < ROWS; i++) {
g.drawLine(MARGIN, MARGIN + i * GRID_SPAN, MARGIN + (COLS - 1) * GRID_SPAN, MARGIN + i * GRID_SPAN);
g.drawLine(MARGIN + i * GRID_SPAN, MARGIN, MARGIN + i * GRID_SPAN, MARGIN + (ROWS - 1) * GRID_SPAN);
}
// 绘制棋子
for (int i = 0; i < ROWS; i++) {
for (int j = 0; j < COLS; j++) {
if (chessBoard[i][j] == 1) {
g.setColor(Color.BLACK);
g.fillOval(MARGIN + i * GRID_SPAN - 15, MARGIN + j * GRID_SPAN - 15, 30, 30);
} else if (chessBoard[i][j] == 2) {
g.setColor(Color.WHITE);
g.fillOval(MARGIN + i * GRID_SPAN - 15, MARGIN + j * GRID_SPAN - 15, 30, 30);
}
}
}
}
}
// 内部类,表示棋盘鼠标监听器
class ChessBoardListener extends MouseAdapter {
public void mouseClicked(MouseEvent e) {
if (gameOver) {
return;
}
int xPos = e.getX();
int yPos = e.getY();
int xIndex = Math.round((float) (xPos - MARGIN) / GRID_SPAN);
int yIndex = Math.round((float) (yPos - MARGIN) / GRID_SPAN);
if (xIndex < 0 || xIndex >= ROWS || yIndex < 0 || yIndex >= COLS) {
return;
}
if (chessBoard[xIndex][yIndex] != 0) {
return;
}
chessBoard[xIndex][yIndex] = isBlack ? 1 : 2;
if (checkWin(chessBoard, xIndex, yIndex)) {
gameOver = true;
String msg = String.format("%s赢了!", isBlack ? "黑棋" : "白棋");
JOptionPane.showMessageDialog(null, msg);
} else if (++xIndex * ++yIndex >= MAX_STEPS) { // 判断是否平局
gameOver = true;
JOptionPane.showMessageDialog(null, "平局!");
}
isBlack = !isBlack;
repaint();
}
}
public static void main(String[] args) {
new GobangGame();
}
}
```
该示例代码使用了Java的Swing GUI库,实现了一个简单的五子棋小游戏。在此示例中,我们使用了一个`JFrame`窗口,将一个自定义的`JPanel`面板添加到窗口中,并在面板中绘制棋盘和棋子。同时,我们还为面板添加了一个鼠标监听器,用于处理玩家下棋的事件,并实现了判断胜负的逻辑。
阅读全文