In this problem you will create a computer game to play the game of Rock-Paper-S
ID: 3586745 • Letter: I
Question
In this problem you will create a computer game to play the game of Rock-Paper-Scissors with a user.
> java RockPaperScissors
Let's play! What's your move? (r=rock, p=paper, s=scissors or q to quit)
r
I choose rock. It's a tie
Let's play! What's your move? (r=rock, p=paper, s=scissors or q to quit)
p
I choose paper. It's a tie
Let's play! What's your move? (r=rock, p=paper, s=scissors or q to quit)
g
That is not a valid move. Please try again.
(r=rock, p=paper, s=scissors or q to quit)
p
I choose rock. You win.
Let's play! What's your move? (r=rock, p=paper, s=scissors or q to quit)
s
I choose rock. I win!
Let's play! What's your move? (r=rock, p=paper, s=scissors or q to quit)
r
I choose rock. It's a tie
Let's play! What's your move? (r=rock, p=paper, s=scissors or q to quit)
s
I choose paper. You win.
Let's play! What's your move? (r=rock, p=paper, s=scissors or q to quit)
q
Thanks for playing!
Our most recent games (in reverse order) were:
Me: paper You: scissors
Me: rock You: rock
Me: rock You: scissors
Me: rock You: paper
Me: paper You: paper
Me: rock You: rock
Our overall stats are:
I won: 16% You won 33% We tied: 50%
Here are some detailed requirements of the game play and specifics about the program:
You will write your code in the main method, but you should use good style and helper functions as needed. Remember that these helper functions need to be static, because you are not creating any RockPaperScissors objects.
The game should repeat until the user enters ‘q’
The game should track the full move history for both players. It should store the move history of the user in an array of Strings and the move history of the system in a LinkedList of Strings. These variables are already set up in the starter code. You just need to use them.
The array that stores the user’s moves is initialized to size 5. As the user enters more moves than the array will hold you should write code to expand this array (really, to make a new array and copy over the contents of the old one). The new array should always be twice the size of the old array. E.g. on the 6th move, the array becomes size 10, on the 11th move it becomes size 20, etc.
At the end of the game, the system should print out up to the last 10 games, in reverse order. If there has not been 10 games, it should print out as many as has been played. If there have been more than 10 games, it should only print the most recent 10. (But remember, it should store the full game histories). It should also print the win and tie statistics as in the example.
Your program should gracefully handle incorrect input by re-prompting the user until they enter valid input. You can look for the letters r, s and p exactly, or allow more freedom in the input.
Your programs should generate no exceptions under (almost) any circumstances. Try to break with bad input.
Here is the code:
import java.util.LinkedList; import java.util.Scanner; import java.util.Random; E public class RockPaperScissors public static void main( Stringt) args) int initialCapacity = 5; /I Store the user's move history string[] userMoves = new String[initialCapacity]; // Store the System' s move history LinkedList systemMoves-new LinkedList TODO: Write the code to play the game as specified by the writeup 7 TODO: When the game is done, write the code to print the move history // and stats //Initialize the quit command with character a //If user doesn't quit, go through the while loop while (quitcmd != 'g'){ system . out.print1n ("Let's play! What's your move? (r-rock, p-paper, s=scissors or q to quit)"); LyExplanation / Answer
package com.rps;
import java.io.*;
import java.util.LinkedList;
public class RockPaperScissors {
/*Game rules for RockPaperScissors*/
public static int decide(char user, char ai) {
if(user=='r' && ai=='p') {
return -1;
}
else if(user=='p' && ai=='r') {
return 1;
}
else if(user=='r' && ai=='s') {
return 1;
}
else if(user=='s' && ai=='r') {
return -1;
}
else if(user=='p' && ai=='s') {
return -1;
}
else if(user=='s' && ai=='p') {
return 1;
}
else
return 0;
}
public static void main(String arhs[])throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int initialCapacity=5;
String[] userMoves= new String[initialCapacity];
//String[] systemMoves= new String[initialCapacity];
LinkedList<String> systemMoves = new LinkedList<>();
for(int i=0;i<initialCapacity;i++) {
systemMoves.add("");
}
char quitCmd = 'n';
int result=0;
char comp;
int t=initialCapacity-1;
int win=0,lose=0,tie=0,games=0;
while (quitCmd!='q') {
System.out.println("Let's play! What's your move? (r=rock, p=paper, "
+ "s=scissors or q to quit)");
quitCmd=br.readLine().charAt(0);
if(quitCmd =='r' || quitCmd =='p' ||quitCmd =='s'){
games++;
comp=computer();
result=decide(quitCmd, comp);
userMoves[t]=display(quitCmd);
systemMoves.remove(t);
systemMoves.add(t,display(comp));
//systemMoves[t]=display(comp);
t--;
if(result==1) {
System.out.println("I choose "+display(quitCmd)+". I win!");
win++;
}
else if(result==-1) {
System.out.println("I choose "+display(quitCmd)+". You win.");
lose++;
}
else {
System.out.println("I choose "+display(quitCmd)+". It's a tie");
tie++;
}
}
else if(quitCmd!='q') {
System.out.println("That is not a valid move. Please try again.");
}
if(t<0) {
t=initialCapacity-1;
}
}System.out.println("Thanks for playing! " +
"Our most recent games (in reverse order) were: " +
"");
for(int i=0;i<initialCapacity;i++) {
if(userMoves[i]!=null)
System.out.println("Me: "+userMoves[i]+" You:"+systemMoves.get(i));
}
double w=(win*100/games);
double l=(lose*100/games);
double tt=(tie*100/games);
System.out.println("I won "+w+
"% "+"You won "+l+"% "+"We tied "+tt+"% ");
}
/*This function makes the computers move randomly*/
public static char computer() {
int randomNum = 1 + (int)(Math.random() * 3);
if(randomNum==1)
return 'r';
else if(randomNum==2)
return 'p';
else
return 's';
}
/*This function is used to store the moves of the players*/
public static String display(char c) {
if(c=='r')
return "rock";
else if(c=='p')
return "paper";
else
return "scissors";
}
}
/******************************************/
END OF CODE
/******************************************/
Sample Input:
r
p
s
w
q
Sample Output:
Let's play! What's your move? (r=rock, p=paper, s=scissors or q to quit)
I choose rock. It's a tie
Let's play! What's your move? (r=rock, p=paper, s=scissors or q to quit)
I choose paper. It's a tie
Let's play! What's your move? (r=rock, p=paper, s=scissors or q to quit)
I choose scissors. I win!
Let's play! What's your move? (r=rock, p=paper, s=scissors or q to quit)
That is not a valid move. Please try again.
Let's play! What's your move? (r=rock, p=paper, s=scissors or q to quit)
Thanks for playing!
Our most recent games (in reverse order) were:
Me: scissors You:paper
Me: paper You:paper
Me: rock You:rock
I won 33.0% You won 0.0% We tied 66.0%
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.