破碎的游戏循环

时间:2016-03-03 02:41:08

标签: java loops logic

我是GUI的新手。 我不确定如何使我的游戏循环更新,以便在determineWinner()更新时调用方法if (game.userPlayString != null) { game.determineWinner(); } 。我尝试制作一个看起来像这样的if语句:

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;


public class RPSSL implements ActionListener{
    public String userPlayString;
    public String computerPlayString;

    public int c;
    public int p;

    public JButton rockButton = new JButton("Rock");
    public JButton paperButton = new JButton("Paper");
    public JButton scissorsButton = new JButton("Scissors");
    public JButton spockButton = new JButton("Spock");
    public JButton lizardButton = new JButton("Lizard");

    public JPanel panel = new JPanel();

public RPSSL() {
    userPlayString = null;
    computerPlayString = null;
    c = 0;
    p = 0;
}

public void actionPerformed(ActionEvent e) {
    Object src = e.getSource();

    if (src == rockButton) {
        userPlayString = "Rock";
        p = 1;
    }
    else if (src == paperButton) {
        userPlayString = "Paper";
        p = 1;
    }
    else if (src == scissorsButton) {
        userPlayString = "Scissors";
        p = 1;
    }
    else if (src == spockButton) {
        userPlayString = "Spock";
        p = 1;
    }
    else if (src == lizardButton) {
        userPlayString = "Lizard";
        p = 1;
    }

}

public void createJFrame() {
    JFrame frame = new JFrame("Rock Paper Scissors Spock Lizard");
    panel.setLayout(null);

    panel.add(rockButton);
    rockButton.setBounds(0, 0, 100, 30);
    rockButton.addActionListener(this);

    panel.add(paperButton);
    paperButton.setBounds(0,30,100,30);
    paperButton.addActionListener(this);

    panel.add(scissorsButton);
    scissorsButton.setBounds(0,60,100,30);
    scissorsButton.addActionListener(this);

    panel.add(spockButton);
    spockButton.setBounds(0,90,100,30);
    spockButton.addActionListener(this);

    panel.add(lizardButton);
    lizardButton.setBounds(0,120,100,30);
    lizardButton.addActionListener(this);



    frame.add(panel);
    frame.setVisible(true);
    frame.setSize(30,200);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

public void computerPlay() {

    Random randomGenerator = new Random();
    int computerPlay = randomGenerator.nextInt(5);
    if (computerPlay == 0){
        computerPlayString = "Rock";
    }
    else if (computerPlay == 1) {
        computerPlayString = "Paper";
    }
    else if (computerPlay == 2) {
        computerPlayString = "Scissors";
    }
    else if (computerPlay == 3) {
        computerPlayString = "Spock";
    }
    else if (computerPlay == 4) {
        computerPlayString = "Lizard";
    }

    System.out.println("Computer played: " +computerPlayString);

}

public void humanWins() {
    System.out.print("You win!");
}

public void computerWins() {
    System.out.print("You lose!");
}

public void tie() {
    System.out.print("It's a tie!");
}

public void determineWinner() {
    //Determines who wins
    if (userPlayString.equalsIgnoreCase("rock")) {
        if (computerPlayString.equalsIgnoreCase("rock")) tie();
        else if (computerPlayString.equalsIgnoreCase("scissors")) humanWins();
        else if (computerPlayString.equalsIgnoreCase("paper")) computerWins();
        else if (computerPlayString.equalsIgnoreCase("spock")) humanWins();
        else if (computerPlayString.equalsIgnoreCase("lizard")) computerWins();
    }

    else if (userPlayString.equalsIgnoreCase("paper")) {
        if (computerPlayString.equalsIgnoreCase("rock")) humanWins();
        else if (computerPlayString.equalsIgnoreCase("scissors")) computerWins();
        else if (computerPlayString.equalsIgnoreCase("paper"))tie();
        else if (computerPlayString.equalsIgnoreCase("spock")) humanWins();
        else if (computerPlayString.equalsIgnoreCase("lizard")) computerWins();
    }

    else if (userPlayString.equalsIgnoreCase("scissors")) {
        if (computerPlayString.equalsIgnoreCase("rock")) computerWins();
        else if (computerPlayString.equalsIgnoreCase("scissors")) tie();
        else if (computerPlayString.equalsIgnoreCase("paper")) humanWins();
        else if (computerPlayString.equalsIgnoreCase("spock")) computerWins();
        else if (computerPlayString.equalsIgnoreCase("lizard")) humanWins();
    }

    else if (userPlayString.equalsIgnoreCase("spock")) {
        if (computerPlayString.equalsIgnoreCase("rock")) computerWins();
        else if (computerPlayString.equalsIgnoreCase("scissors")) humanWins();
        else if (computerPlayString.equalsIgnoreCase("paper")) computerWins();
        else if (computerPlayString.equalsIgnoreCase("spock")) tie();
        else if (computerPlayString.equalsIgnoreCase("lizard")) humanWins();
    }

    else if (userPlayString.equalsIgnoreCase("lizard")) {
        if (computerPlayString.equalsIgnoreCase("rock")) humanWins();
        else if (computerPlayString.equalsIgnoreCase("scissors")) computerWins();
        else if (computerPlayString.equalsIgnoreCase("paper")) humanWins();
        else if (computerPlayString.equalsIgnoreCase("spock")) computerWins();
        else if (computerPlayString.equalsIgnoreCase("lizard")) tie();
    }
}
public static void main(String[] args) {
    RPSSL game = new RPSSL();
    game.createJFrame();
    while(game.c < 1){
        if (game.userPlayString != null) {
            game.determineWinner();
        }
    }
}

}

我认为它应该执行,但没有任何反应。这是我到目前为止的代码。谢谢你的帮助。

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at main.RPSSL.determineWinner(RPSSL.java:126)
at main.RPSSL.actionPerformed(RPSSL.java:34)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2022)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2346)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
at java.awt.Component.processMouseEvent(Component.java:6525)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3321)
at java.awt.Component.processEvent(Component.java:6290)
at java.awt.Container.processEvent(Container.java:2234)
at java.awt.Component.dispatchEventImpl(Component.java:4881)
at java.awt.Container.dispatchEventImpl(Container.java:2292)
at java.awt.Component.dispatchEvent(Component.java:4703)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4898)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4533)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4462)
at java.awt.Container.dispatchEventImpl(Container.java:2278)
at java.awt.Window.dispatchEventImpl(Window.java:2739)
at java.awt.Component.dispatchEvent(Component.java:4703)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:746)
at java.awt.EventQueue.access$400(EventQueue.java:97)
at java.awt.EventQueue$3.run(EventQueue.java:697)
at java.awt.EventQueue$3.run(EventQueue.java:691)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:75)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:86)
at java.awt.EventQueue$4.run(EventQueue.java:719)
at java.awt.EventQueue$4.run(EventQueue.java:717)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:75)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:716)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)

