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 11 of 11

Thread: Help with paper rock scissors game

  1. #1
    Junior Member
    Join Date
    Jul 2014
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Help with paper rock scissors game

    Hello,

    I have a couple quick questions. First I just want to make sure I’m in the right forum? I am taking a class in college and it’s beginning Java.

    I had a paper rock, scissors game as an assignment that was due a couple days ago. I ended up having to submit and not working file but, I’d like to figure out what I’m doing wrong for future assignments.

    I feel like it’s correct and I got it down to just one error (reached end of file while parsing). I think that means that it needs an extra } at the end, but if I add one I go from one to 17 errors. I’m thinking maybe there is something wrong with my while loop or how I created a new function (or method in java, I had a class in python last semester so I’m not 100% sure I got the functions in java down yet). What makes sense to me and how I understand it is, the function itself must be created outside of main, but actually called in main? Also, are all of the functions going to be in the class RPS_Game? We haven’t really learned classes yet this is just my second week.

    Here is my code, any help is greatly appreciated!


    import java.util.Random;
    import java.util.Scanner;
     
    public class RPS_Game
    {
     public static void main(String[] args)
     {
     
    //Variables  
         char player1, player2;
         char r = R;
         char p = P;
         char s = S;
         int winner, p1Score, p2Score, playCount;
     //Setting the p1 and p2 and playCount equal to 0. 
         p1Score = p2Score = playCount = 0;
     
     
         Scanner scan = new Scanner(System.in);
     
      //Start of while loop that asks p1 and p2 for R,P or S.    
         while (playCount < 3);
         {
         System.out.print("Player 1: Please enter either (R)ock, (P)aper, or (S)cissors: ");
         player1 = scan.nextLine() .toUpperCase() .charAt(0);
     
         System.out.print("Player 2: Please enter either (R)ock, (P)aper, or (S)cissors: ");
         player2 = scan.nextLine().toUpperCase() .charAt(0);        
     
     //Add one to the playCount   
         playCount ++;
     
    //Calling the winningPlayer function    
         int winner = winningPlayer(player1, player2);
     
         if (winner = 0) {
             System.out.println("Tie! Nobody wins.");
         } 
         else if (winner = 1) {
             System.out.println("Player 1 wins!");  
             p1Score ++;
          }   
         else if (winner = 2) {
             System.out.println("Player 2 wins");
             p2Score ++;
          }
     
         System.out.println("\nScores after this play:\nPlayer 1:" + p1score + "\nPlayer 2:" + p2Score + "\nThanks for playing!");      
         }
     }  
     
     
     
    //Defining the winningPlayer function that decides which player wins or if there is a tie.    
     public static int winningPlayer(char player1, char player2)
     {
     
         if (player1 == "P") {
             if (player2 == "P")
                 return 0;
             else if (player2 == "R")
                 return 1;
             else 
                 return 2 ;
         } 
         else if (player1 == "R") {
             if (player2 == "R")
                 return 0;
             else if (player2 == "S")
                 return 1;
             else 
                 return 2;            
         } 
         else if (player1 == "S") {
             if (player2 == "S")
                 return 0;
             else if (player2 == "P")
                 return 1;
             else 
                 return 2 ;
       }     
    }
    Last edited by sjud9227; July 19th, 2014 at 12:17 AM.


  2. #2
    Member
    Join Date
    Mar 2012
    Location
    United States
    Posts
    118
    My Mood
    Inspired
    Thanks
    1
    Thanked 33 Times in 31 Posts

    Default Re: Help with paper rock scissors game

    Hi sjud9227,
    I suggest you go here: The Java™ Tutorials and focus on the section "Trails Covering the Basics". There are syntax errors all over the place including incorrect variable initialization, incorrect operator usage, and, as you had said, a missing }. I think your logic is probably fine but translating that into something Java can compile is the real issue. Take a look through the documents on that website and I think you will be able to work through it.

  3. #3
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Help with paper rock scissors game

    Please edit your post and wrap your code with code tags:
    [code=java]
    YOUR CODE GOES HERE
    [/code]
    to get highlighting and preserve formatting.

    One problem I see is the use of == to compare String objects. You should use the equals() method.
    If you don't understand my answer, don't ignore it, ask a question.

  4. #4
    Junior Member
    Join Date
    Jul 2014
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help with paper rock scissors game

    Yeah like I mentioned I'm super new I've only had three classes so far. Thanks though that was very helpful.

    --- Update ---

    Gotcha, thank you.

  5. #5
    Member Ada Lovelace's Avatar
    Join Date
    May 2014
    Location
    South England UK
    Posts
    414
    My Mood
    Angelic
    Thanks
    27
    Thanked 61 Times in 55 Posts

    Default Re: Help with paper rock scissors game

    Just to add one further point to the suggestions above - now is a great time to
    learn how to use a debugger - it is a tool built into most if not all Java IDE's that can
    aid you greatly in finding errors in a program. You can 'step through' the code line by line
    and set breakpoints at places you want execution to terminate. There are loads of helpful
    documents online about it - and even the IDE help filed should have some hints too.
    Just my two cents.

    Wishes Ada xx
    If to Err is human - then programmers are most human of us all.
    "The Analytical Engine offers a new, a vast, and a powerful language . . .
    for the purposes of mankind
    ."
    Augusta Ada Byron, Lady Lovelace (1851)

  6. #6
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Help with paper rock scissors game

    Welcome to the forum! Please read this topic to learn how to post code in code or highlight tags and other useful info for new members.

  7. #7
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Help with paper rock scissors game

    Please copy the full text of any error messages and paste it here.
    If you don't understand my answer, don't ignore it, ask a question.

  8. #8
    Junior Member pyler's Avatar
    Join Date
    Sep 2012
    Posts
    23
    My Mood
    Busy
    Thanks
    3
    Thanked 2 Times in 2 Posts

    Default Re: Help with paper rock scissors game

    Just a suggestion.
    Make a player class that takes a name and the player's selection.
    Have the player class getName, getResponse ,incrementScore, and getScore methods.
    then include a compare method in your other class (the one with the main method) that takes in two players and returns the winning player.
    But you should check to see if the player's responses are equal in you main method before comparing the players.
    Lastly, it would be nice to have score board that displays the scores of the two players. Maybe a method in you class?

    It would simplify things a bit.

    skeleton:
    main(){
    Player player1, player2
    Player winner;
    while(continue==true)
    if(player.getResponse().equals(player.getResponse( ))) it's a draw. Do players get equal points? or none? It's your chose.
    winner = compare(player1,player2);
    winner.incrementScore();
    displayResults();
    continue = userinput;
    }
    Last edited by pyler; July 19th, 2014 at 10:23 PM.

  9. The Following User Says Thank You to pyler For This Useful Post:

    GregBrannon (July 20th, 2014)

  10. #9
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Help with paper rock scissors game

    Also posted here.

  11. #10
    Junior Member
    Join Date
    Jul 2014
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help with paper rock scissors game

    Quote Originally Posted by pyler View Post
    Just a suggestion.
    Make a player class that takes a name and the player's selection.
    Have the player class getName, getResponse ,incrementScore, and getScore methods.
    then include a compare method in your other class (the one with the main method) that takes in two players and returns the winning player.
    But you should check to see if the player's responses are equal in you main method before comparing the players.
    Lastly, it would be nice to have score board that displays the scores of the two players. Maybe a method in you class?

    It would simplify things a bit.

    skeleton:
    main(){
    Player player1, player2
    Player winner;
    while(continue==true)
    if(player.getResponse().equals(player.getResponse( ))) it's a draw. Do players get equal points? or none? It's your chose.
    winner = compare(player1,player2);
    winner.incrementScore();
    displayResults();
    continue = userinput;
    }
    Thank you!! That helps a lot! Yeah I posted to a couple forums Greg, I don't always receive answers so sometimes it helps to post at different places. Thanks again for all of your help! Although this assignment has already passed I want to get it working to hopefully help me with future assignments.

  12. #11
    Junior Member
    Join Date
    Jun 2014
    Posts
    19
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Help with paper rock scissors game

    After correcting loads of errors I got it working within 10 minutes! Great program, do you have it working yet?

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. Creating a GUI for a rock, paper, scissors game
    By nmg13 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: December 11th, 2013, 02:09 PM
  4. Replies: 4
    Last Post: February 15th, 2013, 04:19 PM