Results 1 to 10 of 10
Like Tree1Likes
  • 1 Post By Norm

Thread: Need help writing code for a rock paper scissors game

  1. #1
    loftus727 is offline Member
    Join Date
    Feb 2013
    Posts
    24
    Rep Power
    0

    Default Need help writing code for a rock paper scissors game

    this is what I have so far, it will not run correctly and I can not figure out how to fix it, please help.

    // ************************************************** **************
    // Rock.java
    //
    // Play Rock, Paper, Scissors with the user
    //
    // ************************************************** **************
    import java.util.Scanner;
    import java.util.Random;

    public class Rock
    {
    public static void main(String[] args)
    {
    String personPlay; //User's play -- "R", "P", or "S"
    String computerPlay = ""; //Computer's play -- "R", "P", or "S"
    int computerInt; //Randomly generated number used to determine
    //computer's play

    Scanner scan = new Scanner(System.in);
    Random generator = new Random();




    //Generate computer's play (0,1,2)
    computerInt = generator.nextInt(3);

    //Translate computer's randomly generated play to string using if //statements
    if (computerInt == 0)
    computerPlay = "R";
    if (computerInt == 1)
    computerPlay = "P";
    if(computerInt == 2)
    computerPlay = "S";

    //Get player's play from input-- note that this is stored as a string
    System.out.print("Enter your play: R, P, or S");
    personPlay = scan.next();


    //Make player's play uppercase for ease of comparison

    personPlay = personPlay.toUpperCase();

    //Print computer's play
    System.out.println( computerPlay);


    //See who won. Use nested ifs
    if (personPlay.equals(computerPlay))
    {
    System.out.println("It's a tie!");
    }
    else if (personPlay.equals("R")){
    if (computerPlay.equals("S")){
    System.out.println("Rock crushes scissors. You win!!");
    }
    else if (personPlay.equals("P")){
    if (computerPlay.equals("S")){
    System.out.println("Scissors cut Paper. You lose!!");
    }
    else if (personPlay.equals("S")){
    if (computerPlay.equals("P")){
    System.out.println("Scissors cut Paper. You win!!");
    }
    else if (personPlay.equals("R")){
    if (computerPlay.equals("P")){
    System.out.println("Rock breaks through Paper. You win!!");
    }
    else if (personPlay.equals("S")){
    if (computerPlay.equals("R")){
    System.out.println("Rock crushes scissors. You lose!!");
    }
    else if (personPlay.equals("P")){
    if (computerPlay.equals("R")){
    System.out.println("Rock breaks through Paper. You lose!!");
    }

    }
    }

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,271
    Rep Power
    25

    Default Re: Need help writing code for a rock paper scissors game

    it will not run correctly
    Please explain what the program does that is not correct. Also explain what you would like it to do differently.
    Copy the program's output and paste it here.

    Please edit the post and wrap the code in code tags.
    BB Code List - Java Programming Forum - Learn Java Programming
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    loftus727 is offline Member
    Join Date
    Feb 2013
    Posts
    24
    Rep Power
    0

    Default Re: Need help writing code for a rock paper scissors game

    The program is supposed to be an interactive game of rock, paper, scissors. Now I made changes and it will not even compile without errors.
    This is the output I am getting:


    ----jGRASP exec: javac -g Rock.java

    Rock.java:82: error: reached end of file while parsing
    }
    ^
    Rock.java:84: error: reached end of file while parsing
    2 errors

    ----jGRASP wedge2: exit code for process is 1.
    ----jGRASP: operation complete.

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,271
    Rep Power
    25

    Default Re: Need help writing code for a rock paper scissors game

    error: reached end of file while parsing
    Check that all the { }s are properly paired. Every { must have a } that goes with it.
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    loftus727 is offline Member
    Join Date
    Feb 2013
    Posts
    24
    Rep Power
    0

    Default Re: Need help writing code for a rock paper scissors game

    Okay I fixed that and now it compiles but when I run it some of the outcomes will not print the line that corresponds with it. For instance if the user picks Paper and the Computer picks Rock the line "Rock breaks through Paper. You lose!!" does not print, it just goes on to say operation complete.

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,271
    Rep Power
    25

    Default Re: Need help writing code for a rock paper scissors game

    Please copy the contents of the command prompt window from when you execute the program and post it here.
    Add some comments to it showing what the problem is.

    Please edit your post and wrap your code with code tags:
    [code]
    <YOUR CODE HERE>
    [/code]
    to get highlighting and preserve formatting.
    Last edited by Norm; 02-17-2013 at 10:34 PM.
    If you don't understand my response, don't ignore it, ask a question.

  7. #7
    loftus727 is offline Member
    Join Date
    Feb 2013
    Posts
    24
    Rep Power
    0

    Default Re: Need help writing code for a rock paper scissors game

    I am not sure if this is what you are asking me to do but:


    Java Code:
    // ****************************************************************
    //   Rock.java
    //
    //   Play Rock, Paper, Scissors with the user
    //          
    // ****************************************************************
    import java.util.Scanner;
    import java.util.Random;
    
    public class Rock
    {
        public static void main(String[] args)
        {
    	String personPlay;    //User's play -- "R", "P", or "S"
    	String computerPlay = "";  //Computer's play -- "R", "P", or "S"
    	int computerInt;      //Randomly generated number used to determine
    	                      //computer's play
    
        Scanner scan = new Scanner(System.in);
    	Random generator = new Random();
    
    
    
    
    	//Generate computer's play (0,1,2) 
    		computerInt = generator.nextInt(3);
    
    	//Translate computer's randomly generated play to string using if //statements
    	if (computerInt == 0)
    		computerPlay = "R";
    	if (computerInt == 1)
    		computerPlay = "P";
    	if(computerInt == 2)
    		computerPlay = "S";
    
    	//Get player's play from input-- note that this is stored as a string
    	System.out.print("Enter your play: R, P, or S");
    		personPlay = scan.next();
    
     
    	//Make player's play uppercase for ease of comparison
    
    	personPlay = personPlay.toUpperCase();
    
    	//Print computer's play
    	System.out.println( computerPlay);
    
    
    	//See who won.  Use nested ifs 
    	if (personPlay.equals(computerPlay))
    	{  
    	    System.out.println("It's a tie!");
    	}
                    //After this is where the problem occurs
    
    	else if (personPlay.equals("R")){
    	    if (computerPlay.equals("S"))
    		   System.out.println("Rock crushes scissors.  You win!!");
    	}
    	else if (personPlay.equals("P")){
    		 if (computerPlay.equals("S"))
    		 	System.out.println("Scissors cut Paper. You lose!!");
    	}
    	else if (personPlay.equals("S")){
    		 if (computerPlay.equals("P"))
    		 	System.out.println("Scissors cut Paper. You win!!");
    	}
    	else if (personPlay.equals("R")){
    		 if (computerPlay.equals("P"))
    		 	System.out.println("Rock breaks through Paper. You win!!");
    	}
    	else if (personPlay.equals("S")){
    		 if (computerPlay.equals("R"))
    		 	System.out.println("Rock crushes scissors.  You lose!!");
    	}
    	else if (personPlay.equals("P")){
    		 if (computerPlay.equals("R"))
    		 	System.out.println("Rock breaks through Paper. You lose!!");
    	}
    
    		
    		
    	}
    }

  8. #8
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,271
    Rep Power
    25

    Default Re: Need help writing code for a rock paper scissors game

    You need to consider the way if/else if works.
    The FIRST if that is true is the ONLY one that is executed. All the following else if tests are skipped.

    Java Code:
    	else if (personPlay.equals("P")){
    		 if (computerPlay.equals("S"))
    		 	System.out.println("Scissors cut Paper. You lose!!");
    	}
    If person chooses paper, this is the only code that will be executed. The if at line 76 will be skipped.

    You need to nest within the person's choice ALL of the possible computer choices and results.
    Last edited by Norm; 02-17-2013 at 11:16 PM.
    SurfMan likes this.
    If you don't understand my response, don't ignore it, ask a question.

  9. #9
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    902
    Rep Power
    2

    Default Re: Need help writing code for a rock paper scissors game

    Ah, now I see it, Norm got it right. After the first satisfactory if, all the others are skipped.

    Besides, when the player choses paper, he always loses :) I believe the rule is "Paper wraps stone".

  10. #10
    loftus727 is offline Member
    Join Date
    Feb 2013
    Posts
    24
    Rep Power
    0

    Default Re: Need help writing code for a rock paper scissors game

    Thank you for all your help it runs correctly now

Similar Threads

  1. Rock Paper Scissors Code...
    By Shaeman111 in forum New To Java
    Replies: 8
    Last Post: 02-08-2012, 03:20 AM
  2. Replies: 3
    Last Post: 02-17-2011, 02:42 AM
  3. Paper rock Scissors Game inheritance problem
    By kaanax in forum New To Java
    Replies: 7
    Last Post: 08-20-2010, 02:44 PM
  4. Need Help with Rock paper and Scissors Java Game
    By kingsun in forum New To Java
    Replies: 3
    Last Post: 11-17-2008, 03:35 AM
  5. Need help with Rock Paper Scissors Game
    By GettinGwap in forum New To Java
    Replies: 12
    Last Post: 10-19-2008, 06:15 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •