Question
In the rock paper scissor program I need to modify the Game Program Score code to enhance or make it better. Help!! Using Java &
In the rock paper scissor program I need to modify the Game Program Score code to enhance or make it better. Help!! Using Java & NetBeans IDE 8.2
Here Is the program:
package gui_rps; import java.awt.*; import java.awt.event.*; import java.util.Random; import javax.swing.*; /** * * @author bndixon */ public class GUI_RPS { // These are member variables.
JFrame RPS;
JButton Rock, Paper, Scissors;
JTextField HRPS;
JTextField CRPS;
JTextField Results;
JTextField Counter;
JPanel hInput;
char hChoice = ' ';
char cChoice = ' ';
char[] cc = {'R', 'P', 'S'};
int r3; int tie = 0;
int computerwins = 0;
int humanwins = 0; Random rGen = new Random(); // This is a constructor. It gets called when new GUI_RPS() is called from main.
public GUI_RPS()
{ RPS = new JFrame("Rock, Paper, Scissors");
RPS.setSize(600, 600);
Rock = new JButton("Rock");
Paper = new JButton("Paper");
Scissors = new JButton("Scissors");
hInput = new JPanel(); HRPS = new JTextField(20);
CRPS = new JTextField(20); Results = new JTextField(20); Counter = new JTextField(30);
Rock.addActionListener((ActionEvent e) -> { HRPS.setText("You Chose Rock"); hChoice = 'R'; play() ; });
Paper.addActionListener((ActionEvent e) -> { HRPS.setText("You Chose Paper"); hChoice = 'P'; play() ; });
Scissors.addActionListener((ActionEvent e) -> { HRPS.setText("You Chose Scissors"); hChoice = 'S'; play() ; });
hInput.setLayout(new FlowLayout());
hInput.add(HRPS);
hInput.add(CRPS); hInput.add(Counter);
hInput.add(Rock);
hInput.add(Paper);
hInput.add(Scissors);
RPS.getContentPane().add(hInput, BorderLayout.CENTER);
RPS.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
RPS.pack();
RPS.setVisible(true); } // Added method
private void play()
{
r3 = rGen.nextInt(3); cChoice = cc[r3];
switch (cChoice)
{
case 'R':
CRPS.setText("Computer Chose Rock");
break;
case 'P':
CRPS.setText("Computer Chose Paper");
break;
case 'S':
CRPS.setText("Computer Chose Scisssors");
break;
}
if(hChoice == cChoice) { tie++; } if(hChoice == 'R' && cChoice == 'S') { humanwins ++; } if(hChoice == 'P' && cChoice == 'R') { humanwins++; } if(hChoice == 'S' && cChoice == 'P') { humanwins++; } if(cChoice == 'R' && hChoice == 'S') { computerwins ++; } if(cChoice == 'P' && hChoice == 'R') { computerwins++; } if(cChoice == 'S' && hChoice == 'P') { computerwins++; } Counter.setText("Ties = " + tie + ", Your Wins = " + humanwins + ", Computer Wins = " + computerwins); }
public static void main(String[] args) {
// Set the look and feel to Java Swing Look
try {
UIManager.setLookAndFeel(
UIManager.getCrossPlatformLookAndFeelClassName());
} catch(ClassNotFoundException | IllegalAccessException | InstantiationException | UnsupportedLookAndFeelException e) {}
GUI_RPS application = new GUI_RPS();
}
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started