游戏包含计算机选择数学随机类

时间:2014-11-15 16:26:29

标签: java user-interface artificial-intelligence

我正在尝试用Java中的gui设计一个Rock Paper Scissors游戏。我已经在网上阅读了很多不同的教程,这里有人问过,但是我没有遇到任何问题,所以我希望有人可以帮助我。我有一切工作(现在只是丑陋;一旦游戏实际运作,我会让它看起来更好)。我有三个问题,除了一个之外都很小。首先,我无法显示用户和计算机标签。第二个问题是我将如何实际让游戏说“你选择了#34;并且"计算机被选中"?最后,我如何让计算机选择一个选择?我意识到我将不得不使用数学课和随机但我如何让计算机实际选择它的'选择?

这是我的驱动程序类:

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++;
          }
      }
  }
}

1 个答案:

答案 0 :(得分:0)

这就是为计算机播放器随机选择的方法。

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