The program is suppose to allow the player to input how many iterations to play.
ID: 3620978 • Letter: T
Question
The program is suppose to allow the player to input how many iterations to play. The player clicks a button(rock paper or scissors) and then simulate. A random number is chosen as the computers choice. Score is tracked and the resulting winner is displayed. I am having trouble with implementing the button listeners for the rock, paper, scissors buttons.
Here is my code for programs: RockPaperScissors & RockPaperScissorsPanel
import javax.swing.JOptionPane;
import javax.swing.JFrame;
public class RockPaperScissors
{
//-----------------------------------------------------------------
// Creates the main program frame.
//-----------------------------------------------------------------
public static void main (String[] args)
{
JFrame frame = new JFrame ("Rock Paper Scissors");
frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(new RockPaperScissorsPanel());
frame.pack();
frame.setVisible(true)
}
}
//-***********This is the end of RockPaperScissors.java & beginning the other below*********************
import java.io.*;
import java.util.Random;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import javax.swing.border.*;
public class RockPaperScissorsPanel extends JPanel implements ActionListener
{
int max2;
public static void main (String[] args)
{
final Random generator = new Random();
final int num1 = 0;
int pcount = 0, ccount = 0, tcount = 0;
//----------------------------------------------------------
String max = JOptionPane.showInputDialog( "Number of Iterations");
int max2 = Integer.parseInt(max);
//----------------------------------------------------------
JFrame frame = new JFrame ("Rock Paper scissorss");
frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
JPanel subpanel1 = new JPanel();
subpanel1.setPreferredSize (new Dimension(300,75));
subpanel1.setBackground (Color.yellow);
TitledBorder tb = BorderFactory.createTitledBorder ("Make a Selection");
tb.setTitleJustification (TitledBorder.LEFT);
subpanel1.setBorder (tb);
final JButton rock = new JButton ("Rock");
//---------------------------------------------------------------------------------
//add actionListener
//rock.addActionListener(new ButtonListener());
final JButton paper = new JButton ("Paper");
//paper.addActionListener(new ButtonListener());
JButton scissors = new JButton ("scissors");
// scissors.addActionListener(new ButtonListener());
subpanel1.add (rock);
subpanel1.add (paper);
subpanel1.add (scissors);
JPanel subpanel2 = new JPanel();
subpanel2.setPreferredSize (new Dimension(150,75));
subpanel2.setBackground (Color.green);
final JLabel label1 = new JLabel ("Player's Choice");
subpanel2.add (label1);
JPanel subpanel3 = new JPanel();
subpanel3.setPreferredSize (new Dimension(150,75));
subpanel3.setBackground (Color.green);
final JLabel label2 = new JLabel ("Computer's Choice");
subpanel3.add (label2);
JPanel subpanel4 = new JPanel();
subpanel4.setPreferredSize (new Dimension(300,25));
subpanel4.setBackground (Color.red);
JLabel label3 = new JLabel ("No Results");
subpanel4.add (label3);
JPanel subpanel5 = new JPanel();
subpanel5.setPreferredSize (new Dimension(100,50));
subpanel5.setBackground (Color.white);
JLabel label4 = new JLabel ("Player Wins");
subpanel5.add (label4);
JPanel subpanel6 = new JPanel();
subpanel6.setPreferredSize (new Dimension(100,50));
subpanel6.setBackground (Color.white);
JLabel label5 = new JLabel ("Computer Wins");
subpanel6.add (label5);
JPanel subpanel7 = new JPanel();
subpanel7.setPreferredSize (new Dimension(100,50));
subpanel7.setBackground (Color.white);
JLabel label6 = new JLabel ("Ties");
JTextField field3 = new JTextField (pcount);
subpanel7.add (label6);
JPanel subpanel8 = new JPanel();
subpanel8.setPreferredSize (new Dimension(320,50));
subpanel8.setBackground (Color.blue);
JButton reset = new JButton ("Reset Scoreboard");
JButton sim = new JButton ("Run Simulation");
subpanel8.add (reset);
subpanel8.add (sim);
JPanel primary = new JPanel();
primary.setPreferredSize (new Dimension(320,320));
primary.setBackground (Color.blue);
primary.add (subpanel1);
primary.add (subpanel2);
primary.add (subpanel3);
primary.add (subpanel4);
primary.add (subpanel5);
primary.add (subpanel6);
primary.add (subpanel7);
primary.add (subpanel8);
frame.getContentPane().add(primary);
frame.pack();
frame.setVisible(true); }
int pcount, ccount, tcount;
rock.addActionListener(this);
paper.addActionListener(new ButtonListener());
scissors.addActionListener(new ButtonListener());
//create random generator
Random rand = new Random();
int comp = rand.nextInt(3);
//----------------------------------------------------------
public void actionPerformed (ActionEvent e){
Object paper = e.getSource();
Object scissors = e.getSource();
Object rock = e.getSource();
for (int i = 0; i < max2; i = i + 1) { // Test and Loop
if(e.getSource() == paper) {
if(comp == 0){
JLabel label3 = new JLabel("tie");
tcount++;
JLabel label6 = new JLabel("Ties n""+ tcount);
}
if(comp == 2){
JLabel label3 = new JLabel("computer wins");
ccount++;
JLabel label5 = new JLabel("Computer Wins n""+ ccount);
}
else if(comp == 1){
JLabel label3 = new JLabel("Player wins");
pcount++;
JLabel label4 = new JLabel("Player Wins n""+ pcount);
}
}
if(e.getSource() == rock){
if(comp == 0){
JLabel label3 = new JLabel("computer wins");
ccount++;
JLabel label5 = new JLabel("Computer Wins n""+ ccount);
}
if(comp == 2){
JLabel label3 = new JLabel("player wins");
pcount++;
JLabel label4 = new JLabel("Player Wins n""+ pcount);
}
else if(comp == 1){
JLabel label3 = new JLabel("tie");
tcount++;
JLabel label6 = new JLabel("Ties n""+ tcount);
}
if(e.getSource() == scissors){
if(comp == 0){
JLabel label3 = new JLabel("player wins");
pcount++;
JLabel label4 = new JLabel("Player Wins n""+ pcount);
}
if(comp == 2){
JLabel label3 = new JLabel("tie");
tcount++;
JLabel label6 = new JLabel("Ties n""+ tcount);
}
else if(comp == 1){
JLabel label3 = new JLabel("computer wins");
ccount++;
JLabel label5 = new JLabel("Computer Wins n""+ ccount);
}}
}
}
}
}
Explanation / Answer
Sorry I am not quite sure what the program should do. I edited your code. If that is not how you want it, feel free to tell me. Keep RockPaperScissors.java the same and change RockPaperScissorsPanel.java to the following: import java.io.*; import java.util.Random; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.*; import javax.swing.border.*; public class RockPaperScissorsPanel extends JPanel implements ActionListener { private JButton rock, paper, scissors; private JLabel label1, label2, label3, label4, label5, label6; private int pcount, ccount, tcount; private Random rand = new Random(); int max2; public RockPaperScissorsPanel() { String max = JOptionPane.showInputDialog( "Number of Iterations"); max2 = Integer.parseInt(max); JPanel subpanel1 = new JPanel(); subpanel1.setPreferredSize (new Dimension(300,75)); subpanel1.setBackground (Color.yellow); TitledBorder tb = BorderFactory.createTitledBorder ("Make a Selection"); tb.setTitleJustification (TitledBorder.LEFT); subpanel1.setBorder (tb); rock = new JButton ("Rock"); paper = new JButton ("Paper"); scissors = new JButton ("Scissors"); rock.addActionListener(this); paper.addActionListener(this); scissors.addActionListener(this); subpanel1.add (rock); subpanel1.add (paper); subpanel1.add (scissors); JPanel subpanel2 = new JPanel(); subpanel2.setPreferredSize (new Dimension(150,75)); subpanel2.setBackground (Color.green); label1 = new JLabel ("Player's Choice"); subpanel2.add (label1); JPanel subpanel3 = new JPanel(); subpanel3.setPreferredSize (new Dimension(150,75)); subpanel3.setBackground (Color.green); label2 = new JLabel ("Computer's Choice"); subpanel3.add (label2); JPanel subpanel4 = new JPanel(); subpanel4.setPreferredSize (new Dimension(300,25)); subpanel4.setBackground (Color.red); label3 = new JLabel ("No Results"); subpanel4.add (label3); JPanel subpanel5 = new JPanel(); subpanel5.setPreferredSize (new Dimension(100,50)); subpanel5.setBackground (Color.white); label4 = new JLabel ("Player Wins"); subpanel5.add (label4); JPanel subpanel6 = new JPanel(); subpanel6.setPreferredSize (new Dimension(100,50)); subpanel6.setBackground (Color.white); label5 = new JLabel ("Computer Wins"); subpanel6.add (label5); JPanel subpanel7 = new JPanel(); subpanel7.setPreferredSize (new Dimension(100,50)); subpanel7.setBackground (Color.white); label6 = new JLabel ("Ties"); JTextField field3 = new JTextField (pcount); subpanel7.add (label6); JPanel subpanel8 = new JPanel(); subpanel8.setPreferredSize (new Dimension(320,50)); subpanel8.setBackground (Color.blue); JButton reset = new JButton ("Reset Scoreboard"); JButton sim = new JButton ("Run Simulation"); subpanel8.add (reset); subpanel8.add (sim); JPanel primary = new JPanel(); primary.setPreferredSize (new Dimension(320,320)); primary.setBackground (Color.blue); primary.add (subpanel1); primary.add (subpanel2); primary.add (subpanel3); primary.add (subpanel4); primary.add (subpanel5); primary.add (subpanel6); primary.add (subpanel7); primary.add (subpanel8); this.add(primary); } //---------------------------------------------------------- public void actionPerformed (ActionEvent e){ for (int i = 0; iRelated Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.