Question

Using Java, write a program that allows the user to play the Rock-Paper-Scissors game against the...

Using Java, write a program that allows the user to play the Rock-Paper-Scissors game against the computer through a user interface. The user will choose to throw Rock, Paper or Scissors and the computer will randomly select between the two. In the game, Rock beats Scissors, Scissors beats Paper, and Paper beats Rock. The program should then reveal the computer's choice and print a statement indicating if the user won, the computer won, or if it was a tie. Allow the user to continue playing until they choose to stop. Once they stop, print the number of user wins, losses, and ties.

Here are the two separate files that create the mainframe and the panel.

//********************************************************************
// RockPaperScissors.java
//
// Create a Rock Paper Scissors game
//********************************************************************
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 Game");
   frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

   frame.getContentPane().add(new RockPaperScissorsPanel());
   frame.pack();

   frame.setVisible(true);
 }
}

--------------------------------------------------------------------------------------------------------------------------------

//********************************************************************
// RockPaperScissorsPanel.java Author: Max
//********************************************************************

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class RockPaperScissorsPanel extends JPanel
{

 private JButton rock, paper, scissors;
 private JLabel label, result;
 private JPanel buttonPanel;

 //-----------------------------------------------------------------
 // Constructor: Sets up the GUI.
 //-----------------------------------------------------------------
 public RockPaperScissorsPanel()
 {
   rock = new JButton("Rock");
   paper = new JButton("Paper");
   scissors = new JButton("Scissors");

   ButtonListener listener = new ButtonListener();
   rock.addActionListener(listener);
   paper.addActionListener(listener);
   scissors.addActionListener(listener);

   label = new JLabel("Push a button");
   result = new JLabel(" ");

   //new JPanel inside the larger panel
   buttonPanel = new JPanel();
   buttonPanel.setPreferredSize(new Dimension(400, 40)); //made longer
   buttonPanel.setBackground(Color.blue);
   buttonPanel.add(rock); //replace with rock
   buttonPanel.add(paper); //replace with paper
   buttonPanel.add(scissors); //add one for scissors

   //style
   setPreferredSize(new Dimension(400, 120)); //made longer
   setBackground(Color.cyan);
   add(label); //there's that label!
   add(buttonPanel); //smaller button panel
   add(result);
 }

 //*****************************************************************
 // Represents a listener for both buttons.
 //*****************************************************************
 private class ButtonListener implements ActionListener
 {
   //--------------------------------------------------------------
   // Determines which button was pressed and sets the label
   // text accordingly.
   //--------------------------------------------------------------
   public void actionPerformed(ActionEvent event)
   {
     //this will have to be more complicated...
     String computer = "Rock";

     if (event.getSource() == rock){
       label.setText("You played " + "Rock");
       result.setText("Computer played " + computer);
       //write win/loss/tie code here?
     }

     else if (event.getSource() == paper){
       label.setText("You played " + "Paper");
       result.setText("Computer played " + computer);
       //write win/loss/tie code here?
     }
     else{
       label.setText("You played " + "Scissors");
       result.setText("Computer played " + computer);
       //write win/loss/tie code here?
     }


   }
 }
}

Homework Answers

Answer #1

import java.util.Random;
import java.util.Scanner;

/**
* <p>
* An application that plays the Rock-Paper-Scissors game against the computer.
* </p>
*
* @author Liang Yang
* @version 1.0
*/
public class RockPaperScissors {
/**
* <p>
* This is the main method (entry point) that gets called by the JVM.
* </p>
*
* @param args
* command line arguments.
*/
public static void main(String[] args) {

final int randomLimit = 3;
String personPlay; // User's play -- "R", "P", or "S"
String computerPlay; // Computer's play -- "R", "P", or "S"
int computerInt; // Randomly generated number used to determine
// computer's play
int wins = 0;
int looses = 0;
int ties = 0;
boolean keepplaying = true;
Scanner scan = new Scanner(System.in);
Random generator = new Random();
System.out.println("Game start!Type \"quit\" to stop.");
System.out.println();
do {
// Get player's play
System.out.print("Eenter your play(R,P,S): ");
// Make player's play
personPlay = scan.nextLine().toUpperCase();
// Generate computer's play
if (personPlay.equalsIgnoreCase("quit")) {
keepplaying = false;
} else if (personPlay.equalsIgnoreCase("R")
|| personPlay.equalsIgnoreCase("P")
|| personPlay.equalsIgnoreCase("S")) {
computerInt = generator.nextInt(randomLimit);
switch (computerInt) {
case 0:
computerPlay = "R";
break;
case 1:
computerPlay = "P";
break;
default:
computerPlay = "S";
break;
}
// Print computer's play
System.out.println("Computer play is " + computerPlay);

if (personPlay.equals(computerPlay)) {
ties++;
System.out.println("It's a tie!");
} else if (personPlay.equals("R")) {
if (computerPlay.equals("S")) {
wins++;
System.out.println("Rock crushes scissors. You win!!");
} else {
looses++;
System.out.println("You lose.");
}
} else if (personPlay.equals("S")) {
if (computerPlay.equals("R")) {
looses++;
System.out.println("You lose.");
} else {
wins++;
System.out.println("You win!!");
}
} else {
if (computerPlay.equals("S")) {
looses++;
System.out.println("You lose.");
} else {
wins++;
System.out.println("You win!!");
}
}
} else {
System.out.println("Invalid input!");
}
} while (keepplaying);

System.out.println();
System.out.println("You wons:" + wins + " You looses:" + looses
+ " You ties:" + ties);
System.out.println();
System.out.println("Question two was called and ran sucessfully.");
}

};

