用Java写一个GUI的五子棋游戏
时间: 2023-08-23 15:06:49 浏览: 127
好的,我可以给你提供一个简单的五子棋游戏的代码实现。这个游戏使用Java Swing库来实现GUI界面。
首先,我们需要定义一个 `ChessBoard` 类来表示棋盘,并在其中添加落子、判断胜负等方法。
```java
public class ChessBoard {
private int rows;
private int cols;
private int[][] board;
public ChessBoard(int rows, int cols) {
this.rows = rows;
this.cols = cols;
board = new int[rows][cols];
}
public void reset() {
board = new int[rows][cols];
}
public boolean isValidMove(int row, int col) {
return row >= 0 && row < rows && col >= 0 && col < cols && board[row][col] == 0;
}
public boolean play(int row, int col, int player) {
if (!isValidMove(row, col)) {
return false;
}
board[row][col] = player;
return true;
}
public boolean isWin(int player) {
// 判断横向是否连成五子
for (int i = 0; i < rows; i++) {
for (int j = 0; j <= cols - 5; j++) {
boolean win = true;
for (int k = 0; k < 5; k++) {
if (board[i][j + k] != player) {
win = false;
break;
}
}
if (win) {
return true;
}
}
}
// 判断纵向是否连成五子
for (int i = 0; i <= rows - 5; i++) {
for (int j = 0; j < cols; j++) {
boolean win = true;
for (int k = 0; k < 5; k++) {
if (board[i + k][j] != player) {
win = false;
break;
}
}
if (win) {
return true;
}
}
}
// 判断左上到右下是否连成五子
for (int i = 0; i <= rows - 5; i++) {
for (int j = 0; j <= cols - 5; j++) {
boolean win = true;
for (int k = 0; k < 5; k++) {
if (board[i + k][j + k] != player) {
win = false;
break;
}
}
if (win) {
return true;
}
}
}
// 判断右上到左下是否连成五子
for (int i = 0; i <= rows - 5; i++) {
for (int j = cols - 1; j >= 4; j--) {
boolean win = true;
for (int k = 0; k < 5; k++) {
if (board[i + k][j - k] != player) {
win = false;
break;
}
}
if (win) {
return true;
}
}
}
return false;
}
public int get(int row, int col) {
return board[row][col];
}
public int getRows() {
return rows;
}
public int getCols() {
return cols;
}
}
```
接下来,我们需要定义一个 `ChessBoardPanel` 类来显示棋盘,并在其中添加点击事件处理方法。
```java
public class ChessBoardPanel extends JPanel {
private static final int CELL_SIZE = 40;
private ChessBoard board;
private int currentPlayer;
private int lastRow;
private int lastCol;
public ChessBoardPanel(ChessBoard board) {
this.board = board;
currentPlayer = 1;
lastRow = -1;
lastCol = -1;
setPreferredSize(new Dimension(board.getCols() * CELL_SIZE, board.getRows() * CELL_SIZE));
addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
int row = e.getY() / CELL_SIZE;
int col = e.getX() / CELL_SIZE;
if (row == lastRow && col == lastCol) {
return;
}
if (board.play(row, col, currentPlayer)) {
lastRow = row;
lastCol = col;
repaint();
if (board.isWin(currentPlayer)) {
JOptionPane.showMessageDialog(ChessBoardPanel.this, "Player " + currentPlayer + " wins!");
reset();
} else {
currentPlayer = 3 - currentPlayer;
}
}
}
});
}
public void reset() {
board.reset();
currentPlayer = 1;
lastRow = -1;
lastCol = -1;
repaint();
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
for (int i = 0; i < board.getRows(); i++) {
for (int j = 0; j < board.getCols(); j++) {
g.setColor(Color.WHITE);
g.fillRect(j * CELL_SIZE, i * CELL_SIZE, CELL_SIZE, CELL_SIZE);
g.setColor(Color.BLACK);
g.drawRect(j * CELL_SIZE, i * CELL_SIZE, CELL_SIZE, CELL_SIZE);
if (board.get(i, j) != 0) {
g.setColor(board.get(i, j) == 1 ? Color.BLACK : Color.WHITE);
g.fillOval(j * CELL_SIZE + 2, i * CELL_SIZE + 2, CELL_SIZE - 4, CELL_SIZE - 4);
}
}
}
}
}
```
最后,我们可以在 `Main` 类中创建一个窗口,将 `ChessBoardPanel` 添加到其中,并在点击“重置”按钮时调用 `reset()` 方法。
```java
public class Main {
public static void main(String[] args) {
JFrame frame = new JFrame("五子棋");
ChessBoard board = new ChessBoard(15, 15);
ChessBoardPanel panel = new ChessBoardPanel(board);
JPanel controlPanel = new JPanel();
JButton resetButton = new JButton("重置");
resetButton.addActionListener(e -> panel.reset());
controlPanel.add(resetButton);
frame.getContentPane().add(BorderLayout.CENTER, panel);
frame.getContentPane().add(BorderLayout.SOUTH, controlPanel);
frame.pack();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
```
这样,一个简单的五子棋游戏就完成了。运行程序后,你可以看到一个带有棋盘和重置按钮的窗口。在棋盘上点击鼠标即可落子,当有一方获胜时,程序会弹出提示框,并自动重置棋盘。
阅读全文