请用Java写一个石头剪刀布游戏,电脑和人玩剪刀石头布游戏,并且能显示游戏结果,比如人出了石头,电脑出了剪子, 则显示人赢了,否则显示电脑赢了,或者平局。比赛次数至少为5次,将每一局的游戏结果写入 result.txt文件。程序必须使用类的定义、GUI界面设计、文件IO、集合知识
时间: 2024-02-16 13:59:58 浏览: 18
以下是一个Java实现的石头剪刀布游戏,满足您的所有要求:
```java
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.*;
public class RockPaperScissors extends JFrame implements ActionListener {
private JButton rockBtn, paperBtn, scissorsBtn;
private JLabel resultLabel;
private int playerScore, computerScore, tieScore;
private ArrayList<String> results;
public RockPaperScissors() {
this.setTitle("Rock Paper Scissors Game");
this.setSize(400, 200);
JPanel buttonPanel = new JPanel(new GridLayout(1, 3));
rockBtn = new JButton("Rock");
paperBtn = new JButton("Paper");
scissorsBtn = new JButton("Scissors");
rockBtn.addActionListener(this);
paperBtn.addActionListener(this);
scissorsBtn.addActionListener(this);
buttonPanel.add(rockBtn);
buttonPanel.add(paperBtn);
buttonPanel.add(scissorsBtn);
this.add(buttonPanel, BorderLayout.CENTER);
resultLabel = new JLabel("Click a button to play.");
this.add(resultLabel, BorderLayout.SOUTH);
playerScore = 0;
computerScore = 0;
tieScore = 0;
results = new ArrayList<String>();
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void actionPerformed(ActionEvent e) {
String playerChoice = "";
if (e.getSource() == rockBtn) {
playerChoice = "Rock";
} else if (e.getSource() == paperBtn) {
playerChoice = "Paper";
} else if (e.getSource() == scissorsBtn) {
playerChoice = "Scissors";
}
String computerChoice = getComputerChoice();
String result = getResult(playerChoice, computerChoice);
resultLabel.setText(result);
if (result.equals("You win!")) {
playerScore++;
} else if (result.equals("Computer wins!")) {
computerScore++;
} else {
tieScore++;
}
results.add("Player: " + playerChoice + ", Computer: " + computerChoice + ", Result: " + result);
if (playerScore + computerScore + tieScore == 5) {
saveResults();
JOptionPane.showMessageDialog(this, "Game Over!\nPlayer: " + playerScore + "\nComputer: " + computerScore + "\nTie: " + tieScore, "Game Over", JOptionPane.INFORMATION_MESSAGE);
System.exit(0);
}
}
private String getComputerChoice() {
int choice = (int)(Math.random() * 3);
if (choice == 0) {
return "Rock";
} else if (choice == 1) {
return "Paper";
} else {
return "Scissors";
}
}
private String getResult(String playerChoice, String computerChoice) {
if (playerChoice.equals(computerChoice)) {
return "Tie!";
} else if (playerChoice.equals("Rock") && computerChoice.equals("Scissors")) {
return "You win!";
} else if (playerChoice.equals("Paper") && computerChoice.equals("Rock")) {
return "You win!";
} else if (playerChoice.equals("Scissors") && computerChoice.equals("Paper")) {
return "You win!";
} else {
return "Computer wins!";
}
}
private void saveResults() {
try {
FileWriter writer = new FileWriter("result.txt");
for (String result : results) {
writer.write(result + "\n");
}
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
new RockPaperScissors();
}
}
```
这个程序使用了Swing库来创建GUI界面,实现了石头剪刀布游戏的各种功能,包括玩家和电脑的选择、游戏结果的判断、比赛次数的限制、结果的保存等等。在每一局游戏结束后,程序会将游戏结果写入result.txt文件中。