note: plzzz don't give dislike.....plzzz comment if you have any problem i will try to solve your problem.....plzzz give thumbs up i am in need....

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
(Game: scissor, rock, paper) Write a program that plays the popular scissor-rockpaper game. (A scissor can...
(Game: scissor, rock, paper) Write a program that plays the popular scissor-rockpaper game. (A scissor can cut a paper, a rock can knock a scissor, and a paper can wrap a rock.) The program randomly generates a number 0, 1, or 2 representing scissor, rock, and paper. The program prompts the user to enter a number 0, 1, or 2 and displays a message indicating whether the user or the computer wins, loses, or draws.
Hi, I'm writing a Java program that prints a grid with circles and squares that have...
Hi, I'm writing a Java program that prints a grid with circles and squares that have letters in them and it is also supposed to print the toString() function to the console window each time the application runs. This toString() function is supposed to show the tile's shape, letter, and color component (or components). Could someone please review and debug my code to help me figure out why my toString() function is not working? import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JButton;...
R-S-P Requirement: - write one C++ program that mimics the Rock-Scissor-Paper game. This program will ask...
R-S-P Requirement: - write one C++ program that mimics the Rock-Scissor-Paper game. This program will ask user to enter an input (out of R-S-P), and the computer will randomly pick one and print out the result (user wins / computer wins). - User's input along with computer's random pick will be encoded in the following format: -- user's rock: 10 -- user's scissor:          20 -- user's paper:            30 -- comp's rock:            1 -- comp's scissor:        2 -- comp's...
   import javax.swing.*; import java.awt.*; import java.awt.event.*; //Class that paints according to the user wish. public...
   import javax.swing.*; import java.awt.*; import java.awt.event.*; //Class that paints according to the user wish. public class RapidPrototyping extends JFrame implements MouseListener,ItemListener,ActionListener,MouseMotionListener {       //panel to hold color,shapes and thickness components    JPanel panel;    //shapes combobox    JComboBox shapes;    //color radio buttons    JRadioButton red, green, blue;    //thickness combobox    JComboBox thicknesses;    //clear button.    JButton clear;    JPanel center;       /*values of each selection*/    Color color;    int thickness;    String shape;...
IN JAVA Speed Control Problem: The files SpeedControl.java and SpeedControlPanel.java contain a program (and its associated...
IN JAVA Speed Control Problem: The files SpeedControl.java and SpeedControlPanel.java contain a program (and its associated panel) with a circle that moves on the panel and rebounds from the edges. (NOTE: the program is derived from Listing 8.15 and 8.16 in the text. That program uses an image rather than a circle. You may have used it in an earlier lab on animation.) The Circle class is in the file Circle.java. Save the program to your directory and run it...
I am making a game like Rock Paper Scissors called fire water stick where the rules...
I am making a game like Rock Paper Scissors called fire water stick where the rules are Stick beats Water by floating on top of it Water beats Fire by putting it out Fire beats Stick by burning it The TODOS are as follows: TODO 1: Declare the instance variables of the class. Instance variables are private variables that keep the state of the game. The recommended instance variables are: 1. 2. 3. 4. 5. 6. A variable, “rand” that...
in the scheme programming language implement a game of rock paper scissors between a user and...
in the scheme programming language implement a game of rock paper scissors between a user and the computer. Only the following scheme subsets should be used: Special symbols (not case sensitive in our version (R5RS), but is in R6RS): a. Boolean: #t (else) and #f b. Characters: #\a, #\b ... #\Z c. Strings: in double quotes 3. Basic functions: a. quote b. car c. cdr d. c _ _ _ _ r, where each _ is either “a” or “d”...
Compile and execute the application. You will discover that is has a bug in it -...
Compile and execute the application. You will discover that is has a bug in it - the filled checkbox has no effect - filled shapes are not drawn. Your first task is to debug the starter application so that it correctly draws filled shapes. The bug can be corrected with three characters at one location in the code. Java 2D introduces many new capabilities for creating unique and impressive graphics. We’ll add a small subset of these features to the...
I've posted this question like 3 times now and I can't seem to find someone that...
I've posted this question like 3 times now and I can't seem to find someone that is able to answer it. Please can someone help me code this? Thank you!! Programming Project #4 – Programmer Jones and the Temple of Gloom Part 1 The stack data structure plays a pivotal role in the design of computer games. Any algorithm that requires the user to retrace their steps is a perfect candidate for using a stack. In this simple game you...
Objective: Write a Java program that will use a JComboBox from which the user will select...
Objective: Write a Java program that will use a JComboBox from which the user will select to convert a temperature from either Celsius to Fahrenheit, or Fahrenheit to Celsius. The user will enter a temperature in a text field from which the conversion calculation will be made. The converted temperature will be displayed in an uneditable text field with an appropriate label. Specifications Structure your file name and class name on the following pattern: The first three letters of your...