1 Replies - 716 Views - Last Post: 26 April 2015 - 08:17 PM Rate Topic: -----

#1 imaloser  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 26-April 15

PLEASE help me fix the following Rock, Paper, Scissors Program!

Posted 26 April 2015 - 06:46 PM

Okay, so this is the program I've written thus far. The biggest issue I'm having is counting the wins and losses from the rpsls method. I know that the line
int output = rpsls(retVal); 
isn't valid, but can't figure out a way to call the retVal from rpsls in. I've been at this all day and still can't make it work. Can anyone help me get this working?






import java.util.Scanner; 
public class Rpsls
{

   public static void main (String [] args) 
   {

  
      //while loop 
      Scanner input = new Scanner (System.in);
      int x = input.nextInt();
         while (x > 0 && x <= 5) 
         {
            //input gesture 
         
            System.out.println(" ROCK PAPER SCISSORS LIZARD SPOCK");
            System.out.println(" (1)   (2)     (3)     (4)    (5)");
            System.out.print("Enter your choice, or 0 to end: ");
            
                    
            //Call rpsls for inputted gesture       
            int gesture = input.nextInt();
            rpsls(gesture);
            
            //counting wins and losses
            int output = rpsls(retVal);
           
            if (output == 0) {
               ties++;
            }
            
            else if (output == 1) {
               wins++;
            }
            
            else {
               losses++;
            }
                     
         }
         
            //print sum of wins and losses 
            System.out.println("You won " + wins + " games, lost " + losses + " games, and tied " + ties + " games.");
         
   
   } 
   
