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? } } } }
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....
Get Answers For Free
Most questions answered within 1 hours.