ZBLOG

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 java...

修改后的代码如下:

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 {

  1. private static final long serialVersionUID = 920921981358099067L;
  2. private JLabel lbPer, lbCom, lbLast;
  3. private JButton btnStart, btnOne, btnTwo, btnThree;
  4. private JTextField tfPcshow, tfLastshow;
  5. private boolean win = true;
  6. public Test1() {
  7. super("拿火柴小游戏");
  8. setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  9. setSize(600, 300);
  10. setResizable(false);
  11. initUI();
  12. setLocationRelativeTo(null);
  13. }
  14. private void initUI() {
  15. Container container = getContentPane();
  16. ButtonGroup group = new ButtonGroup();
  17. btnStart = new JButton("开始新游戏");
  18. btnStart.setBounds(410, 50, 150, 30);
  19. lbPer = new JLabel("人拿火柴数");
  20. lbPer.setBounds(40, 50, 120, 30);
  21. lbCom = new JLabel("计算机拿火柴数");
  22. lbCom.setBounds(40, 100, 120, 30);
  23. lbLast = new JLabel("剩余火柴数");
  24. lbLast.setBounds(40, 150, 120, 30);
  25. btnOne = new JButton("1");
  26. btnOne.setBounds(210, 50, 80, 40);
  27. btnTwo = new JButton("2");
  28. btnTwo.setBounds(310, 50, 80, 40);
  29. btnThree = new JButton("3");
  30. btnThree.setBounds(410, 100, 80, 40);
  31. tfPcshow = new JTextField();
  32. tfPcshow.setBounds(210, 100, 150, 30);
  33. tfLastshow = new JTextField();
  34. tfLastshow.setBounds(210, 150, 150, 30);
  35. btnStart.addActionListener(this);
  36. btnOne.addActionListener(this);
  37. btnTwo.addActionListener(this);
  38. btnThree.addActionListener(this);
  39. group.add(btnOne);
  40. group.add(btnTwo);
  41. group.add(btnThree);
  42. container.setLayout(null);
  43. container.add(lbPer);
  44. container.add(lbCom);
  45. container.add(lbLast);
  46. container.add(btnStart);
  47. container.add(btnOne);
  48. container.add(btnTwo);
  49. container.add(btnThree);
  50. container.add(tfPcshow);
  51. container.add(tfLastshow);
  52. }
  53. public void actionPerformed(ActionEvent e) {
  54. if (e.getSource() == btnStart) {
  55. int mode = JOptionPane.showOptionDialog(this,
  56. "请选择游戏难度",
  57. "游戏难度选择",
  58. JOptionPane.YES_NO_OPTION,
  59. JOptionPane.QUESTION_MESSAGE,
  60. null,
  61. new String[]{"简单", "普通"},
  62. "简单");
  63. if(mode == JOptionPane.YES_OPTION){
  64. startNewGame(10); // 简单模式,初始火柴数为10
  65. }else{
  66. startNewGame(20); // 普通模式,初始火柴数为20
  67. }
  68. return;
  69. }
  70. int k = Integer.parseInt(((JButton)e.getSource()).getText());
  71. int pc = computer(k); // 计算机拿的火柴数
  72. if(sumMatch <= pc+k){ // 火柴数不够拿
  73. JOptionPane.showMessageDialog(this, "火柴数不足,请重新选择", "错误", JOptionPane.ERROR_MESSAGE);
  74. return;
  75. }
  76. sumMatch -= (pc + k);
  77. if (sumMatch == 0) {
  78. JOptionPane.showMessageDialog(this, "恭喜你赢了!", "游戏结束", JOptionPane.INFORMATION_MESSAGE);
  79. startNewGame(sumMatch); // 胜利后开始新游戏
  80. } else if (sumMatch < 0) {
  81. JOptionPane.showMessageDialog(this, "请开始新游戏", "游戏结束", JOptionPane.INFORMATION_MESSAGE);
  82. startNewGame(sumMatch); // 失败后开始新游戏
  83. }
  84. tfPcshow.setText("计算机拿了" + pc + "根");
  85. tfLastshow.setText("火柴剩余" + sumMatch + "根");
  86. if (!win) {
  87. JOptionPane.showMessageDialog(this, "你输了,再试一次吧", "游戏结束", JOptionPane.INFORMATION_MESSAGE);
  88. startNewGame(sumMatch); // 输了后开始新游戏
  89. }
  90. }
  91. private void startNewGame(int matchCount){
  92. sumMatch = matchCount;
  93. tfPcshow.setText("");
  94. tfLastshow.setText("");
  95. }
  96. private int computer(int k) {
  97. int ret = 0;
  98. if(k == 1){ // 如果人拿一根,则计算机随机拿1-3根
  99. ret = (int) (Math.random() * (3 - 1)) + 1;
  100. }else{ // 如果人拿两或三根,则计算机自动选择拿剩余的火柴数的一半
  101. ret = (sumMatch - k) / 2;
  102. }
  103. if(ret < 1){ // 计算机至少拿一根火柴
  104. ret = 1;
  105. }else if(ret > 3){ // 计算机最多只能拿三根火柴
  106. ret = 3;
  107. }
  108. return ret;
  109. }
  110. public static void main(String[] args) {
  111. Test1 frame = new Test1();
  112. frame.setVisible(true);
  113. }

}

本站部分文章来源于网络,版权归原作者所有,如有侵权请联系站长删除。
转载请注明出处:https://golang.0voice.com/?id=3683

分享:
扫描分享到社交APP
上一篇
下一篇
发表列表
游客 游客
此处应有掌声~
评论列表

还没有评论,快来说点什么吧~

联系我们

在线咨询: 点击这里给我发消息

微信号:3007537140

上班时间: 10:30-22:30

关注我们
x

注册

已经有帐号?