用Java的GUI界面设计一个五子棋游戏,可以选择人机对战或人人对战,并能统计一局棋所需时间,统计对战次数以及胜率
时间: 2024-03-05 21:49:51 浏览: 127
为了实现这个五子棋游戏,我们可以使用Java Swing库来创建GUI组件并将它们组合在一起。
下面是一个简单的五子棋游戏界面设计的示例代码:
```java
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class FiveChessGame extends JFrame implements ActionListener {
private JButton[][] buttons;
private JLabel timeLabel;
private JLabel winLabel;
private int[][] board;
private int currentPlayer;
private boolean isComputer;
private boolean isGameOver;
private int winCount;
private int gameCount;
private long startTime;
public FiveChessGame() {
setTitle("五子棋游戏");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(600, 600);
setLocationRelativeTo(null);
// 创建主面板
JPanel mainPanel = new JPanel(new BorderLayout());
// 创建棋盘面板
JPanel boardPanel = new JPanel(new GridLayout(15, 15));
// 初始化棋盘
board = new int[15][15];
// 创建棋盘按钮
buttons = new JButton[15][15];
for (int i = 0; i < 15; i++) {
for (int j = 0; j < 15; j++) {
buttons[i][j] = new JButton();
buttons[i][j].setPreferredSize(new Dimension(40, 40));
buttons[i][j].addActionListener(this);
boardPanel.add(buttons[i][j]);
}
}
// 设置面板布局
mainPanel.add(boardPanel, BorderLayout.CENTER);
// 创建统计面板
JPanel statPanel = new JPanel(new GridLayout(3, 1));
// 创建时间标签
timeLabel = new JLabel("00:00:00");
// 创建胜率标签
winLabel = new JLabel("对战次数:0 胜率:0.00%");
// 创建开始按钮
JButton startButton = new JButton("开始");
startButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
startGame();
}
});
// 添加组件到统计面板
statPanel.add(timeLabel);
statPanel.add(winLabel);
statPanel.add(startButton);
// 设置面板布局
mainPanel.add(statPanel, BorderLayout.EAST);
// 添加主面板到窗口
add(mainPanel);
setVisible(true);
}
// 开始游戏
private void startGame() {
// 初始化游戏数据
currentPlayer = 1;
isComputer = false;
isGameOver = false;
startTime = System.currentTimeMillis();
resetBoard();
// 显示倒计时
Timer timer = new Timer(1000, new ActionListener() {
int time = 0;
@Override
public void actionPerformed(ActionEvent e) {
time++;
timeLabel.setText(String.format("%02d:%02d:%02d", time / 3600, (time % 3600) / 60, time % 60));
}
});
timer.start();
// 显示选项对话框
String[] options = {"人机对战", "人人对战"};
int choice = JOptionPane.showOptionDialog(this, "请选择对战模式", "选项", JOptionPane.DEFAULT_OPTION, JOptionPane.INFORMATION_MESSAGE, null, options, options[0]);
// 根据选择设置游戏模式
if (choice == 0) {
isComputer = true;
}
// 游戏循环
while (!isGameOver) {
if (isComputer && currentPlayer == 2) {
// 如果是电脑下棋
int[] move = getComputerMove();
int row = move[0];
int col = move[1];
makeMove(row, col);
}
}
}
// 重置棋盘
private void resetBoard() {
for (int i = 0; i < 15; i++) {
for (int j = 0; j < 15; j++) {
buttons[i][j].setIcon(null);
board[i][j] = 0;
}
}
}
// 处理棋盘按钮的点击事件
@Override
public void actionPerformed(ActionEvent e) {
JButton button = (JButton) e.getSource();
int row = -1, col = -1;
for (int i = 0; i < 15; i++) {
for (int j = 0; j < 15; j++) {
if (buttons[i][j] == button) {
row = i;
col = j;
break;
}
}
}
if (row >= 0 && col >= 0 && board[row][col] == 0 && !isGameOver) {
makeMove(row, col);
if (isComputer && currentPlayer == 1 && !isGameOver) {
int[] move = getComputerMove();
row = move[0];
col = move[1];
makeMove(row, col);
}
}
}
// 电脑下棋
private int[] getComputerMove() {
// TODO: 实现电脑下棋的算法
return new int[] {0, 0};
}
// 处理一步棋
private void makeMove(int row, int col) {
// 放置棋子
board[row][col] = currentPlayer;
ImageIcon icon = new ImageIcon(currentPlayer == 1 ? "black.png" : "white.png");
buttons[row][col].setIcon(icon);
// 检查是否有玩家获胜
if (checkWin(row, col, currentPlayer)) {
isGameOver = true;
long gameTime = System.currentTimeMillis() - startTime;
double winRate = (double) winCount / gameCount * 100;
JOptionPane.showMessageDialog(this, "玩家 " + currentPlayer + " 获胜!\n本局用时:" + gameTime / 1000.0 + "秒\n对战次数:" + gameCount + " 胜率:" + String.format("%.2f", winRate) + "%", "游戏结束", JOptionPane.INFORMATION_MESSAGE);
winCount++;
gameCount++;
winLabel.setText("对战次数:" + gameCount + " 胜率:" + String.format("%.2f", winRate) + "%");
resetBoard();
} else {
// 切换玩家
currentPlayer = currentPlayer == 1 ? 2 : 1;
}
}
// 检查是否有玩家获胜
private boolean checkWin(int row, int col, int player) {
// TODO: 实现检查是否有玩家获胜的算法
return false;
}
public static void main(String[] args) {
new FiveChessGame();
}
}
```
这个示例代码演示了如何使用Java Swing库创建一个五子棋游戏,并实现了选择人机对战或人人对战,统计一局棋所需时间,统计对战次数以及胜率的功能。
阅读全文