   public static int rpsls(int gesture) 
   {
      //generate random gesture for computer 
      
      int attack = (int)(Math.random()*5);
      int retVal = 0;
      
      //decide who won 
        if (gesture == 1 && attack == 1) {
         System.out.println("You chose ROCK.");
         System.out.println("Computer chose ROCK.");
         System.out.println("It's a tie!");
         retVal = 0;
        }
        if (gesture == 1 && attack == 2) {
         System.out.println("You chose ROCK.");
         System.out.println("Computer chose PAPER.");
         System.out.println("PAPER covers ROCK.");
         System.out.println("Computer wins!");
         retVal = -1;
        }
        if (gesture == 1 && attack == 3) {
         System.out.println("You chose ROCK.");
         System.out.println("Computer chose SCISSORS.");
         System.out.println("ROCK crushes SCISSORS.");
         System.out.println("You win!");
         retVal = 1;
        }
        if (gesture == 1 && attack == 4) {
         System.out.println("You chose ROCK.");
         System.out.println("Computer chose LIZARD.");
         System.out.println("ROCK crushes LIZARD.");
         System.out.println("You win!");
         retVal = 1;
        }
        if (gesture == 1 && attack == 5) {
         System.out.println("You chose ROCK.");
         System.out.println("Computer chose SPOCK.");
         System.out.println("SPOCK vaporizes ROCK.");
         System.out.println("Computer wins!");
         retVal = -1;
        }
        if (gesture == 2 && attack == 1) {
         System.out.println("You chose PAPER.");
         System.out.println("Computer chose ROCK.");
         System.out.println("PAPER covers ROCK.");
         System.out.println("You win!");
         retVal = 1;
        }
        if (gesture == 2 && attack == 2) {
         System.out.println("You chose PAPER.");
         System.out.println("Computer chose PAPER.");
         System.out.println("It's a tie!");
         retVal = 0;
        }
        if (gesture == 2 && attack == 3) {
         System.out.println("You chose PAPER.");
         System.out.println("Computer chose SCISSORS.");
         System.out.println("SCISSORS cut PAPER.");
         System.out.println("Computer wins!");
         retVal = -1;
        }
        if (gesture == 2 && attack == 4) {
         System.out.println("You chose PAPER.");
         System.out.println("Computer chose LIZARD.");
         System.out.println("LIZARD eats PAPER.");
         System.out.println("Computer wins!");
         retVal = -1;
        }
        if (gesture == 2 && attack == 5) {
         System.out.println("You chose PAPER.");
         System.out.println("Computer chose SPOCK.");
         System.out.println("PAPER disproves SPOCK.");
         System.out.println("You win!");
         retVal = 1;
        }
        if (gesture == 3 && attack == 1) {
         System.out.println("You chose SCISSORS.");
         System.out.println("Computer chose ROCK.");
         System.out.println("ROCK crushes SCISSORS.");
         System.out.println("Computer wins!");
         retVal = -1;
        }
      
       if (gesture == 3 && attack == 2) {
        System.out.println("You chose SCISSORS.");
        System.out.println("Computer chose PAPER.");
        System.out.println("SCISSORS cut PAPER.");
        System.out.println("You win!");
        retVal = 1;
       }
       if (gesture == 3 && attack == 3) {
        System.out.println("You chose SCISSORS.");
        System.out.println("Computer chose SCISSORS.");
        System.out.println("It's a tie!");
        retVal = 0;
       }
       if (gesture == 3 && attack == 4) {
        System.out.println("You chose SCISSORS.");
        System.out.println("Computer chose LIZARD.");
        System.out.println("SCISSORS decapitate LIZARD.");
        System.out.println("You win!");
        retVal = 1;
       }
       if (gesture == 3 && attack == 5) {
        System.out.println("You chose SCISSORS.");
        System.out.println("Computer chose SPOCK.");
        System.out.println("SPOCK smashes SCISSORS.");
        System.out.println("Computer wins!");
        retVal = -1;
       }
       if (gesture == 4 && attack == 1) {
        System.out.println("You chose LIZARD.");
        System.out.println("Computer chose ROCK.");
        System.out.println("ROCK crushes LIZARD.");
        System.out.println("Computer wins!");
        retVal = -1;
       }
       if (gesture == 4 && attack == 2) {
        System.out.println("You chose LIZARD.");
        System.out.println("Computer chose PAPER.");
        System.out.println("LIZARD eats PAPER.");
        System.out.println("You win!");
        retVal = 1;
       }
       if (gesture == 4 && attack == 3) {
        System.out.println("You chose LIZARD.");
        System.out.println("Computer chose SCISSORS.");
        System.out.println("SCISSORS decapitate LIZARD.");
        System.out.println("Computer wins!");
        retVal = -1;
       }
       if (gesture == 4 && attack == 4) {
        System.out.println("You chose LIZARD.");
        System.out.println("Computer chose LIZARD.");
        System.out.println("It's a tie!");
        retVal = 0;
       }
       if (gesture == 4 && attack == 5) {
        System.out.println("You chose LIZARD.");
        System.out.println("Computer chose SPOCK.");
        System.out.println("LIZARD poisons SPOCK.");
        System.out.println("You win!");
        retVal = 1;
       }
       if (gesture == 5 && attack == 1) {
        System.out.println("You chose SPOCK.");
        System.out.println("Computer chose ROCK.");
        System.out.println("SPOCK vaporizes ROCK.");
        System.out.println("You win!");
        retVal = 1;
       } 
       if (gesture == 5 && attack == 2) {
        System.out.println("You chose SPOCK.");
        System.out.println("Computer chose PAPER.");
        System.out.println("PAPER disproves SPOCK.");
        System.out.println("Computer wins!");
        retVal = -1;
       }
       if (gesture == 5 && attack == 3) {
        System.out.println("You chose SPOCK.");
        System.out.println("Computer chose SCISSORS.");
        System.out.println("SPOCK smashes SCISSORS.");
        System.out.println("You win!");
        retVal = 1;
       }
       if (gesture == 5 && attack == 4) {
        System.out.println("You chose SPOCK.");
        System.out.println("Computer chose LIZARD.");
        System.out.println("LIZARD poisons SPOCK.");
        System.out.println("Computer wins!");
        retVal = -1;
       }
       if (gesture == 5 && attack == 5) {
        System.out.println("You chose SPOCK.");
        System.out.println("Computer chose SPOCK.");
        System.out.println("It's a tie!");
        retVal = 0;
       }        

      return retVal;
   }    
  
}



Is This A Good Question/Topic? 0
  • +

Replies To: PLEASE help me fix the following Rock, Paper, Scissors Program!

#2 DeveloperOfThings  Icon User is offline

  • D.I.C Head

Reputation: 26
  • View blog
  • Posts: 89
  • Joined: 12-April 15

Re: PLEASE help me fix the following Rock, Paper, Scissors Program!

Posted 26 April 2015 - 08:17 PM

//Call rpsls for inputted gesture      
022
	            int gesture = input.nextInt();
023
	            rpsls(gesture);
024
	             
025
	            //counting wins and losses
026
	            int output = rpsls(retVal);



rpsls returns a value, so you need to use that return value in some way, in your case it looks like you want to save it into int output. On line 23 you call rpsls but you don't save it into a variable, so it's useless to call it since the whole point of calling it is to return some int (in this case indicating 1 for win, 0 for tie, -1 for loss) so that you can use that number to increase wins, losses, or ties.

Then you try call rpsls and save it into int output which is good, but you pass retValue, and retValue is only a variable inside the rpsls method so outside of that method it doesn't exist.

Look at the method and see that it accepts an int that represents a gesture. So call rpsls like you did the first time, and save it into int output.

So delete line 23, because it isn't doing anything, and change line 26 to pass the right variable.

This post has been edited by DeveloperOfThings: 26 April 2015 - 08:19 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1