Reputation: 91
I'm new to GUI.
I'm not really sure how to make my game loop update so that when userPlayString
updates, the method determineWinner()
is called. I tried to make an if statement that looked like this:
if (game.userPlayString != null) {
game.determineWinner();
}
I thought it should execute, but nothing happens.This is the code I have so far. Thanks for the help.
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();
}
}
}
}
Errors:
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)
Upvotes: 1
Views: 94
Reputation: 339
I've made a few changes to your code. Within the determineWinner() function, I added a call to the computerPlay() function. Previously the computerPlay() function was never called. I also added an incrementation of the game.c variable within the if-statement to prevent an infinite loop. Furthermore, to prevent overworking the CPU, I have synchronized your main method, thrown the InterruptedException, and implemented a small pause within each iteration of your while loop. I was unable to make this work without doing so. This program does call the determineWinner() function correctly, allowing the user to play the game.
Compare your code to this working example.
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();
}
}
}
Upvotes: 1