java创建的PairOfDice类,设计并实现一个类来玩Pig游戏,由用户和计算机进行比赛,每一轮中,每一个玩家掷一次骰子(一对)并且累计点数。最终目标是先达到100点的对手获胜。在任何一轮比赛中,如果玩家掷到一个1,则该轮的点数无效并且换对手掷骰子,而如果掷得两个1,则到目前为止所有的点数全部无效并且转交骰子控制权。每次掷完骰子后,当前的玩家可以自由选择继续掷(成为Pig)并且担当失去点子的风险,或者将骰子的控制权移交给对手!,但可能使对手赢得比赛。如果计算机在任何一轮中累计点数超过20则放弃骰子的控制权。 程序要求:(1)用可视化编程控件实现该程序。(2)用户每次掷完骰子后,弹出对话框,询问用户是否继续掷骰子。(3)显示计算机每次掷骰子的值,每轮累加超过20后,计算机自动放弃控制骰子权限。
时间: 2023-11-28 09:51:12 浏览: 37
下面是一个可能的实现:
```java
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.SwingConstants;
import java.awt.Font;
import java.awt.event.ActionListener;
import java.util.Random;
import java.awt.event.ActionEvent;
public class PigGame extends JFrame {
private JPanel contentPane;
private JLabel lblPlayerScore;
private JLabel lblComputerScore;
private PairOfDice dice;
private int playerScore;
private int computerScore;
private Random random;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
PigGame frame = new PigGame();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public PigGame() {
setTitle("Pig Game");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JButton btnRoll = new JButton("Roll");
btnRoll.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
roll();
}
});
btnRoll.setBounds(164, 164, 117, 29);
contentPane.add(btnRoll);
JLabel lblPlayer = new JLabel("Player");
lblPlayer.setFont(new Font("Lucida Grande", Font.BOLD, 16));
lblPlayer.setHorizontalAlignment(SwingConstants.CENTER);
lblPlayer.setBounds(65, 28, 117, 21);
contentPane.add(lblPlayer);
JLabel lblComputer = new JLabel("Computer");
lblComputer.setFont(new Font("Lucida Grande", Font.BOLD, 16));
lblComputer.setHorizontalAlignment(SwingConstants.CENTER);
lblComputer.setBounds(265, 28, 117, 21);
contentPane.add(lblComputer);
lblPlayerScore = new JLabel("0");
lblPlayerScore.setHorizontalAlignment(SwingConstants.CENTER);
lblPlayerScore.setFont(new Font("Lucida Grande", Font.PLAIN, 48));
lblPlayerScore.setBounds(65, 61, 117, 72);
contentPane.add(lblPlayerScore);
lblComputerScore = new JLabel("0");
lblComputerScore.setHorizontalAlignment(SwingConstants.CENTER);
lblComputerScore.setFont(new Font("Lucida Grande", Font.PLAIN, 48));
lblComputerScore.setBounds(265, 61, 117, 72);
contentPane.add(lblComputerScore);
dice = new PairOfDice();
random = new Random();
}
private void roll() {
int playerRoll = dice.roll();
int computerRoll = dice.roll();
playerScore += playerRoll;
computerScore += computerRoll;
lblPlayerScore.setText(Integer.toString(playerScore));
lblComputerScore.setText(Integer.toString(computerScore));
if (playerRoll == 1) {
JOptionPane.showMessageDialog(this, "You rolled a 1, your turn is over.");
computerTurn();
} else if (playerRoll == 2) {
JOptionPane.showMessageDialog(this, "You rolled two 1s, all your points are lost and your turn is over.");
playerScore = 0;
computerScore = 0;
lblPlayerScore.setText("0");
lblComputerScore.setText("0");
computerTurn();
} else {
int choice = JOptionPane.showConfirmDialog(this, "You rolled a " + playerRoll + ". Roll again?", "Roll Again?", JOptionPane.YES_NO_OPTION);
if (choice == JOptionPane.YES_OPTION) {
roll();
} else {
computerTurn();
}
}
}
private void computerTurn() {
while (computerScore < 100 && (computerScore < 20 || random.nextBoolean())) {
int computerRoll = dice.roll();
computerScore += computerRoll;
lblComputerScore.setText(Integer.toString(computerScore));
if (computerRoll == 1) {
JOptionPane.showMessageDialog(this, "Computer rolled a 1, your turn.");
return;
} else if (computerRoll == 2) {
JOptionPane.showMessageDialog(this, "Computer rolled two 1s, all its points are lost and your turn.");
playerScore = 0;
computerScore = 0;
lblPlayerScore.setText("0");
lblComputerScore.setText("0");
return;
}
}
if (computerScore >= 100) {
JOptionPane.showMessageDialog(this, "Computer wins!");
}
}
}
class PairOfDice {
private int die1;
private int die2;
private Random random;
public PairOfDice() {
random = new Random();
roll();
}
public int getDie1() {
return die1;
}
public int getDie2() {
return die2;
}
public int roll() {
die1 = random.nextInt(6) + 1;
die2 = random.nextInt(6) + 1;
return die1 + die2;
}
}
```
这个程序使用了 Java Swing 库中的一些控件来实现可视化界面,包括 JFrame、JPanel、JLabel、JButton 等。在初始化时,创建了一个 PairOfDice 对象来模拟一对骰子,以及一个 Random 对象来模拟计算机的随机决策。
在掷骰子时,先掷出一个随机数表示点数,然后根据点数更新当前玩家的累计分数,并且更新界面上的分数显示。如果点数为 1,则当前玩家的分数清零,并且将掷骰子的权限转交给对手。如果点数为 2,则当前玩家的分数和对手的分数全部清零,并且将掷骰子的权限转交给对手。如果点数为其它值,则弹出一个对话框询问玩家是否继续掷骰子,根据玩家的选择决定是否继续掷骰子或者将掷骰子的权限转交给对手。
在计算机的回合中,计算机会根据当前的分数情况来决定是否继续掷骰子。如果分数超过了 20 或者已经达到了 100 分,则计算机放弃掷骰子的权限。否则,计算机随机决定是否继续掷骰子,并且更新分数和界面显示。如果计算机掷出了 1 或者 2,则将掷骰子的权限转交给玩家。如果计算机已经达到了 100 分,则弹出一个对话框表示计算机获胜。