javaswing五子棋
时间: 2024-01-14 15:21:50 浏览: 90
以下是一个使用Java Swing实现的五子棋游戏的示例:
```java
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Gobang extends JFrame {
private static final int ROWS = 15;
private static final int COLS = 15;
private static final int CELL_SIZE = 30;
private static final int WIDTH = COLS * CELL_SIZE;
private static final int HEIGHT = ROWS * CELL_SIZE;
private boolean isBlackTurn = true;
private boolean gameOver = false;
private int[][] board = new int[ROWS][COLS];
public Gobang() {
setTitle("Java Swing 五子棋");
setSize(WIDTH, HEIGHT);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setResizable(false);
BoardPanel boardPanel = new BoardPanel();
add(boardPanel);
boardPanel.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
if (gameOver) {
return;
}
int x = e.getX() / CELL_SIZE;
int y = e.getY() / CELL_SIZE;
if (x >= 0 && x < COLS && y >= 0 && y < ROWS && board[y][x] == 0) {
board[y][x] = isBlackTurn ? 1 : 2;
isBlackTurn = !isBlackTurn;
boardPanel.repaint();
if (checkWin(x, y)) {
String winner = isBlackTurn ? "白棋" : "黑棋";
JOptionPane.showMessageDialog(null, winner + "获胜!");
gameOver = true;
}
}
}
});
}
private boolean checkWin(int x, int y) {
int color = isBlackTurn ? 1 : 2;
// 水平方向
int count = 1;
for (int i = x - 1; i >= 0 && board[y][i] == color; i--) {
count++;
}
for (int i = x + 1; i < COLS && board[y][i] == color; i++) {
count++;
}
if (count >= 5) {
return true;
}
// 垂直方向
count = 1;
for (int i = y - 1; i >= 0 && board[i][x] == color; i--) {
count++;
}
for (int i = y + 1; i < ROWS && board[i][x] == color; i++) {
count++;
}
if (count >= 5) {
return true;
}
// 左上到右下方向
count = 1;
for (int i = x - 1, j = y - 1; i >= 0 && j >= 0 && board[j][i] == color; i--, j--) {
count++;
}
for (int i = x + 1, j = y + 1; i < COLS && j < ROWS && board[j][i] == color; i++, j++) {
count++;
}
if (count >= 5) {
return true;
}
// 右上到左下方向
count = 1;
for (int i = x + 1, j = y - 1; i < COLS && j >= 0 && board[j][i] == color; i++, j--) {
count++;
}
for (int i = x - 1, j = y + 1; i >= 0 && j < ROWS && board[j][i] == color; i--, j++) {
count++;
}
if (count >= 5) {
return true;
}
return false;
}
private class BoardPanel extends JPanel {
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(Color.BLACK);
for (int i = 0; i <= ROWS; i++) {
g.drawLine(0, i * CELL_SIZE, WIDTH, i * CELL_SIZE);
}
for (int i = 0; i <= COLS; i++) {
g.drawLine(i * CELL_SIZE, 0, i * CELL_SIZE, HEIGHT);
}
for (int i = 0; i < ROWS; i++) {
for (int j = 0; j < COLS; j++) {
if (board[i][j] == 1) {
g.setColor(Color.BLACK);
g.fillOval(j * CELL_SIZE + 2, i * CELL_SIZE + 2, CELL_SIZE - 4, CELL_SIZE - 4);
} else if (board[i][j] == 2) {
g.setColor(Color.WHITE);
g.fillOval(j * CELL_SIZE + 2, i * CELL_SIZE + 2, CELL_SIZE - 4, CELL_SIZE - 4);
}
}
}
}
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> {
Gobang gobang = new Gobang();
gobang.setVisible(true);
});
}
}
```
阅读全文