Need help with JAVA. For this project, create a Rock, Paper, Scissors (RPS) game
ID: 3856401 • Letter: N
Question
Need help with JAVA.
For this project, create a Rock, Paper, Scissors (RPS) game that allows a user to play RPS against the computer. If you’re not familiar with RPS, check out the Wikipedia page: http://en.wikipedia.org/wiki/Rock-paper-scissors (Links to an external site.)Links to an external site.
The game works this way:
In a single round:
The user clicks a button to make their move (rock, paper, or scissors).
The program randomly generates a move for the computer.
The moves are displayed as images.
The program determines and displays the outcome of this one round.
The user can play as many rounds as they want.
The program keeps track of and displays the number of computer wins, user wins, and ties across all rounds.
Part 1: The RPS Game (50 points)
Write a class (RPSGame) that represents the game.
This class only represents the game.
This class does not interact with the user at all.
The RPSGame object is described by three characteristics. These are the instance data variables:
number of computer wins
number of user wins
number of ties
Include appropriate getters and setters.
Include constants to represent the possible moves (rock, paper, or scissors) and the possible outcomes of a single round (user win, computer win, or tie).
Use these constants to make your code more readable.
For example, if you are using ints to store the moves, it's more clear to test whether userMove==ROCK than userMove==1.
You can decide what type of data to use for the constants (String, int, or something else).
Write a generateComputerPlay method that generates a random move by the computer.
Write a findWinner method that takes in two moves as parameters (the user move and the computer move) and determines the outcome (user wins, computer wins, or tie).
Determining the winner will require you to compare a lot of possible match-ups through a series of nested conditionals.
The method should update the instance data (number of computer wins, number of user wins, and number of ties) depending on the outcome.
Part 2: The GUI (50 points)
Write the GUI program to interact with the user.
This class, named "RPSGUIGame", will create an object of type RPSGame and invoke the methods from that class to run the game.
The constructor sets up the display and create the instance of RPSGame.
Because I don’t want you to spend the majority of your time thinking through the layout, I have provided a shell file you can use as a starting point.
You are not required to use this- if you want to start from scratch, you are welcome to do so. You are also welcome to make changes to the shell.
Your code goes inside the listener class.
The listener classes responds to the user selecting a move, invokes the appropriate methods in the RPS game class, and then updates the display.
Do not write all of the code inside the single listener class method. Write helper methods that are invoked from the listener class to break up and organize the code.
Here is some pseudocode for the listener class. You are not required to follow this algorithm.
get the move from the user by determining which button was clicked
update the display of the user’s move
generate a move by the computer by invoking a method on the RPSGame object
update the display of the computer’s move
determine the outcome (the winner) by invoking a method on the RPS object
update the display of the outcome
get the updated game stats (number of computer wins, number of user wins, and number of ties) by invoking methods on the RPS object
update the display of the outcome
You can use my images or use your own.
Part of this assignment is making sure that each class handles the appropriate tasks.
The RPSGame class should only handle the game- it shouldn't have any interaction with the user.
The RPSGUI class should only interact with the user- it doesn't really know how to play the game at all.
The GUI only knows how to get info from the user, send that info to the game, get info back from the game, and display that info to the user.
Make sure you preserve this object-oriented idea throughout your program!
Extra Credit A- Betting (5 points)
Add support for betting.
In RPSGame:
Add instance data to represent the amount the user is betting (the same amount on each match) and the user’s balance of money. Write appropriate getters and setters for these instance data.
In the findWinner method, when the number of wins/losses is updated, also update the user’s balance by adding or subtracting their bet amount.
In the GUI program:
Use the JOptionPane class to ask the user about betting prior to the game starting. This code should go inside the main method.
Use the JOptionPane.showConfirmDialog method to determine whether the user wants to bet.
If the user wants to bet, use the JOptionPane.showInputDialog method to find out how much the user wants to bet.
Change the RPSGUIGame constructor so that it takes one parameter: the bet amount.
Create a new JLabel to display the user’s balance. I recommend adding the label to the statusPanel. You can choose another place if you want.
If the user is betting, when the display of the stats is updated, update the display of the balance.
If the user is not betting, no money information should be displayed during the game.
Extra Credit B- Text-Based Program (5 points)
Write a text-based driver program that allows the user to play through the console (instead of through a GUI).
Use only text input and output.
Use the same RPSGame class that you used with the GUI- do not have a second, different RPSGame class.
Here is some pseudocode for your main method. You are not required to follow this algorithm:
create an instance of the RPSGame
while the user wants to keep playing:
read in the move from the user
generate a move by the computer by invoking a method on the RPSGame object
determine the outcome (the winner) by invoking a method on the RPS object
get the updated game stats (number of computer wins, number of user wins, and number of ties) by invoking methods on the RPS object
print out the outcome and game stats to the user
find out if the user wants to keep playing
Explanation / Answer
This is the code for the graphical user interface for the game rock paper scissor. Hope you'll like it.
RPS.java
import java.awt.Color;
import java.awt.Container;
import java.awt.event.*;
import javax.swing.*;
public class RPS {
static int hWin;
static int won=0;
static int tot=0;
static int tied=0;
public static void main(String[] args){
GP();
intoPanel();
}
private static void intoPanel(){
String intro="Rock, Paper, Scissors!Pick your hands whenever you are ready. Rock beats scissors, scissors beats paper ,paper wraps the rock. Yes ,paper beats rock.";
JOptionPane.showMessageDialog(null,intro, "How to play!", 0, new ImageIcon(System.getProperty("users.dir")+"/image/rock.gif"));
}
private static void GP(){
JFrame fr = new JFrame("Rock, Scissors, Paper");
Container cont = fr.getContentPane();
cont.setLayout(null);
String[] conn= new String[3];
int[] bndInt= new int[3];
for(int uu=0; uu<=2; uu++){
conn[uu]=System.getProperty("users.dir")+"/image/"+uu+"paper.jpg";
bndInt[uu]=40+150*uu;
}
JButton btn = new JButton (" ", new ImageIcon(conn[0]));
btn.setBackground(Color.white);
btn.setBounds(40,bndInt[0],150,100);
JButton btn1 = new JButton (" ", new ImageIcon(conn[1]));
btn1.setBackground(Color.white);
btn1.setBounds(40,bndInt[1],150,100);
JButton btn2 = new JButton (" ", new ImageIcon(conn[2]));
btn2.setBackground(Color.white);
btn2.setBounds(40,bndInt[2],150,100);//creating three buttons
JLabel lbl1 = new JLabel(new ImageIcon(System.getProperty("user.dir")+"/image/sciss.jpg"));
lbl1.setBounds(300, 140, 128, 200);
cont.add(lbl1);
JButton btn3 = new JButton("Cheat");
btn3.setBounds(350, 430, 80, 30);
JButton btn4 = new JButton("Quit");
btn4.setBounds(260, 430, 80, 30);
cont.add(btn);
cont.add(btn1);
cont.add(btn2);
cont.add(btn3);
cont.add(btn4);
btn.addActionListener(
new ActionListener() {
public void actionPerformed( ActionEvent event ) {
cntWinner(1);
}
}
);
btn1.addActionListener(
new ActionListener() {
public void actionPerformed( ActionEvent event ) {
cntWinner(2);
}
}
);
btn2.addActionListener(
new ActionListener() {
public void actionPerformed( ActionEvent event ) {
cntWinner(3);
}
}
);
btn3.addActionListener(
new ActionListener() {
public void actionPerformed( ActionEvent event ) {
won=won+1;
tot=tot+1;
JOptionPane.showMessageDialog(null,"Rack up another won!"+" Win/Loss rate: " + won+"/"+tot+" Tie: "+tied,"Cheater do not prospers", 0, new ImageIcon(System.getProperty("user.dir")+"/image/rock.jpg"));
}
}
);
btn4.addActionListener(
new ActionListener() {
public void actionPerformed( ActionEvent event ) {
String intro="Paper:The rock keeps trying to break free and the scissors keep cutting me!
Rock: Let me out! Scissors: Oh rock! Snip snip.
Author: Thank you for playing and I have to take these for the treatment now.";
JOptionPane.showMessageDialog(null,intro, "Thank you for playing!Please come again......", 0, new ImageIcon(System.getProperty("user.dir")+"/image/ty.gif"));
System.exit(0);
}
}
);
fr.setSize(500, 500);
fr.setVisible(true);
fr.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void cntWinner(int x){
int cmpCh=computerRandomChoice();
int humanCh=x;
String intro,txt1="";
String winCombo= ""+Math.min(cmpCh, humanCh)+Math.max(cmpCh, humanCh);
switch(Integer.parseInt(winCombo)){
case 12:
intro = "Paper wins!";
if(humanCh==2) hWin=1;
break;
case 13:
intro = "Rock wins!";
if(humanCh==1) hWin=1;
break;
case 23:
intro = "Scissors wins!";
if(humanCh==3) hWin=1;
break;
default: intro="It is a tied!";
hWin=2;
tied=tied+1;
}
if(hWin==1){
txt1="Player wins! ";
hWin=0;
won=won+1;
tot=tot+1;
}else if(hWin==2){
txt1="It is a tied! ";
hWin=0;
}else{
txt1="Computer wins! ";
tot=tot+1;
}
JFrame fr = new JFrame("Rock, Scissors, Paper");
Container cont = fr.getContentPane();
cont.setLayout(null);
JLabel lbl0 = new JLabel(txt1+intro);
lbl0.setBounds(75, 10, 300, 35);
cont.add(lbl0);
JLabel lbl1 = new JLabel("Player's Choice");
lbl1.setBounds(40, 35, 150, 35);
cont.add(lbl1);
JLabel lbl2 = new JLabel("Computer's Choice");
lbl2.setBounds(215, 35, 150, 35);
cont.add(lbl2);
JLabel lbl3 = new JLabel(new ImageIcon(System.getProperty("users.dir")+"/image/"+(humanCh-1)+".jpg"));
lbl3.setBounds(10, 100, 170, 60);
cont.add(lbl3);
JLabel lbl4 = new JLabel(new ImageIcon(System.getProperty("users.dir")+"/image/"+(cmpCh-1)+".jpg"));
lbl4.setBounds(200, 100,170, 60);
cont.add(lbl4);
JLabel lbl5 = new JLabel("Win/Loss rate: " + won+"/"+tot);
lbl5.setBounds(125, 25, 150, 350);
cont.add(lbl5);
JLabel lbl6 = new JLabel("Tie: "+tied);
lbl6.setBounds(125, 30, 125, 370);
cont.add(lbl6);
fr.setSize(400, 270);
fr.setVisible(true);
}
public static int computerRandomChoice(){
int res=(int)(Math.random()*3)+1;
return res;
}
}
Please rate the answer if it helped.....Thankyou
Hope it helps......
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.