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
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...
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...
THIS IS FOR JAVA I have to write a method for a game of Hangman. The...
THIS IS FOR JAVA I have to write a method for a game of Hangman. The word the user is trying to guess is made up of hashtags like so " ###### " If the user guesses a letter correctly then that letter is revealed on the hashtags like so "##e##e##" If the user guesses incorrectly then it increments an int variable named count " ++count; " String guessWord(String guess,String word, String pound) In this method, you compare the character(letter)...
1) Consider the following Java program. Which statement updates the appearance of a button? import java.awt.event.*;...
1) Consider the following Java program. Which statement updates the appearance of a button? import java.awt.event.*; import javax.swing.*; public class Clicker extends JFrame implements ActionListener {     int count;     JButton button;     Clicker() {         super("Click Me");         button = new JButton(String.valueOf(count));         add(button);         button.addActionListener(this);         setSize(200,100);         setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);         setVisible(true);     }     public void actionPerformed(ActionEvent e) {         count++;         button.setText(String.valueOf(count));     }     public static void main(String[] args) { new Clicker(); } } a. add(button);...
In java create a dice game called sequences also known as straight shooter. Each player in...
In java create a dice game called sequences also known as straight shooter. Each player in turn rolls SIX dice and scores points for any sequence of CONSECUTIVE numbers thrown beginning with 1. In the event of two or more of the same number being rolled only one counts. However, a throw that contains three 1's cancels out player's score and they mst start from 0. A total of scores is kept and the first player to reach 100 points,...
Homework Draw class diagrams for your HW4 - the Tetris Game shown below: Part 1: UML...
Homework Draw class diagrams for your HW4 - the Tetris Game shown below: Part 1: UML As a review, Here are some links to some explanations of UML diagrams if you need them. • https://courses.cs.washington.edu/courses/cse403/11sp/lectures/lecture08-uml1.pdf (Links to an external site.) • http://creately.com/blog/diagrams/class-diagram-relationships/ (Links to an external site.) • http://www.cs.bsu.edu/homepages/pvg/misc/uml/ (Links to an external site.) However you ended up creating the UML from HW4, your class diagram probably had some or all of these features: • Class variables: names, types, and...
Complete a Java program named ARMgr that maintains customer accounts receivable in a database. The code...
Complete a Java program named ARMgr that maintains customer accounts receivable in a database. The code to initialize the CustomerAccountsDB database table and add a set of customer accounts is provided. Finish the code in these 3 methods in CustomerAccountDB.java to update or query the database: -purchase(double amountOfPurchase) -payment(double amountOfPayment) -getCustomerName() Hint: For getCustomerName(), look at the getAccountBalance() method to see an example of querying data from the database. For the purchase() and payment() methods, look at the addCustomerAccount() method...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT