Question

In java language here is my code so far! i only need help with the extra...

In java language

here is my code so far! i only need help with the extra credit part

For this project, you will create a Rock, Paper, Scissors game.

Write a GUI program that allows a user to play Rock, Paper, Scissors against the computer.

If you’re not familiar with Rock, Paper, Scissors, check out the Wikipedia page: http://en.wikipedia.org/wiki/Rock-paper-scissors

This is how the program works:

The user clicks a button to make their move (rock, paper, or scissors).

The program randomly generates a move for the computer.

The program uses images to display the user's move and the computer's move.

The program displays the outcome of the match.

The program keeps track of and displays the number of computer wins, user wins, and ties.

I have provided an executable jar sample file.

Requirements:

Write a class (RPSGame) that represents the game. (50 points)

This class only represents the game itself. This class does not interact with the userr.

A game is made up of many matches. (A match is a single selection of rock, paper, or scissors). Thus, the RPSGame object is described by three characteristics: number of computer wins, number of user wins, and number of ties.

Write the instance data variables for these three characteristics and appropriate getters/setters.

Write a generateComputerPlay method that generates a random move by the computer.

Write a findWinner method that takes in two moves (the user and the computer) 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 appropriate win/loss/tie count (as instance data) depending on the outcome.

Include constants to represent the game states (user wins, computer wins, tie) and the moves (rock, paper, scissors).

You can represent these as Strings, ints, or something else- it's up to you.

Think through your decision and make sure it is a good design choice.

Declare and use your constants. (For example, create a constant called ROCK- this allows you to compare whether userMove == ROCK rather than userMove == 1, which is much more meaningful!)

Write the GUI program. (50 points)

This class will create the interface to interact with the user. It will also create an object of type RPSGame and invoke the methods in that class.

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.

You can use my images or use your own.

The listener classes will respond to the user selecting a move and then invoke the appropriate method in the RPS game class. Write helper methods that are invoked from the listener class rather than putting all the code inside a single method.

Here is some pseudocode for the listener class. You are not required to follow this, but it's here if it's helpful.

