请用Java写一个石头剪刀布游戏,电脑和人玩剪刀石头布游戏,并且能显示游戏结果,比如人出了石头,电脑出了剪子, 则显示人赢了,否则显示电脑赢了,或者平局。比赛次数至少为5次,将每一局的游戏结果写入 result.txt文件。程序必须使用类的定义、GUI界面设计、文件IO、集合知识
时间: 2024-02-19 11:58:32 浏览: 156
好的,我可以帮您完成这个任务。首先,让我们来定义一些类:
1. 游戏类 Game:包含游戏的逻辑和规则,如判断胜负、游戏次数等。
2. 玩家类 Player:包含玩家的名称和出拳的方法。
3. 电脑类 Computer:继承自玩家类,实现电脑的出拳方法。
4. 记录类 Record:用于记录每一局游戏的结果。
接下来,我们可以使用Java的Swing库来设计GUI界面。在界面上,我们需要添加一些按钮和标签,如“石头”、“剪刀”、“布”按钮,以及用于显示游戏结果的标签。
在游戏开始后,每一局游戏的结果都需要保存到result.txt文件中。我们可以使用Java的文件IO操作来实现。
最后,我们可以使用Java的集合知识来管理游戏记录。我们可以创建一个ArrayList来存储每一局游戏的结果,然后在游戏结束后将其写入result.txt文件中。
下面是一个简单的实现示例,仅供参考:
```java
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.*;
import javax.swing.*;
public class GameGUI extends JFrame implements ActionListener {
private static final long serialVersionUID = 1L;
private JLabel resultLabel;
private JButton rockButton, paperButton, scissorButton;
private int gameCount;
private ArrayList<Record> recordList;
public GameGUI() {
super("石头剪刀布游戏");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// 初始化界面
JPanel panel = new JPanel(new GridLayout(4, 1));
resultLabel = new JLabel("请出拳:");
rockButton = new JButton("石头");
paperButton = new JButton("剪刀");
scissorButton = new JButton("布");
rockButton.addActionListener(this);
paperButton.addActionListener(this);
scissorButton.addActionListener(this);
panel.add(resultLabel);
panel.add(rockButton);
panel.add(paperButton);
panel.add(scissorButton);
getContentPane().add(panel);
pack();
// 初始化游戏
gameCount = 0;
recordList = new ArrayList<Record>();
}
public void actionPerformed(ActionEvent e) {
Player player = new Player("玩家");
Computer computer = new Computer("电脑");
String playerAction = "", computerAction = "";
// 判断玩家出拳
if (e.getSource() == rockButton) {
playerAction = "石头";
} else if (e.getSource() == paperButton) {
playerAction = "剪刀";
} else if (e.getSource() == scissorButton) {
playerAction = "布";
}
// 电脑随机出拳
computerAction = computer.getAction();
// 判断胜负
int result = Game.getResult(playerAction, computerAction);
// 显示结果
String resultString = "";
if (result == 1) {
resultString = "玩家赢了!";
} else if (result == -1) {
resultString = "电脑赢了!";
} else {
resultString = "平局!";
}
resultLabel.setText("你出了" + playerAction + ",电脑出了" + computerAction + "," + resultString);
// 记录游戏结果
Record record = new Record();
record.setGameCount(++gameCount);
record.setPlayerAction(playerAction);
record.setComputerAction(computerAction);
record.setResult(resultString);
recordList.add(record);
// 判断游戏是否结束
if (gameCount >= 5) {
// 写入游戏记录
try {
FileWriter fw = new FileWriter("result.txt");
for (Record r : recordList) {
fw.write(r.toString() + "\n");
}
fw.close();
} catch (IOException ex) {
ex.printStackTrace();
}
// 显示最终结果
int playerWinCount = 0, computerWinCount = 0;
for (Record r : recordList) {
if (r.getResult().equals("玩家赢了!")) {
playerWinCount++;
} else if (r.getResult().equals("电脑赢了!")) {
computerWinCount++;
}
}
String finalResult = "";
if (playerWinCount > computerWinCount) {
finalResult = "你赢了!";
} else if (playerWinCount < computerWinCount) {
finalResult = "电脑赢了!";
} else {
finalResult = "平局!";
}
JOptionPane.showMessageDialog(this, "游戏结束," + finalResult, "游戏结果", JOptionPane.INFORMATION_MESSAGE);
System.exit(0);
}
}
public static void main(String[] args) {
GameGUI game = new GameGUI();
game.setVisible(true);
}
}
class Game {
public static int getResult(String playerAction, String computerAction) {
if (playerAction.equals(computerAction)) {
return 0;
} else if (playerAction.equals("石头") && computerAction.equals("剪刀")) {
return 1;
} else if (playerAction.equals("剪刀") && computerAction.equals("布")) {
return 1;
} else if (playerAction.equals("布") && computerAction.equals("石头")) {
return 1;
} else {
return -1;
}
}
}
class Player {
private String name;
public Player(String name) {
this.name = name;
}
public String getName() {
return name;
}
public String getAction() {
return "";
}
}
class Computer extends Player {
public Computer(String name) {
super(name);
}
public String getAction() {
Random random = new Random();
int n = random.nextInt(3);
if (n == 0) {
return "石头";
} else if (n == 1) {
return "剪刀";
} else {
return "布";
}
}
}
class Record {
private int gameCount;
private String playerAction;
private String computerAction;
private String result;
public int getGameCount() {
return gameCount;
}
public void setGameCount(int gameCount) {
this.gameCount = gameCount;
}
public String getPlayerAction() {
return playerAction;
}
public void setPlayerAction(String playerAction) {
this.playerAction = playerAction;
}
public String getComputerAction() {
return computerAction;
}
public void setComputerAction(String computerAction) {
this.computerAction = computerAction;
}
public String getResult() {
return result;
}
public void setResult(String result) {
this.result = result;
}
public String toString() {
return gameCount + "\t" + playerAction + "\t" + computerAction + "\t" + result;
}
}
```
这个示例代码实现了一个简单的石头剪刀布游戏,使用了类的定义、GUI界面设计、文件IO、集合知识等多种Java知识。在游戏开始后,每一局游戏的结果都会保存到result.txt文件中,游戏结束后将显示最终结果。
阅读全文