Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #29122

Re: Help with paper, rock, scissors game

Newsgroups comp.lang.java.programmer
Date 2014-07-19 00:34 -0700
References <ad9edd21-86dd-4476-bf07-822ee29796a2@googlegroups.com> <TPqdnSOT7IVphlfOnZ2dnUVZ8tadnZ2d@giganews.com>
Message-ID <3f072f43-2b61-4521-8e9f-9c406dfb8dd9@googlegroups.com> (permalink)
Subject Re: Help with paper, rock, scissors game
From scottwd01@gmail.com

Show all headers | View raw


On Saturday, July 19, 2014 12:19:16 AM UTC-7, Leif Roar Moldskred wrote:
> scott wrote:
> 
> > 
> 
> >
> 
> > 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.
> 
> 
> 
> 
> 
> The overall gist of your program is right, but you have a number
> 
> of syntax errors so that you get 17 errors when you add the missing
> 
> close bracket is not unexpected.
> 
> 
> 
> One tip, particularly when you start out, is to label your close
> 
> bracket -- like this:
> 
> 
> 
> public class Example {
> 
>   public static void main( String[] args ) {
> 
>     if( 1 > 2 ) {
> 
> 
> 
>     } // end if -- mathematical impossibility
> 
>     else {
> 
> 
> 
>     } // end else -- mathematical certainity: always happens
> 
>   } // end main()
> 
> } // end class Example
> 
> 
> 
> 
> 
> I've not got a java compiler at hand, so I might overlook
> 
> a few errors, but here's what I've eyeballed of errors:
> 
> 
> 
> >     char r = R;
> 
> >     char p = P;
> 
> >     char s = S;
> 
> 
> 
> This bit is wrong. Just R, P and S without any quotation marks around
> 
> them would be a reference to _variables_ namend R, P and S, and as
> 
> these don't exist the program will complainq. When you mean the
> 
> _character_ R, you need to enclose it in single quotes like this:
> 
> 
> 
>   char r = 'R';
> 
> 
> 
> Of course, as you don't use the variables r, p or s anywhere in the
> 
> program, you don't actually need these three lines at all.
> 
> 
> 
> > 
> 
> >  //Start of while loop that asks p1 and p2 for R,P or S.    
> 
> >     while (playCount < 3);
> 
> 
> 
> This bit is wrong -- the semicolon marks an end of the while loop,
> 
> and the following open bracket then starts a block of code that has
> 
> nothing to do with the while. basically, you've written this:
> 
> 
> 
>   while( playCount < 3 ) {
> 
>     /* Do nothing */
> 
>   }
> 
>  
> 
>   {
> 
>      System.out.print("Player 1: Please enter either (R)ock, (P)aper, or (S)cissors: ");
> 
>      player1 = scan.nextLine() .toUpperCase() .charAt(0);
> 
>      ...
> 
>   }
> 
> 
> 
> As playCount is never incremented inside the while loop, the loop
> 
> will never exit but run forever.
> 
>  
> 
> > //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 ;
> 
> >     } 
> 
> 
> 
> Here you have used the wrong quotation marks. In some languages, both single and double
> 
> quotation marks can be used to enclose strings -- in Java there's a difference. Double
> 
> quotation marks enclose strings, and single quotation marks enclose chars. Since player1
> 
> and player2 are variables of type char, you need to use single quotation marks here:
> 
> 
> 
>  if( player1 == 'P' ) {
> 
>    ...
> 
>  } 
> 
> 
> 
> >     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 ;
> 
> >   } // [1]    
> 
> > } // [2]
> 
> 
> 
> Now, the close bracket I've annotated with [1] closes the last "else if" and the
> 
> one I've annotated with [2] closes the method, so you need one more to close the
> 
> class.
> 
> 
> 
> Additionaly, as written, the scoring method has a problem in that it doesn't
> 
> return a value for every possibile case, so that won't compile. If player1
> 
> enters something besides 'P', 'R' or 'S' you'll "skip over" the if, the
> 
> first else if and the second else if, and hit the end of the method where 
> 
> there's no return statement.
> 
> 
> 
> You'll want to either throw an exception or return an error value at that
> 
> point. (If you choose to return an error value you should check for that
> 
> value in the main method.)
> 
> 
> 
> Hope this helps.
> 
> 
> 
> 
> 
> P.S. You might get some posters here who will complain about the style
> 
> of your code (how it's formatted and how it looks rather than what it
> 
> does.) Don't worry about that -- right now you should focus on getting
> 
> the code to do what you want it to do. Programming style is important
> 
> later, but right now it'll just be one more thing to confuse you.

Wow, thanks Lief!!  You've helped a ton!  You've given me a lot to go over so I'm gonna go back and try and implement your suggestions.   

As for my formatting, I'm sure it's a mess, but I'm only three days into this class and I totally agree, I need the help with getting the code to actually work first.  I appreciate that you explained things to not just gave me answers.  I got a bunch of nasty responses on another forum and zero help so I really appreciate yours.  

P.S.  Is this a good forum for these type of questions?  

Scott

Back to comp.lang.java.programmer | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

Help with paper, rock, scissors game scottwd01@gmail.com - 2014-07-18 23:35 -0700
  Re: Help with paper, rock, scissors game leif.roar@invalid.com (Leif Roar Moldskred) - 2014-07-19 02:19 -0500
    Re: Help with paper, rock, scissors game scottwd01@gmail.com - 2014-07-19 00:34 -0700
      Re: Help with paper, rock, scissors game leif.roar@invalid.com (Leif Roar Moldskred) - 2014-07-19 03:49 -0500
  Re: Help with paper, rock, scissors game lipska the kat <lipskathekat@example.com> - 2014-07-19 08:39 +0100
    Re: Help with paper, rock, scissors game scottwd01@gmail.com - 2014-07-19 22:38 -0700
      Re: Help with paper, rock, scissors game markspace <markspace@nospam.nospam> - 2014-07-19 23:12 -0700
        Re: Help with paper, rock, scissors game Martin Gregorie <martin@address-in-sig.invalid> - 2014-07-20 10:49 +0000
          Re: Help with paper, rock, scissors game scottwd01@gmail.com - 2014-07-20 18:37 -0700
            Re: Help with paper, rock, scissors game scottwd01@gmail.com - 2014-07-20 18:47 -0700
              Re: Help with paper, rock, scissors game Martin Gregorie <martin@address-in-sig.invalid> - 2014-07-21 12:01 +0000
                Re: Help with paper, rock, scissors game ĵess stone <jess.st0ne@gmail.com> - 2014-07-21 21:59 +0000
                Re: Help with paper, rock, scissors game Martin Gregorie <martin@address-in-sig.invalid> - 2014-07-21 22:22 +0000
                Re: Help with paper, rock, scissors game ĵess stone <jess.st0ne@gmail.com> - 2014-07-21 22:35 +0000
                Re: Help with paper, rock, scissors game "Chris Uppal" <chris.uppal@metagnostic.REMOVE-THIS.org> - 2014-07-22 13:56 +0100
        Re: Help with paper, rock, scissors game lipska the kat <lipskathekat@example.com> - 2014-07-20 12:29 +0100
          Re: Help with paper, rock, scissors game Scott Dunning <scottwd01@gmail.com> - 2014-07-20 20:10 -0700
  Re: Help with paper, rock, scissors game Roedy Green <see_website@mindprod.com.invalid> - 2014-07-19 02:51 -0700

csiph-web