get the move from the user (determine 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 winner (by invoking a method on the RPS object)

update the display of the outcome and the game stats (obtained from RPS object)

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 know how to play the game at all. It 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 (15 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 betting or balance information should be displayed.

here is the sample output

Sample output:

https://gyazo.com/c80f0408b4f4aea32b4a27efcf2925a3

https://gyazo.com/3d33e172a3d54434b623bf26838f653b

https://gyazo.com/3c452e7796a21c7be1e4fe5c60197a62

// rpsgamegui.java file

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class RPSGUIGame extends JFrame {
// buttons for the user to enter their move
private JButton rockButton, paperButton, scissorsButton;
// labels to display the number of wins/losses/ties
private JLabel statusC, statusU, statusT;
// images and labels to display the computer and user's moves and the outcome of a match-up
private ImageIcon rockImage, paperImage, scissorsImage;
private JLabel compPlay, userPlay;
private JLabel outcome;
  
// the game object
private RPSGame game;
public RPSGUIGame() {
// initializes the window
super("Rock, Paper, Scissors, Go!");
setSize(350, 300);
Container contentPane = getContentPane();
contentPane.setLayout(new BorderLayout());
contentPane.setBackground(Color.black);
setResizable(false);
// creates the game object
game = new RPSGame(0);
// NOTE: IF COMPLETING THE EXTRA CREDIT, YOU WILL CHANGE THE 0 PARAMETER TO A VARIABLE THAT REPRESENTS THE BET AMOUNT
// creates the labels for displaying the computer and user's move;
// the images for the moves and the outcome of a match-up will be displayed
// in a single panel
rockImage = new ImageIcon("rock.jpg");
paperImage = new ImageIcon("paper.jpg");
scissorsImage = new ImageIcon("scissors.jpg");
compPlay = new JLabel();
compPlay.setVerticalTextPosition(SwingConstants.BOTTOM);
compPlay.setHorizontalTextPosition(SwingConstants.CENTER);
compPlay.setBorder(BorderFactory.createLineBorder(Color.black, 5));
compPlay.setForeground(Color.cyan);
userPlay = new JLabel();
userPlay.setVerticalTextPosition(SwingConstants.BOTTOM);
userPlay.setHorizontalTextPosition(SwingConstants.CENTER);
userPlay.setBorder(BorderFactory.createLineBorder(Color.black, 5));
userPlay.setForeground(Color.cyan);
  
outcome = new JLabel();
outcome.setHorizontalTextPosition(SwingConstants.CENTER);
outcome.setForeground(Color.pink);
  
JPanel imageOutcomePanel = new JPanel();
imageOutcomePanel.setBackground(Color.black);
imageOutcomePanel.setLayout(new BorderLayout());
imageOutcomePanel.add(compPlay, BorderLayout.WEST);
imageOutcomePanel.add(userPlay, BorderLayout.EAST);
imageOutcomePanel.add(outcome, BorderLayout.SOUTH);
  
// creates the labels for the status of the game (number of wins, losses, and ties);
// the status labels will be displayed in a single panel
statusC = new JLabel("Computer Wins: " + game.getCWins());
statusU = new JLabel("User Wins: " + game.getUWins());
statusT = new JLabel("Ties: " + game.getTies());
statusC.setForeground(Color.white);
statusU.setForeground(Color.white);
statusT.setForeground(Color.white);
JPanel statusPanel = new JPanel();
statusPanel.setBackground(Color.black);
statusPanel.add(statusC);
statusPanel.add(statusU);
statusPanel.add(statusT);
// the play and status panels are nested in a single panel
JPanel gamePanel = new JPanel();
gamePanel.setPreferredSize(new Dimension(250, 250));
gamePanel.setBackground(Color.black);
gamePanel.add(imageOutcomePanel);
gamePanel.add(statusPanel);
  
// creates the buttons and displays them in a control panel;
// one listener is used for all three buttons
rockButton = new JButton("Play Rock");
rockButton.addActionListener(new GameListener());
paperButton = new JButton("Play Paper");
paperButton.addActionListener(new GameListener());
scissorsButton = new JButton("Play Scissors");
scissorsButton.addActionListener(new GameListener());
JPanel controlPanel = new JPanel();
controlPanel.add(rockButton);
controlPanel.add(paperButton);
controlPanel.add(scissorsButton);
controlPanel.setBackground(Color.black);
// the gaming and control panel are added to the window
contentPane.add(gamePanel, BorderLayout.CENTER);
contentPane.add(controlPanel, BorderLayout.SOUTH);
  
setVisible(true);
}
/* determines which button was clicked and updates the game accordingly */
private class GameListener implements ActionListener {
public void actionPerformed(ActionEvent event) {
// YOUR CODE GOES HERE!
String nombre=event.getActionCommand();

if(nombre.equals("Play Rock")){
System.out.println("Se ha pulsado el botón Rock");
int opComp = game.generateComputerPlay();
System.out.println("Computer selected "+game.getOptionComp(opComp));
game.findWinner(opComp, game.ROCK);
}
if(nombre.equals("Play Paper")){
System.out.println("Se ha pulsado el botón Paper");
int opComp = game.generateComputerPlay();
System.out.println("Computer selected "+game.getOptionComp(opComp));
game.findWinner(opComp, game.PAPER);
}
if(nombre.equals("Play Scissors")){
System.out.println("Se ha pulsado el botón Scissors");
int opComp = game.generateComputerPlay();
System.out.println("Computer selected "+game.getOptionComp(opComp));
game.findWinner(opComp, game.SCISSORS);
}
game.display();
update();
}
}
  
public void update(){
statusC.setText("Computer Wins: " + game.getCWins());
statusU.setText("User Wins: " + game.getUWins());
statusT.setText("Ties: " + game.getTies());
}
  
public static void main(String args[]) {
// create an object of your class
RPSGUIGame frame = new RPSGUIGame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}

// file rps game

import java.util.Random;

public class RPSGame {
private int CWins; // Computer Wins
private int UWins; // User Wins
private int Ties; // Ties
  
// Constants
static final int ROCK = 1;
static final int PAPER = 2;
static final int SCISSORS = 3;
  
// Constructor
public RPSGame(int init){
CWins = init;
UWins = init;
Ties = init;
}
// Setters and Getters
public void setCWins(int cw){
CWins = cw;
}
public void setUWins(int uw){
UWins = uw;
}
public void setTies(int t){
Ties = t;
}
public int getCWins(){
return CWins;
}
public int getUWins(){
return UWins;
}
public int getTies(){
return Ties;
}
// Display status
public void display(){
System.out.println("Comp Wins "+CWins+" User Wins "+UWins+" Ties"+Ties);
}
// Genera Random Computer Play
public int generateComputerPlay(){
Random r = new Random();
return (int)(r.nextDouble() * 3 + 1);
}
// Find Winner between Computer and User
public boolean findWinner(int compMove, int userMove){
if (compMove == userMove)
setTies(getTies()+1); // update Ties
else
if (compMove == ROCK){
if (userMove == PAPER)
setUWins(getUWins()+1); // update User Win
else
setCWins(getCWins()+1); // update Comp Win
}else{
if (compMove == PAPER){
if (userMove == SCISSORS)
setUWins(getUWins()+1); // update User Win
else
setCWins(getCWins()+1); // update Comp Win
}else{
if (compMove == SCISSORS){
if (userMove == ROCK)
setUWins(getUWins()+1); // update User Win
else
setCWins(getCWins()+1); // update Comp Win
}
}
}
return true;
}
// Option String of Computer
public String getOptionComp(int op){
String s="";
switch(op){
case 1 : s="ROCK"; break;
case 2 : s="PAPER"; break;
default : s="SCISSORS"; break;
}
return s;
}
}

0 0
Add a comment Improve this question Transcribed image text
Request Professional Answer

Request Answer!

We need at least 10 more requests to produce the answer.

0 / 10 have requested this problem solution

The more requests, the faster the answer.

Request! (Login Required)


All students who have requested the answer will be notified once they are available.
Know the answer?
Add Answer to:
In java language here is my code so far! i only need help with the extra...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Similar Homework Help Questions
  • JAVA Only Help on the sections that say Student provide code. The student Provide code has...

    JAVA Only Help on the sections that say Student provide code. The student Provide code has comments that i put to state what i need help with. import java.util.Scanner; public class TicTacToe {     private final int BOARDSIZE = 3; // size of the board     private enum Status { WIN, DRAW, CONTINUE }; // game states     private char[][] board; // board representation     private boolean firstPlayer; // whether it's player 1's move     private boolean gameOver; // whether...

  • In this problem, you’ll play a simple rock, paper, scissors game. First, you’ll ask the user...

    In this problem, you’ll play a simple rock, paper, scissors game. First, you’ll ask the user to pick rock, paper, or scissors. Then, you’ll have the computer randomly choose one of the options. After that, print out the winner! You should keep playing the game until the user hits enter. Note: You’ll need to implement a method called String getWinner(String user, String computer). Luckily, you just wrote that in an earlier program! Here is a sample run of the program....

  • Write a C# program that allows one user to play Rock-Paper-Scissors with computer. The user will...

    Write a C# program that allows one user to play Rock-Paper-Scissors with computer. The user will pick a move (Rock Paper Scissors) from 3 radio buttons and click the play button to play the game. The application will use random number generator to pick a move for the computer. Then, the application display the computer's move and also display whether you won or lost the game, or the game is tie if both moves are the same. The application must...

  • java pls Rock Paper Scissors Lizard Spock Rock Paper Scissors Lizard Spock is a variation of...

    java pls Rock Paper Scissors Lizard Spock Rock Paper Scissors Lizard Spock is a variation of the common game Rock Paper Scissors that is often used to pass time (or sometimes to make decisions.) The rules of the game are outlined below: • • Scissors cuts Paper Paper covers Rock Rock crushes Lizard Lizard poisons Spock Spock smashes Scissors Scissors decapitates Lizard Lizard eats Paper Paper disproves Spock Spock vaporizes Rock Rock crushes Scissors Write a program that simulates the...

  • Java CSC252   Programming II    Static Methods, Enums, Constants, Random Rock Paper Scissors Write a program that...

    Java CSC252   Programming II    Static Methods, Enums, Constants, Random Rock Paper Scissors Write a program that allows the user to play "Rock, Paper, Scissors". Write the RPS class. All code should be in one file. main() will be part of the RPS class, fairly small, and contain only a loop that asks the user if they want to play "Rock, Paper, Scissors". If they say yes, it calls the static method play() of the RPS class. If not, the program...

  • Python please. I have a working one that doesn't keep track of w/l ratio, it may...

    Python please. I have a working one that doesn't keep track of w/l ratio, it may be helpful to see how others did the entire program and inserted that module. Write a modular program that let the user play the game of Rock, Paper, Scissors against the computer. The program should work as follows: When the program begins, a random number in the range of 1 thru 3 is generated but do not display the computer choice immediately. Number 1...

  • IN JAVA. Write a program that lets the user play the game of Rock, Paper, Scissors...

    IN JAVA. Write a program that lets the user play the game of Rock, Paper, Scissors against the computer. The program should work as follows. When the program begins, a random number in the range of 1 through 3 is generated. If the number is 1, then the computer has chosen rock. If the number is 2, then the computer has chosen paper. If the number is 3, then the computer has chosen scissors. Don’t display the computer’s choice yet....

  • (Java) Write a program that lets the user play the game of Rock, Paper, Scissors against...

    (Java) Write a program that lets the user play the game of Rock, Paper, Scissors against the computer. The program should work as follows. When the program begins, a random number in the range of 1 through 3 is generated. If the number is 1, then the computer has chosen rock. If the number is 2, then the computer has chosen paper. If the number is 3, then the computer has chosen scissors. Don’t display the computer’s choice yet. The...

  • I need help finishing this excerise this is what I have:   I get this output for an error: RPS_ExerciseE1.rb:171:in `+&#...

    I need help finishing this excerise this is what I have:   I get this output for an error: RPS_ExerciseE1.rb:171:in `+': no implicit conversion of nil into String (TypeError) from RPS_ExerciseE1.rb:171:in `display_results' from RPS_ExerciseE1.rb:89:in `play_game' from RPS_ExerciseE1.rb:240:in `block in <main>' from RPS_ExerciseE1.rb:237:in `loop' from RPS_ExerciseE1.rb:237:in `<main>' Currently, the Game class’s get_player_move method uses a regular expression to validate the player’s moves of Rock, Paper, or Scissors, rejecting any input other than one of these words. Making the player enter entire words...

  • Using python 3.7.3 Challenge: Rock, Paper, Scissors GamePDF Description: Create a menu-driven rock, paper, scissors game...

    Using python 3.7.3 Challenge: Rock, Paper, Scissors GamePDF Description: Create a menu-driven rock, paper, scissors game in Python 3 that a user plays against the computer with the ability to save and load a game and its associated play statistics. Purpose: The purpose of this challenge is to assess the developer’s ability to create an interactive application with data persistence in Python 3. Requirements: Create a Rock, Paper, Scissors game in Python named rps.py according to the requirements specified in...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT