JAVA Program The Rock, Paper, Scissors Game Your problem You are to write a prog
ID: 3697472 • Letter: J
Question
JAVA Program The Rock, Paper, Scissors Game
Your problem
You are to write a program to play 10 rounds of the game between the program and a human player. In each round, the program makes a random choice and asks the human player to make his/her choice by typing an integer value in the range [1, 3] (1 = rock, 2 = paper, 3 = scissors). The program then determines and displays the result of that round. The program also maintains counts of wins by computer, wins by human player, and ties. Once all rounds are played, an overall winner is declared or a tie is declared.
I already have structure but some part inside I don't know how to do fix that.
And this is what I do about RPS but I can not get result like output in picture.
package javaapplication10;
/**
*
* @author peilunlu
*/
import java.util.Scanner;
import java.util.Random;
public class rockpaperscissors
{
public static void main (String[] args)
{
int cChoice;
int pChoice = 0;
int cScore = 0, pScore = 0, tie = 0, rounds = 0;
String loop="yes";
Scanner input = new Scanner(System.in);
Random rgen = new Random();
System.out.println("Hello, for this exercise we're going to be playing everyone's favourite game, Rock-Paper-Scissors!");
while (loop.equals("yes"))
{
cChoice=rgen.nextInt(3)+1;
System.out.println("Please make your selection: R - Rock. P - Paper. S - Scissors");
String hInput = input.nextLine();
String hChoice = hInput.toUpperCase();
if (hChoice.equals("R") || hChoice.equals("P") || hChoice.equals("S"))
{
System.out.println("");
if (hChoice.equals("R"))
{
pChoice = 1;
}
if (hChoice.equals("P"))
{
pChoice = 2;
}
if (hChoice.equals("S"))
{
pChoice = 3;
}
if (pChoice == cChoice)
{
System.out.println("Tie Game!");
System.out.println("");
tie++;
rounds++;
} else
{
if (cChoice==1 && pChoice==3)
{
System.out.println("Computer picked Rock!");
System.out.println("Rock beats Scissors!");
System.out.println("**Computer Wins!**");
System.out.println("");
cScore++;
rounds++;
}
if (cChoice==1 && pChoice==2)
{
System.out.println("Computer picked Rock!");
System.out.println("Paper beats Rock!");
System.out.println("**Player Wins!**");
System.out.println("");
pScore++;
rounds++;
}
if (cChoice==2 && pChoice==3)
{
System.out.println("Computer picked Paper!");
System.out.println("Scissors beats Paper!");
System.out.println("**Player Wins!**");
System.out.println("");
pScore++;
rounds++;
}
if (cChoice==2 && pChoice==1)
{
System.out.println("Computer picked Paper!");
System.out.println("Paper beats Rock!");
System.out.println("**Computer Wins!**");
System.out.println("");
cScore++;
rounds++;
}
if (cChoice==3 && pChoice==1)
{
System.out.println("Computer picked Scissors!");
System.out.println("Rock beats Scissors!");
System.out.println("**Player Wins!**");
System.out.println("");
pScore++;
rounds++;
}
if (cChoice==3 && pChoice==2)
{
System.out.println("Computer picked Scissors!");
System.out.println("Scissors beats Paper!");
System.out.println("**Computer Wins!**");
System.out.println("");
cScore++;
rounds++;
}
}
} else // end the game
{
System.out.println ("Sorry, you didn't pick Rock, Paper, or Scissors. The game will end now.");
System.out.println ("Here are the final scores after " + rounds +" rounds:");
System.out.println ("You Computer Ties");
System.out.println (" "+ pScore +" " + cScore + " " + tie);
loop = "no";
}
}
}
}
I can do this without method but I want know how to do this program in use method.
Explanation / Answer
import java.io.*;
import java.util.*;
class game
{
static final PrintStream cout = System.out;
static final Random rand = new Random();
static final Scanner cin = new Scanner(System.in);
static final int ROUNDS = 10;
// choices to pick from
static final int ROCK = 1, PAPER = 2, SCISSORS = 3;
// results from one round of play
static final int COMPUTER_WIN = 1, PLAYER_WIN = 2, TIE = 3;
// String arrays to translate numbers to word desriptions
static final String[] CHOICE_NAMES = {"", "Rock", "Paper", "Scissors"};
static final String[] OUTCOME_NAMES = {"", "Computer win", "Player win", "Tie"};
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
cout.print("CPS 150 Final Exam Take Home problem by YOUR NAME HERE ");
cout.print("Let's play Rock-Paper-Scissors! ");
cout.print("We will play " + ROUNDS + " rounds. ");
// declare/initialize all primitive type and array reference variables
int count = 10;
int cChoice;
int pChoice = 0;
int cScore = 0, pScore = 0, tie = 0, rounds = 0;
Scanner input = new Scanner(System.in);
Random rgen = new Random();
int[] computerChoices =new int[ROUNDS];
int[] playerChoices =new int[ROUNDS];
int[] results =new int[ROUNDS];
// The loop starts here
while(rounds<ROUNDS){
cChoice=rgen.nextInt(3)+1;
System.out.print("Pick 1 (Rock), 2 (Paper), or 3 (Scissors): ");
pChoice = input.nextInt();
computerChoices[rounds] = cChoice;
playerChoices[rounds] =pChoice;
if((pChoice>3 || pChoice<1)){
System.out.print("Invalid Choice, try again.");
} else {
if (pChoice == cChoice)
{
System.out.println("Tie Game!");
System.out.println("");
tie++;
results[rounds] = 3;
} else
{
if (cChoice==1 && pChoice==3)
{
System.out.println("Computer picked Rock!");
System.out.println("Rock beats Scissors!");
System.out.println("**Computer Wins!**");
System.out.println("");
cScore++;
results[rounds] = 1;
}
if (cChoice==1 && pChoice==2)
{
System.out.println("Computer picked Rock!");
System.out.println("Paper beats Rock!");
System.out.println("**Player Wins!**");
System.out.println("");
pScore++;
results[rounds] = 2;
}
if (cChoice==2 && pChoice==3)
{
System.out.println("Computer picked Paper!");
System.out.println("Scissors beats Paper!");
System.out.println("**Player Wins!**");
System.out.println("");
pScore++;
results[rounds] = 2;
}
if (cChoice==2 && pChoice==1)
{
System.out.println("Computer picked Paper!");
System.out.println("Paper beats Rock!");
System.out.println("**Computer Wins!**");
System.out.println("");
cScore++;
results[rounds] = 1;
}
if (cChoice==3 && pChoice==1)
{
System.out.println("Computer picked Scissors!");
System.out.println("Rock beats Scissors!");
System.out.println("**Player Wins!**");
System.out.println("");
pScore++;
results[rounds] = 2;
}
if (cChoice==3 && pChoice==2)
{
System.out.println("Computer picked Scissors!");
System.out.println("Scissors beats Paper!");
System.out.println("**Computer Wins!**");
System.out.println("");
cScore++;
results[rounds] = 1;
}
}
rounds++;
}
}
showGameResults(computerChoices,playerChoices,results);
System.out.println ("Here are the final scores after " + rounds +" rounds:");
System.out.println ("You Computer Ties");
System.out.println (" "+ pScore +" " + cScore + " " + tie);
// end loop
// call the showGameResults method
// display final summary from 10 rounds
cout.print(" CPS 150 Final Exam Take home problem complete ");
} // end main
static int getPlayerChoice() {
// stub code
return ROCK;
} // end getPlayerChoice
static int getRoundResult(final int computerChoice, final int playerChoice) {
// stub code
return PLAYER_WIN;
} // end getRoundResult
static void showRoundResult(final int computerChoice, final int playerChoice,
final int roundResult) {
// stub code
cout.println("showRoundResult: " + computerChoice + " " + playerChoice +
" " + roundResult);
} // end showRoundResult
// This method is complete
static void showGameResults(int[] computerChoices,
int[] playerChoices, int[] results) {
cout.print("Game results ");
cout.print("============ ");
cout.printf("%5s %-14s%-16s%-13s ", "Round", "Player choice",
"Computer choice", "Round result");
cout.printf("%5s %-14s%-16s%-13s ", "-----", "-------------",
"---------------", "------------");
for (int k = 1; k < results.length; k++) {
cout.printf("%5d %-14s%-16s%-13s ", k, CHOICE_NAMES[playerChoices[k]],
CHOICE_NAMES[computerChoices[k]], OUTCOME_NAMES[results[k]]);
} // end loop
} // end showGameResults
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.