用Java实现一个GUI模式的五子棋游戏
时间: 2024-05-01 08:22:12 浏览: 113
以下是一个基本的Java GUI五子棋游戏实现。我们使用Swing库来构建GUI界面。
```java
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Gobang extends JFrame implements ActionListener{
private static final long serialVersionUID = 1L;
private final int ROWS = 15;
private final int COLS = 15;
private final int GAP = 30;
private final int WIDTH = COLS * GAP + 50;
private final int HEIGHT = ROWS * GAP + 70;
private JButton[][] board;
private int[][] data;
private int currentPlayer = 1;
public Gobang() {
setTitle("五子棋");
setSize(WIDTH, HEIGHT);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setResizable(false);
initBoard();
setVisible(true);
}
private void initBoard() {
JPanel p = new JPanel();
p.setLayout(new GridLayout(ROWS, COLS));
board = new JButton[ROWS][COLS];
data = new int[ROWS][COLS];
for (int i = 0; i < ROWS; i++) {
for (int j = 0; j < COLS; j++) {
board[i][j] = new JButton();
board[i][j].setBackground(Color.WHITE);
board[i][j].addActionListener(this);
p.add(board[i][j]);
data[i][j] = 0;
}
}
add(p, BorderLayout.CENTER);
}
public void actionPerformed(ActionEvent e) {
for (int i = 0; i < ROWS; i++) {
for (int j = 0; j < COLS; j++) {
if (e.getSource() == board[i][j]) {
if (data[i][j] == 0) {
board[i][j].setBackground(currentPlayer == 1 ? Color.BLACK : Color.WHITE);
data[i][j] = currentPlayer;
if (checkWin(i, j)) {
JOptionPane.showMessageDialog(this, "玩家 " + currentPlayer + " 获胜!");
System.exit(0);
}
currentPlayer = currentPlayer == 1 ? 2 : 1;
}
}
}
}
}
private boolean checkWin(int row, int col) {
int count = 1;
int r, c;
//检查横向
for (int i = col - 1; i >= 0; i--) {
if (data[row][i] == currentPlayer) {
count++;
} else {
break;
}
}
for (int i = col + 1; i < COLS; i++) {
if (data[row][i] == currentPlayer) {
count++;
} else {
break;
}
}
if (count >= 5) {
return true;
}
//检查纵向
count = 1;
for (int i = row - 1; i >= 0; i--) {
if (data[i][col] == currentPlayer) {
count++;
} else {
break;
}
}
for (int i = row + 1; i < ROWS; i++) {
if (data[i][col] == currentPlayer) {
count++;
} else {
break;
}
}
if (count >= 5) {
return true;
}
//检查左上-右下斜线
count = 1;
r = row - 1;
c = col - 1;
while (r >= 0 && c >= 0) {
if (data[r][c] == currentPlayer) {
count++;
} else {
break;
}
r--;
c--;
}
r = row + 1;
c = col + 1;
while (r < ROWS && c < COLS) {
if (data[r][c] == currentPlayer) {
count++;
} else {
break;
}
r++;
c++;
}
if (count >= 5) {
return true;
}
//检查左下-右上斜线
count = 1;
r = row + 1;
c = col - 1;
while (r < ROWS && c >= 0) {
if (data[r][c] == currentPlayer) {
count++;
} else {
break;
}
r++;
c--;
}
r = row - 1;
c = col + 1;
while (r >= 0 && c < COLS) {
if (data[r][c] == currentPlayer) {
count++;
} else {
break;
}
r--;
c++;
}
if (count >= 5) {
return true;
}
return false;
}
public static void main(String[] args) {
new Gobang();
}
}
```
我们首先定义了一个15x15的棋盘,使用了一个二维数组来存储每个位置的状态。玩家可以通过点击棋盘上的按钮来下棋。每次点击后,我们会检查当前玩家是否获胜,如果是,我们会弹出一个提示框。
在checkWin()方法中,我们会检查是否有5个同色棋子连成一线,如果是,返回true,否则返回false。
这只是一个简单的五子棋游戏实现,你可以根据自己的需求对其进行扩展和优化。
阅读全文