This is my homework assignment where I have to make rock, paper, scissors, lizard, spock game. I need the game to run until either the player or the computer has won by 2. I think I have to do some kind of loop, but I'm not too sure. Also, I need to ask the player if they want to keep playing after displaying the results. How would I go about doing this? THanks!
import java.util.Scanner;
/**
* @author
*
*/
public class RockPaperScissors {
public static void main(final String[] args) {
final Scanner input = new Scanner(System.in);
//define variables
double playerWins = 0;
double computerWins = 0;
//prompt user for an input equal to 1, 2, or 3
System.out.println("Let's play rock, paper, scissors, with a twist!");
//start the loop to end until one has two more games won than the other
while(true){
System.out.println("Enter rock, paper, scissors, Lizard, or Spock.");
//store the user's input in the string
final String player = input.nextLine().toLowerCase();
//generate random number for the computer to play
final double randomNumber = (int)(Math.random() * 5 + 1);
//if-else statements for user input = rock
if (player.equals("rock")) {
if (randomNumber == 0) {
System.out.println("Rock versus rock. It's a tie!");
}else if (randomNumber == 1) {
System.out.println("Rock versus paper. Paper covers rock. You lose!");
computerWins++;
}else if (randomNumber == 2) {
System.out.println("Rock versus scissors. Rock smashes scissors. You win!");
playerWins++;
}else if (randomNumber == 3) {
System.out.println("Rock versus Lizard. Rock smashes Lizard. You win!");
playerWins++;
}else {
System.out.println("Rock versus Spock. Spock vaporizes rock. You lose!");
computerWins++;
}
}
//if-else statements for user input = paper
if (player.equals("paper")) {
if (randomNumber == 0) {
System.out.println("Paper versus rock. Paper covers rock. You win!");
playerWins++;
}else if (randomNumber == 1) {
System.out.println("Paper versus paper. It's a tie!");
}else if (randomNumber == 2) {
System.out.println("Paper versus scissors. Scissors cuts paper. You lose!");
computerWins++;
}else if (randomNumber == 3) {
System.out.println("Paper versus Lizard. Lizard eats paper. You lose!");
computerWins++;
}else {
System.out.println("Paper versus Spock. Paper disproves Spock. You win!");
playerWins++;
}
}
//if-else statements for user input = scissors
if (player.equals("scissors")) {
if (randomNumber == 0) {
System.out.println("Scissors versus rock. Rock smashes scissors. You lose!");
computerWins++;
}else if (randomNumber == 1) {
System.out.println("Scissors versus paper. Scissors cuts paper! You win!");
playerWins++;
}else if (randomNumber == 2) {
System.out.println("Scissors versus scissors. It's a tie!");
}else if (randomNumber == 3) {
System.out.println("Scissors versus Lizard. Scissors decapitates Lizard. You win!");
playerWins++;
}else {
System.out.println("Scissors versus Spock. Spock smashes scissors. You lose!");
computerWins++;
}
}
//if-else statements for user input = Lizard
if (player.equals("lizard")) {
if (randomNumber == 0) {
System.out.println("Lizard versus rock. Rock crushes Lizard. You lose!");
computerWins++;
}else if (randomNumber == 1) {
System.out.println("Lizard versus paper. Lizard eats paper. You win!");
playerWins++;
}else if (randomNumber == 2) {
System.out.println("Lizard versus scissors. Scissors decapitates Lizard. You lose!");
computerWins++;
}else if (randomNumber == 3) {
System.out.println("Lizard versus Lizard. It's a tie!");
}else {
System.out.println("Lizard versus Spock. Lizard poisons Spock. You win!");
playerWins++;
}
}
//if-else statements for user input = Spock
if (player.equals("spock")) {
if (randomNumber == 0) {
System.out.println("Spock versus rock. Spock vaporizes rock. You win!");
playerWins++;
}else if (randomNumber == 1) {
System.out.println("Spock versus paper. Paper disproves Spock. You lose!");
computerWins++;
}else if (randomNumber == 2) {
System.out.println("Spock versus scissors. Spock smashes scissors. You win!");
playerWins++;
}else if (randomNumber == 3) {
System.out.println("Spock versus Lizard. Lizard poisons Spock. You lose!");
computerWins++;
}else {
System.out.println("Spock versus Spock. It's a tie!");
}
}
}
}
}
Aucun commentaire:
Enregistrer un commentaire