It looks like you're using an Ad Blocker.

Please white-list or disable AboveTopSecret.com in your ad-blocking tool.

Thank you.

 

Some features of ATS will be disabled while you continue to use an ad-blocker.

 

Rock Paper Scissors GUI Help

page: 1
1

log in

join
share:

posted on Nov, 14 2014 @ 10:16 AM
link   
So I am in the process of making a rock paper scissors gui game. I have everything (aside from it being ugly atm, I'll pretty it up once it is actually working lol) anyway; I have a few problems. First problem is how would I incorporate the computers choice? I know it will be from the math class and will be random, but as you will see in my code, how would I actually do it? I'm a but lost now. Also, how would I display the win lose draw and what was played? I have the ints (as you will see) but they are not displaying atm I'm not sure why and cannot seem to figure it out. I've been working on this multiple different times over the course of this past week, referencing my book, online, and a bought another book but to no avail. I must be missing something. I looked at some premade ones online, but they are not working for me -_- so I figured I would ask you guys. So here is my code. Thank you for all who help me.


Driver Class:



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);
]

]



Other file (panel and such)



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



posted on Nov, 14 2014 @ 11:56 AM
link   
a reply to: wwe9112

First you need to define your trigger event.

Not seeing your gui it's hard to know how a user selects his choice and submits.

Figure that out and then you can work out how to integrate both choices together.

check out this RPS.

www.nytimes.com...



posted on Nov, 14 2014 @ 12:02 PM
link   
wwe9112

Looks like your missing commands on this level

String ac = ae.getActionCommand


Goodluck



posted on Nov, 14 2014 @ 12:14 PM
link   
a reply to: grey580

As of right now its just three buttons. Youre supposed to be able to click say rock and the PC picks say scissors. The screen will say what the user picked and what the computer picked and increase the count by one so for this example wins would go from 0 to 1.



new topics

top topics
 
1

log in

join