Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 10 of 10

Thread: Rock Paper Scissors issue

  1. #1
    Junior Member
    Join Date
    Jun 2014
    Location
    Northern Utah
    Posts
    13
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Default Rock Paper Scissors issue

    As usual i've waited until the last minute to work on this and now i'm stuck.
    I'm not sure what is causing the error.
    Any help would be greatly appreciated.
    PS i'm not very Java savvy so maybe dumb down the explanations a little?

    thanks




    /******************
    *R/P/S Program
    *Eddy Caraballo
    ******************/
    import java.util.Scanner;
    import java.util.Random;
    public class RockPaperScissors 
    {
        public static void main (String[] args) 
        {    
            int cChoice;                                        // variable for computers choice (R/P/S)
            int pChoice = 0;                                    // Holds converted choice (R=1, P=2, S=3)
            int cScore = 0, pScore = 0, tie = 0, rounds = 0;    // Initialised variables for score keeping (c = computer, p = player) plus keeps track of number of rounds played
            String loop="yes";                                  // Starts our loop
            Scanner input = new Scanner(System.in);             // Creates scanner object
            Random rgen = new Random();
            System.out.println("Rock-Paper-Scissors time!  (sung like peanut butter jelly time)");
     
            while (loop.equals("yes"))                          // This loop keeps our game going only while our string.loop is equal to 'yes'
            { 
                cChoice=rgen.nextInt(3)+1;
                System.out.println("Enter Rock, Paper or Scissors");
                String pInput = input.nextLine();               // variable for players choice (R/P/S) 
                if (pChoice.equals("Rock") || pChoice.equals("Paper") || pChoice.equals("Scissors")) // Ensures player has entered the correct choice for the game to continue
                {
                    System.out.println("");
     
                    if (pChoice.equals("Rock"))                // This converts pChoice to the numeric value for "Rock"
                    {
                        pChoice = 1;
                    }
                    if (pChoice.equals("Paper"))              // This converts pChoice to the numeric value for "Rock"
                    {
                        pChoice = 2;
                    }
                    if (pChoice.equals("Scissors"))          // This converts pChoice to the numeric value for "Rock"
                    {
                        pChoice = 3;
                    }
     
                    if (pChoice == cChoice)                  // Takes care of Ties
                    { 
                        System.out.println("Tie Game!");
                        System.out.println("");
                        tie++;
                        rounds++;
                    } else                                      // Accounts for scoring for non-tie scenarios
                    {
                        if (cChoice==1 && pChoice==3)           // Computer picks Rock and player has Scissors - adds point to score and rounds
                        {
                            System.out.println("Computer picked Rock!");
                            System.out.println("Rock beats Scissors!");
                            System.out.println("**Computer Wins!**");
                            System.out.println("");
                            cScore++;
                            rounds++;
                        } 
     
                        if (cChoice==1 && pChoice==2)           // Computer picks Rock and player has Paper - adds point to score and rounds
                        {
                            System.out.println("Computer picked Rock!");
                            System.out.println("Paper beats Rock!");
                            System.out.println("**Player Wins!**");
                            System.out.println("");
                            pScore++;
                            rounds++;
                        } 
     
                        if (cChoice==2 && pChoice==3)           // Computer picks Paper and player has Scissors - adds point to score and rounds
                        {
                            System.out.println("Computer picked Paper!");
                            System.out.println("Scissors beats Paper!");
                            System.out.println("**Player Wins!**");
                            System.out.println("");
                            pScore++;
                            rounds++;
                        } 
     
                        if (cChoice==2 && pChoice==1)           // Computer picks Paper and player has Rock - adds point to score and rounds
                        {
                            System.out.println("Computer picked Paper!");
                            System.out.println("Paper beats Rock!");
                            System.out.println("**Computer Wins!**");
                            System.out.println("");
                            cScore++;
                            rounds++;
                        } 
     
                        if (cChoice==3 && pChoice==1)           // Computer picks Scissors and player has Rock - adds point to score and rounds
                        {
                            System.out.println("Computer picked Scissors!");
                            System.out.println("Rock beats Scissors!");
                            System.out.println("**Player Wins!**");
                            System.out.println("");
                            pScore++;
                            rounds++;
                        } 
     
                        if (cChoice==3 && pChoice==2)          // Computer picks Scissors and player has Paper - adds point to score and rounds
                        {
                            System.out.println("Computer picked Scissors!");
                            System.out.println("Scissors beats Paper!");
                            System.out.println("**Computer Wins!**");
                            System.out.println("");
                            cScore++;
                            rounds++;
                        } 
                    }                            
                } else                                          // end the game
                {
                    System.out.println ("Sorry, you didn't pick Rock, Paper, or Scissors. The game will end now.");
                    System.out.println ("Here are the final scores after " + rounds +" rounds:");
                    System.out.println ("You\tComputer\tTies");
                    System.out.println (" "+ pScore +"\t   " + cScore + "\t\t " + tie);
                    loop = "no";                               // terminates the while loop keeping the game going
                }
            }    
        }
    }


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Rock Paper Scissors issue

    What error are you receiving?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Junior Member
    Join Date
    Jun 2014
    Location
    Northern Utah
    Posts
    13
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Default Re: Rock Paper Scissors issue

    ----jGRASP exec: javac -g RockPaperScissors.java
     
    RockPaperScissors.java:24: error: int cannot be dereferenced
                if (pChoice.equals("Rock") || pChoice.equals("Paper") || pChoice.equals("Scissors")) // Ensures player has entered the correct choice for the game to continue
                           ^
    RockPaperScissors.java:24: error: int cannot be dereferenced
                if (pChoice.equals("Rock") || pChoice.equals("Paper") || pChoice.equals("Scissors")) // Ensures player has entered the correct choice for the game to continue
                                                     ^
    RockPaperScissors.java:24: error: int cannot be dereferenced
                if (pChoice.equals("Rock") || pChoice.equals("Paper") || pChoice.equals("Scissors")) // Ensures player has entered the correct choice for the game to continue
                                                                                ^
    RockPaperScissors.java:28: error: int cannot be dereferenced
                    if (pChoice.equals("Rock"))                // This converts pChoice to the numeric value for "Rock"
                               ^
    RockPaperScissors.java:32: error: int cannot be dereferenced
                    if (pChoice.equals("Paper"))              // This converts pChoice to the numeric value for "Rock"
                               ^
    RockPaperScissors.java:36: error: int cannot be dereferenced
                    if (pChoice.equals("Scissors"))          // This converts pChoice to the numeric value for "Rock"
                               ^
    6 errors

  4. #4
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Rock Paper Scissors issue

    Well, the error says it all. Your pChoice variable is an int. You're trying to compare it to a String. That's not going to work.

    Hint: You also seem to ignore your pInput variable.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  5. The Following User Says Thank You to KevinWorkman For This Useful Post:

    Heri623 (June 16th, 2014)

  6. #5
    Member Abhilash's Avatar
    Join Date
    Jun 2014
    Location
    Kolkata, West Bengal, INDIA
    Posts
    108
    My Mood
    Busy
    Thanks
    5
    Thanked 10 Times in 10 Posts

    Default Re: Rock Paper Scissors issue

    Exactly! pChoice is an integer variable which you are comparing with string "Rock", "Paper", or "Scissor".

    Get it.
    Last edited by Abhilash; June 16th, 2014 at 11:44 AM.

  7. The Following User Says Thank You to Abhilash For This Useful Post:

    Heri623 (June 16th, 2014)

  8. #6
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Rock Paper Scissors issue

    I was trying to give OP a nudge in the right direction so he could figure it out. Simply posting the code solution isn't always the best way to help somebody learn.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  9. #7
    Member Abhilash's Avatar
    Join Date
    Jun 2014
    Location
    Kolkata, West Bengal, INDIA
    Posts
    108
    My Mood
    Busy
    Thanks
    5
    Thanked 10 Times in 10 Posts

    Default Re: Rock Paper Scissors issue

    Okay! I'm sorry. Post has been edited.

  10. #8
    Junior Member
    Join Date
    Jun 2014
    Location
    Northern Utah
    Posts
    13
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Default Re: Rock Paper Scissors issue

    I understand the what you're saying but I am hitting a wall as to what I need to do .
    Do I need to Parse the integer back to a string?

  11. #9
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Rock Paper Scissors issue

    What variable holds the value you want to compare?
    What do you want to compare that variable's contents against?

    Most comparisons are done with operands of the same type:
    int vs int
    String vs String
    If you don't understand my answer, don't ignore it, ask a question.

  12. The Following User Says Thank You to Norm For This Useful Post:

    Heri623 (June 17th, 2014)

  13. #10
    Junior Member
    Join Date
    Jun 2014
    Location
    Northern Utah
    Posts
    13
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Default Re: Rock Paper Scissors issue

    Thanks for all the help.
    Finally got it figured out.
    Sorry for being so dense.

Similar Threads

  1. rock paper scissors
    By Audz in forum What's Wrong With My Code?
    Replies: 3
    Last Post: February 18th, 2014, 09:41 PM
  2. ROCK, PAPER, SCISSORS GAME.
    By kofiachie in forum What's Wrong With My Code?
    Replies: 3
    Last Post: December 21st, 2013, 07:24 AM
  3. Help Improve My Rock,Paper,Scissors
    By Emperor_Xyn in forum Java Theory & Questions
    Replies: 3
    Last Post: December 16th, 2011, 10:34 PM
  4. Rock paper scissors project
    By katie_gsu in forum Loops & Control Statements
    Replies: 1
    Last Post: November 28th, 2011, 02:34 PM