修改后的代码如下:
import java.awt.Color; import java.awt.Container; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JButton; import javax.swing.JTextField; import javax.swing.ButtonGroup; import javax.swing.JOptionPane; import javax.swing.JRadioButton;
public class Test1 extends JFrame implements ActionListener {
private static final long serialVersionUID = 920921981358099067L;
private JLabel lbPer, lbCom, lbLast;
private JButton btnStart, btnOne, btnTwo, btnThree;
private JTextField tfPcshow, tfLastshow;
private boolean win = true;
public Test1() {
super("拿火柴小游戏");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(600, 300);
setResizable(false);
initUI();
setLocationRelativeTo(null);
}
private void initUI() {
Container container = getContentPane();
ButtonGroup group = new ButtonGroup();
btnStart = new JButton("开始新游戏");
btnStart.setBounds(410, 50, 150, 30);
lbPer = new JLabel("人拿火柴数");
lbPer.setBounds(40, 50, 120, 30);
lbCom = new JLabel("计算机拿火柴数");
lbCom.setBounds(40, 100, 120, 30);
lbLast = new JLabel("剩余火柴数");
lbLast.setBounds(40, 150, 120, 30);
btnOne = new JButton("1");
btnOne.setBounds(210, 50, 80, 40);
btnTwo = new JButton("2");
btnTwo.setBounds(310, 50, 80, 40);
btnThree = new JButton("3");
btnThree.setBounds(410, 100, 80, 40);
tfPcshow = new JTextField();
tfPcshow.setBounds(210, 100, 150, 30);
tfLastshow = new JTextField();
tfLastshow.setBounds(210, 150, 150, 30);
btnStart.addActionListener(this);
btnOne.addActionListener(this);
btnTwo.addActionListener(this);
btnThree.addActionListener(this);
group.add(btnOne);
group.add(btnTwo);
group.add(btnThree);
container.setLayout(null);
container.add(lbPer);
container.add(lbCom);
container.add(lbLast);
container.add(btnStart);
container.add(btnOne);
container.add(btnTwo);
container.add(btnThree);
container.add(tfPcshow);
container.add(tfLastshow);
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == btnStart) {
int mode = JOptionPane.showOptionDialog(this,
"请选择游戏难度",
"游戏难度选择",
JOptionPane.YES_NO_OPTION,
JOptionPane.QUESTION_MESSAGE,
null,
new String[]{"简单", "普通"},
"简单");
if(mode == JOptionPane.YES_OPTION){
startNewGame(10); // 简单模式,初始火柴数为10
}else{
startNewGame(20); // 普通模式,初始火柴数为20
}
return;
}
int k = Integer.parseInt(((JButton)e.getSource()).getText());
int pc = computer(k); // 计算机拿的火柴数
if(sumMatch <= pc+k){ // 火柴数不够拿
JOptionPane.showMessageDialog(this, "火柴数不足,请重新选择", "错误", JOptionPane.ERROR_MESSAGE);
return;
}
sumMatch -= (pc + k);
if (sumMatch == 0) {
JOptionPane.showMessageDialog(this, "恭喜你赢了!", "游戏结束", JOptionPane.INFORMATION_MESSAGE);
startNewGame(sumMatch); // 胜利后开始新游戏
} else if (sumMatch < 0) {
JOptionPane.showMessageDialog(this, "请开始新游戏", "游戏结束", JOptionPane.INFORMATION_MESSAGE);
startNewGame(sumMatch); // 失败后开始新游戏
}
tfPcshow.setText("计算机拿了" + pc + "根");
tfLastshow.setText("火柴剩余" + sumMatch + "根");
if (!win) {
JOptionPane.showMessageDialog(this, "你输了,再试一次吧", "游戏结束", JOptionPane.INFORMATION_MESSAGE);
startNewGame(sumMatch); // 输了后开始新游戏
}
}
private void startNewGame(int matchCount){
sumMatch = matchCount;
tfPcshow.setText("");
tfLastshow.setText("");
}
private int computer(int k) {
int ret = 0;
if(k == 1){ // 如果人拿一根,则计算机随机拿1-3根
ret = (int) (Math.random() * (3 - 1)) + 1;
}else{ // 如果人拿两或三根,则计算机自动选择拿剩余的火柴数的一半
ret = (sumMatch - k) / 2;
}
if(ret < 1){ // 计算机至少拿一根火柴
ret = 1;
}else if(ret > 3){ // 计算机最多只能拿三根火柴
ret = 3;
}
return ret;
}
public static void main(String[] args) {
Test1 frame = new Test1();
frame.setVisible(true);
}
}