游戏合并计算机选择数学随机类



我正在尝试用Java设计一个带有gui的石头剪刀布游戏。 我已经在线阅读了许多不同的教程以及这里提出的教程,但没有一个真正遇到我遇到的问题,所以我希望有人可以帮助我。 我一切正常(现在很丑;一旦游戏实际运行,我会让它看起来更好)。我有三个问题,除了一个问题之外,其他问题都很小。 首先,我无法显示用户和计算机标签。 第二个问题是我该如何让游戏说"你选择了"和"电脑选择了"?最后,如何合并计算机以选择选项? 我意识到我将不得不使用数学课和随机课,但我如何让计算机真正选择它的选择?

这是我的驱动程序类:

import javax.swing.JFrame;

public class GameMain {
    public static void main(String[] args) {
        JFrame frame = new JFrame ("Rock Paper Scissors");
        GamePanel panel = new GamePanel();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().add(panel);
        frame.pack();
        frame.setVisible(true);
    }
}

这是我的另一门课,其中包括我在没有计算机做出选择的情况下进行选择的实际想法。 任何提示和帮助我将不胜感激。 我感谢你所做的一切。再次,谢谢。

import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;

 public class GamePanel extends JPanel{
     private JLabel userLabel, computerLabel, resultLabel, winLabel, tieLabel, loseLabel;
     private JButton rockButton, paperButton, scissorsButton;
     private int winInt, tieInt, loseInt;
  public GamePanel(){
      winInt = 0;
      tieInt= 0;
      loseInt = 0;
      rockButton = new JButton("Rock");
      paperButton = new JButton("Paper");
      scissorsButton = new JButton("Scissors");
      //action listeners
      rockButton.addActionListener(new ButtonListener());
      paperButton.addActionListener(new ButtonListener());
      scissorsButton.addActionListener(new ButtonListener());
      add(rockButton);
      add(paperButton);
      add(scissorsButton);
      setBackground(Color.BLUE.darker());
      setPreferredSize(new Dimension(300, 150));
  }
  private class ButtonListener implements ActionListener {
      public void actionPerformed(ActionEvent ae){
          Object userSource = ae.getSource();
          Object computerSource = ae.getSource();
          if (userSource == rockButton && computerSource == scissorsButton){
              winInt++;
          }else if (userSource == paperButton && computerSource == rockButton){
              winInt++;
          }else if (userSource == scissorsButton && computerSource == paperButton){
              winInt++;
          }else  if (computerSource == rockButton && userSource == scissorsButton){
              loseInt++;
          }else if (computerSource == paperButton && userSource == rockButton){
              loseInt++;
          }else if (computerSource == scissorsButton && userSource == paperButton){
              loseInt++;
          }else{
              tieInt++;
          }
      }
  }
}

这就是您可以为计算机播放器获得随机选择的方式。

public String getRandomChoice(){
        double d = Math.random();
        if(d < .33){
            return "ROCK";
        }
        else if(d < .66){
            return "PAPER";
        }
        else{
            return "SCISSORS";
        }
    }

尝试此链接以获取有关如何在 Java 中为 RPS 游戏编写 GUI 的一些信息。 http://staticvoidgames.com/tutorials/swing/rockPaperScissors

最新更新