错误:

SELECT parties_name, birthday
    FROM birthdays
    WHERE birthday >= UTC_DATE
    ORDER BY birthday ASC

UNION

SELECT parties_name, birthday
    FROM birthdays
    WHERE birthday < UTC_DATE
    ORDER BY birthday ASC   

1 个答案:

答案 0 :(得分:1)

我对您的代码进行了一些更改。在determineWinner()函数中,我添加了对computerPlay()函数的调用。以前从未调用过computerPlay()函数。我还在if语句中添加了game.c变量的增量,以防止无限循环。此外,为了防止CPU过度工作,我已经同步了main方法,抛出了InterruptedException,并在while循环的每次迭代中实现了一个小暂停。没有这样做,我无法完成这项工作。该程序可以正确调用determineWinner()函数,允许用户玩游戏。

将您的代码与此工作示例进行比较。

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class RPSSL implements ActionListener
{
  public String userPlayString;
  public String computerPlayString;

  public int c;
  public int p;

  public JButton rockButton = new JButton("Rock");
  public JButton paperButton = new JButton("Paper");
  public JButton scissorsButton = new JButton("Scissors");
  public JButton spockButton = new JButton("Spock");
  public JButton lizardButton = new JButton("Lizard");

  public JPanel panel = new JPanel();

  public static synchronized void main(String[] args) throws InterruptedException
  {
    RPSSL game = new RPSSL();
    game.createJFrame();
    while(game.c < 1)
    { 
      Thread.sleep(10);
      if (game.userPlayString != null) 
      {
        game.determineWinner();
        game.c++;
      }
    }
  }

  public RPSSL() 
  {
    userPlayString = null;
    computerPlayString = null;
    c = 0;
    p = 0;
  }

  public void actionPerformed(ActionEvent e) 
  {
    Object src = e.getSource();

    if (src == rockButton) 
    {
      userPlayString = "Rock";
      p = 1;
    }
    else if (src == paperButton) 
    {
      userPlayString = "Paper";
      p = 1;
    }
    else if (src == scissorsButton) 
    {
      userPlayString = "Scissors";
      p = 1;
    }
    else if (src == spockButton) 
    {
      userPlayString = "Spock";
      p = 1;
    }
    else if (src == lizardButton) 
    {
      userPlayString = "Lizard";
      p = 1;
    }
  }

  public void createJFrame() 
  {
    JFrame frame = new JFrame("Rock Paper Scissors Spock Lizard");
    panel.setLayout(null);

    panel.add(rockButton);
    rockButton.setBounds(0, 0, 100, 30);
    rockButton.addActionListener(this);

    panel.add(paperButton);
    paperButton.setBounds(0,30,100,30);
    paperButton.addActionListener(this);

    panel.add(scissorsButton);
    scissorsButton.setBounds(0,60,100,30);
    scissorsButton.addActionListener(this);

    panel.add(spockButton);
    spockButton.setBounds(0,90,100,30);
    spockButton.addActionListener(this);

    panel.add(lizardButton);
    lizardButton.setBounds(0,120,100,30);
    lizardButton.addActionListener(this);



    frame.add(panel);
    frame.setVisible(true);
    frame.setSize(30,200);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  }

  public void computerPlay() 
  {
    Random randomGenerator = new Random();
    int computerPlay = randomGenerator.nextInt(5);
    if (computerPlay == 0)
    {
      computerPlayString = "Rock";
    }
    else if (computerPlay == 1) 
    {
        computerPlayString = "Paper";
    }
    else if (computerPlay == 2) 
    {
        computerPlayString = "Scissors";
    }
    else if (computerPlay == 3) 
    {
        computerPlayString = "Spock";
    }
    else if (computerPlay == 4) 
    {
        computerPlayString = "Lizard";
    }

    System.out.println("Computer played: " + computerPlayString);

  }

  public void humanWins() 
  {
    System.out.print("You win!");
  }

  public void computerWins() 
  {
    System.out.print("You lose!");
  }

  public void tie() 
  {
    System.out.print("It's a tie!");
  }

  public void determineWinner() 
  {
    computerPlay();
    //Determines who wins
    if (userPlayString.equalsIgnoreCase("rock")) 
    {
        if (computerPlayString.equalsIgnoreCase("rock")) tie();
        else if (computerPlayString.equalsIgnoreCase("scissors")) humanWins();
        else if (computerPlayString.equalsIgnoreCase("paper")) computerWins();
        else if (computerPlayString.equalsIgnoreCase("spock")) humanWins();
        else if (computerPlayString.equalsIgnoreCase("lizard")) computerWins();
    }

    else if (userPlayString.equalsIgnoreCase("paper")) 
    {
        if (computerPlayString.equalsIgnoreCase("rock")) humanWins();
        else if (computerPlayString.equalsIgnoreCase("scissors")) computerWins();
        else if (computerPlayString.equalsIgnoreCase("paper"))tie();
        else if (computerPlayString.equalsIgnoreCase("spock")) humanWins();
        else if (computerPlayString.equalsIgnoreCase("lizard")) computerWins();
    }

    else if (userPlayString.equalsIgnoreCase("scissors")) 
    {
        if (computerPlayString.equalsIgnoreCase("rock")) computerWins();
        else if (computerPlayString.equalsIgnoreCase("scissors")) tie();
        else if (computerPlayString.equalsIgnoreCase("paper")) humanWins();
        else if (computerPlayString.equalsIgnoreCase("spock")) computerWins();
        else if (computerPlayString.equalsIgnoreCase("lizard")) humanWins();
    }

    else if (userPlayString.equalsIgnoreCase("spock")) 
    {
        if (computerPlayString.equalsIgnoreCase("rock")) computerWins();
        else if (computerPlayString.equalsIgnoreCase("scissors")) humanWins();
        else if (computerPlayString.equalsIgnoreCase("paper")) computerWins();
        else if (computerPlayString.equalsIgnoreCase("spock")) tie();
        else if (computerPlayString.equalsIgnoreCase("lizard")) humanWins();
    }

    else if (userPlayString.equalsIgnoreCase("lizard")) 
    {
        if (computerPlayString.equalsIgnoreCase("rock")) humanWins();
        else if (computerPlayString.equalsIgnoreCase("scissors")) computerWins();
        else if (computerPlayString.equalsIgnoreCase("paper")) humanWins();
        else if (computerPlayString.equalsIgnoreCase("spock")) computerWins();
        else if (computerPlayString.equalsIgnoreCase("lizard")) tie();
    }
  }
}