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


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

Re: Help with paper, rock, scissors game

Date 2014-07-19 08:39 +0100
From lipska the kat <lipskathekat@example.com>
Newsgroups comp.lang.java.programmer
Subject Re: Help with paper, rock, scissors game
References <ad9edd21-86dd-4476-bf07-822ee29796a2@googlegroups.com>
Message-ID <bK6dnRhyD_JdvVfOnZ2dnUVZ7qqdnZ2d@bt.com> (permalink)

Show all headers | View raw


On 19/07/14 07:35, scottwd01@gmail.com wrote:
> Hello,
>
> I have a couple quick questions.  FYI, I am taking a class in college and it's beginning Java and this is only my second week, so I'm super new.  Hopefully this is the correct place to post my java related questions.  If not, if you could point me in the right direction I'd appreciate it.  If so, here are my questions.

It is :-)
>
> I had a paper rock, scissors game as an assignment that was due a couple days ago.
> I ended up having to submit an not working file but, I'd like to figure out what I'm doing wrong for future
 > assignments.
>
> I feel like it's correct

It isn't :-(

> 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!

OK

The best way to approach this is to show you some code that compiles
There are a number of problems I have highlighted with a /* */ comment 
block.

Have a look at the code and see if you can compile it
then try and understand the changes

Please note I have only checked if this compiles and starts up
I have NOT checked it for game correctness.

Are you using an IDE or a text editor and the command line?

/* compiles and runs */

import java.util.Random; /* not required but does not cause an error */
import java.util.Scanner;

public class RPS_game{
	
  public static void main(String[] args){

	 //Variables
      char player1, player2;

      /* These should be characters not Strings */
      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) /* no semicolon */
      {
	     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
	     winner = winningPlayer(player1, player2);
	
	     /* should be == not = */
	     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 ++;
	      }                                                       /* 
uppercase S in p1Score*/
	     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){
		
	     /* explicit result to return */
		 int result = 0;
		
		 /* should be characters not Strings */
	     if (player1 == 'P') {
	         if (player2 == 'P')
	             result = 0;
	         else if (player2 == 'R')
	        	 result = 1;
	         else
	        	 result = 2 ;
	     }
	     else if (player1 == 'R') {
	         if (player2 == 'R')
	        	 result = 0;
	         else if (player2 == 'S')
	        	 result = 1;
	         else
	             result = 2;
	     }
	     else if (player1 == 'S') {
	         if (player2 == 'S')
	        	 result = 0;
	         else if (player2 == 'P')
	        	 result = 1;
	         else
	        	 result = 2 ;
	   }
	   return result;
	}
} /* need a final } */


/* end */



> 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 ;
>     }
> }
>


-- 
lipska the kat - treacherous feline.
Proudly nominated for IPOTY 2014 - LIGAF
GNU/Linux user #560883 - linuxcounter.net

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