Joseph Clay Joseph Clay - 2 months ago 9
Java Question

Rock Paper Scissors Lizards Spock

I need to print the number of user wins, losses, and ties in a program. It took me forever to resolve the looping issue, but I finally got it. I am new to Java, but have coded in HTML and other scripting languages before.

import java.io.InputStream;
import java.util.Random;
import java.util.Scanner;

public class RPSLS {

//AUTHOR: JOSEPH CLAY


//TODO make program continue until user chooses to stop.
//TODO then print # of user wins, losses, and ties.


public static void main(String[] args) {

//declare objects
String user;
String comp= "";
Random comprand;
String answer=null;


do
{



//scanner and random setup and imported
Scanner scan=new Scanner(System.in);
//Random gen=new Random();



//prompt to play game/instructions
System.out.println("Yo, let's play a game of Rock, Paper, Scissors, Lizard, Spock");
System.out.println("Please enter R for rock, P for paper, S for scissors, L for lizard, or V for Spock");



//User selection input
System.out.println("Pick your poison: ");
user=scan.next();

//Randomly generated # of 5
System.out.println();
comprand=new Random();
int x=comprand.nextInt(5)+1;



//translate random # to string
if(x==1)
comp="R";
else if(x==2)
comp="P";
else if(x==3)
comp="S";
else if(x==4)
comp="L";
else if(x==5)
comp="V";



//capitalize user selection
user=user.toUpperCase();



//print computer choice
System.out.println("Your adversary chose: "+comp);



//conditions of possible outcomes




//tie
if (user.equals(comp))
System.out.println("Tie!");


//if user enters r
else if (user.equalsIgnoreCase("R"))
if (comp.equals("S"))
System.out.println("Rock crushes scissors. You win!");
else if (comp.equals("P"))
System.out.println("Paper envelopes rock. You lose.");
else if (comp.equals("L"))
System.out.println("Rock destroys lizard. You win!");
else if (comp.equals("V"))
System.out.println("Spock crushes rock. You lose.");

//if user enters p
if (user.equalsIgnoreCase("P"))
if (comp.equals("R"))
System.out.println("Paper envelopes rock. You win!");
else if (comp.equals("S"))
System.out.println("Scissors cuts paper. You lose");
else if (comp.equals("L"))
System.out.println("Lizard eats paper. You lose.");
else if (comp.equals("V"))
System.out.println("Paper disproves Spock. You win!");

//if user enters s
if (user.equalsIgnoreCase("S"))
if (comp.equals("R"))
System.out.println("Rock crushes scissors. You lose.");
else if (comp.equals("P"))
System.out.println("Scissors cuts paper. You win!");
else if (comp.equals("L"))
System.out.println("Scissors eviscerates lizard. You win!");
else if (comp.equals("V"))
System.out.println("Spock vaporizes scissors. You lose.");

//if user enters l
if (user.equalsIgnoreCase("L"))
if (comp.equals("R"))
System.out.println("Rock crushes lizard. You lose.");
else if (comp.equals("P"))
System.out.println("Lizard eats paper. You win!");
else if (comp.equals("S"))
System.out.println("Scissors eviscerate lizard. You lose.");
else if (comp.equals("V"))
System.out.println("Lizard poisons Spock. You win!");

//if user enters v
if (user.equalsIgnoreCase("V"))
if (comp.equals("R"))
System.out.println("Spock crushes rock. You win!");
else if (comp.equals("P"))
System.out.println("Paper disproves Spock. You lose!");
else if (comp.equals("S"))
System.out.println("Spock vaporizes scissors. You win!");
else if (comp.equals("L"))
System.out.println("Lizard poisons Spock. You lose.");




System.out.println("Would you like to continue? (yes or no)");
answer = scan.next();
}

while(answer.equals("y"));

while (answer.equalsIgnoreCase("n"))
break;


}

}

Answer

Per your comments on your question, here is how you could implement keeping track of how many wins, losses, and ties the user has.

You declare variables to track each before you start your do loop. Since you want to count them I'd suggest a simple integer variable for each:

int wins = 0;
int losses = 0;
int ties = 0;

Then inside your loop you increment each counter when that situation occurs. For example:

     if (comp.equals("R")){
         System.out.println("Spock crushes rock. You win!");
         wins++;
     }
     else if (comp.equals("P")){
         System.out.println("Paper disproves Spock. You lose!");
         losses++;
     }

After your loop ends, i.e. after your while you then print these numbers.

while(answer.equals("y"));
System.out.println("You won "+wins+" times");
System.out.println("You lost "+losses+" times");
System.out.println("You tied "+ties+" times");

Lastly, some code review items for you. You generally never want to write code like this:

while(answer.equals("y"));

The reason is that if answer is null this will throw a NullPointerException at runtime. It's safer to write:

while("y".equals(answer));

In the code above "y" can never be null so you will never have a NullPointerException thrown by this code.

Secondly, hardcoding all of the possible scenarios is not the most efficient and maintainable way to write your program. Instead if you think in terms of objects (and you should since Java is an object oriented language) you could create an object to represent each possible choice and each object can contain others of its kind representing which choices will defeat it. Then you can simply so something like this:

if(userChoice.defeats(computerChoice)){
    //handle user win here
}else if(computerChoice.defeats(userChoice){
   //handle computer win here
}else{
   //handle tie here
}

Hope this helps you!

Comments