Hey everyone, I’m trying to make a simple rock, paper, scissors game using JButtons and a simple random AI. I believe I have everything in order but the order of execution in my main. I’m trying to display the games options, wait for the user to select an option, next have the computer randomly select an option, then have the game compare the two choices then see who has won and display a score on the top of the window.
I think I need to tackle this problem with a nested while loops. I think I need to create a Boolean for my first while loop so that the program runs normally and then have my second while look wait for input from the user to execute the rest of the code. I’ve been stumped for some days so I thought I ask for help. If someone could point me the right direction I would be very appreciative!
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;
import javax.swing.*;
public class GamePanel implements ActionListener{
static int playersco = 0;
static int compsco = 0;
static int player = 0;
static int comp;
String playerroc, playersis, playerpap ;
static String compprint, comproc, comppap, compsis;
JButton roc, pap, sis;
JPanel selectpanel, gamtex, compout;
static JLabel playerscore;
JLabel compscore;
JLabel compchoice;
public JPanel ContentPane(){
JPanel masterPanel = new JPanel();
masterPanel.setLayout(null);
gamtex = new JPanel();
gamtex.setLayout(null);
gamtex.setLocation(100, 10);
gamtex.setSize(155,20);
gamtex.setBackground(Color.yellow);
masterPanel.add(gamtex);
playerscore = new JLabel(""+ playersco +" Player");
playerscore.setLocation(0,0);
playerscore.setSize(90,20);
playerscore.setHorizontalAlignment(0);
gamtex.add(playerscore);
compscore = new JLabel(""+compsco+" Computer");
compscore.setLocation(20,0);
compscore.setSize(190,20);
compscore.setHorizontalAlignment(0);
gamtex.add(compscore);
compout = new JPanel();
compout.setLayout(null);
compout.setLocation(130,35);
compout.setSize(110,100);
masterPanel.add(compout);
//compout.setBackground(Color.DARK_GRAY);
compchoice = new JLabel(compprint);
compchoice.setLocation(0,0);
compchoice.setSize(85,20);
compout.add(compchoice);
selectpanel = new JPanel();
selectpanel.setLayout(null);
selectpanel.setLocation(0,15);
selectpanel.setSize(100,190);
//selectpanel.setBackground(Color.green);
masterPanel.add(selectpanel);
roc = new JButton("Rock");
roc.setLocation(10,5);
roc.setSize(80,30);
roc.addActionListener(this);
selectpanel.add(roc);
pap = new JButton("Paper");
pap.setLocation(10,45);
pap.setSize(80,30);
pap.addActionListener(this);
selectpanel.add(pap);
sis = new JButton("Sissors");
sis.setLocation(10,85);
sis.setSize(80,30);
sis.addActionListener(this);
selectpanel.add(sis);
masterPanel.setOpaque(true);
return masterPanel;
}
public void actionPerformed(ActionEvent e){
if(e.getSource() == roc ){
player = 1;
}else if(e.getSource() == pap){
player = 2;
}else if(e.getSource() == sis){
player = 3;
}
}
public static void comprandom(){
Random randomGenerator = new Random();
int randomInt = randomGenerator.nextInt(3);
if(randomInt == 1){
comp = 4;
compprint = "Sissors!";
}else if(randomInt == 2){
comp = 5 ;
compprint = "Rock!";
}else
{
comp = 6 ;
compprint = "Paper!";
}
}
public static void gamebrain()
{
if(player == 1 && comp == 4 ){
playersco = playersco +1;
playerscore.setText(""+ playersco +" Player");
}else if( player == 2 && comp == 5) {
playersco = playersco +1;
playerscore.setText(""+ playersco +" Player");
}else if( player == 3 && comp == 6){
playersco = playersco +1;
playerscore.setText(""+ playersco +" Player");
}else{compsco = compsco + 1;}
}
private static void showgame(){
JFrame frame = new JFrame("Rock, Paper, Sissors");
GamePanel show = new GamePanel();
frame.setContentPane(show.ContentPane());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(280, 190);
frame.setVisible(true);
}
public static void main(String[] args){
while(){
showgame();
while(!(player == 0)){
gamebrain();
comprandom();
}
}
}
}