Results 1 to 7 of 7
  1. #1
    CYANiDE is offline Member
    Join Date
    Sep 2009
    Posts
    6
    Rep Power
    0

    Default Rock, Paper, Scissor Game Help

    I've run into a few problems with this one. I've tried my best following the directions. I can't figure out how to properly use "switch" since we haven't really put it to use in class. I want to create random integers (this I know how to do) that will become Strings (letters converted to strings). How would I go about converting the integers to "strings" from the alphabet?

    Also, if I comment out the switch statement I get a compile error about my else and if states for the game.

    Help with any details you can.

    Thanks!

    Java Code:
    /*Rock, Paper, Scissors
    
    $ java Rock
    Enter your play: R, P, or S
    r
    Computer play is S
    Rock crushes scissors, you win!
    
    Note that the user should be able to enter either upper or lower case r, p, and s. The user's play is stored as a 
    string to make it easy to convert whatever is entered to upper case. Use a switch statement to convert the randomly 
    generated integer for the computer's play to a string.*/
    
    // ****************************************************************
    //   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();
    
    	//Get player's play -- note that this is stored as a string
    	//Make player's play uppercase for ease of comparison
    	//Generate computer's play (0,1,2)
    	//Translate computer's randomly generated play to string
    	switch (computerInt)
    {
    
    	}
    
    	//Print computer's play
    	//See who won.
    	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 (computerPlay.equals("P"))
    		System.out.println ("Paper eats rock. You lose!!");
    		}
    	else if (personPlay.equals("P"))
    		{
    	    if (computerPlay.equals("S"))
    		System.out.println("Scissor cuts paper.  You lose!!");
    	    else (personPlay.equals("R"))
    		System.out.println ("Paper eats rock. You win!!");
    		}
    	else if (personPlay.equals("S"))
    		{
    		 if (computerPlay.equals("P"))
    		System.out.println ("Scissor cuts paper. You win!!");
    		 else (computerPlay.equals("R"))
    		System.out.println ("Rock breaks scissors. You lose!!");
    		}
        }
    }

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,304
    Blog Entries
    7
    Rep Power
    20

    Default

    You don't need if-statements not switch-statements; for such a small game you can create simple decision tables. Assume that each player selects a 0, a 1 or a 2; 1 wins from 0, 2 wins from 1 and 0 wins from 2; this is your rock-scissors-paper game. For a simple 3x3 matrix; assume the computer's choice is a row and the human's choice is a column; the value of a matrix cell indicates which player won: -1: computer won, 1: human won, 0: a draw.

    This is what the decision table looks like:

    Java Code:
    int[][] decision= { { 0, 1, -1 }, { -1, 0, 1 }, { 1, -1, 0 } };
    given a computer move i and a human move j, the value decision[i][j] shows you the outcome of the game.

    Of course making the human move a 0, 1 or 2 is so computeresque; we want the user to type a p, s or r (in this order). The following little method accepts a character and returns -1 if the character is illegal or 0, 1 or 2:

    Java Code:
    int input(char c) {
       int i= "psr".indexOf(c);
       retur (i >= 0)?i:"PSR".indexOf(c);
    }
    The computer makes a move 0, 1 or 2 and the following method returns the associated character (an uppercase letter):

    Java Code:
    char computer(int i) {
       return "PSR".charAt(i);
    }
    Does this help a bit?

    kind regards,

    Jos

  3. #3
    CYANiDE is offline Member
    Join Date
    Sep 2009
    Posts
    6
    Rep Power
    0

    Default

    Well, I had some more specific guidelines to follow where I couldn't use decision tables. I used the switch statement to create the computer moves and the input of the user was capitalized for comparison.

    I already finished and and will post the program in a few hours since its saved on another computer.

    Thanks for the help though! I appreciate it. That decision table would make it so much easier but unfortunately we haven't learned it so we can't use it in our assignment.

  4. #4
    CYANiDE is offline Member
    Join Date
    Sep 2009
    Posts
    6
    Rep Power
    0

    Default

    As promised:
    Java Code:
    /*Rock, Paper, Scissors
    
    Program Rock.java contains a skeleton for the game Rock, Paper, Scissors. Open it and save it to your directory. 
    Add statements to the program as indicated by the comments so that the program asks the user to enter a play, 
    generates a random play for the computer, compares them and announces the winner (and why). For example, one run 
    of your program might look like this: 
    
    $ java Rock
    Enter your play: R, P, or S
    r
    Computer play is S
    Rock crushes scissors, you win!
    
    Note that the user should be able to enter either upper or lower case r, p, and s. The user's play is stored as a 
    string to make it easy to convert whatever is entered to upper case. Use a switch statement to convert the randomly 
    generated integer for the computer's play to a string.*/
    
    // ****************************************************************
    //   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();
    	
    	System.out.println ("Enter R for Rock, P for Paper, S for Scissors: "); //Get player's play -- note that this is stored as a string
    	personPlay = scan.next();
    	
    	personPlay = personPlay.toUpperCase();
    	
    	computerInt = generator.nextInt(3);
    	
    	switch (computerInt)
    	{
    		case 0:
    			{
    			computerPlay = "R";
    			break;
    			}
    		case 1:
    			{
    			computerPlay = "P";
    			break;
    			}
    		case 2:
    			{
    			computerPlay = "S";
    			break;
    			}
    		default:
    			{
    			computerPlay = "will not happen";
    			}	
    	}
    	
    	System.out.println ("Computer plays: " + computerPlay);
    	
    		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 (computerPlay.equals("P"))
    			System.out.println ("Paper eats rock. You lose!!");
    		}
    		else if (personPlay.equals("P"))
    		{
    	   	if (computerPlay.equals("S"))
    			System.out.println ("Scissor cuts paper. You lose!!");
    	   	else if (computerPlay.equals("R"))
    			System.out.println ("Paper eats rock. You win!!");
    		}
    		else if (personPlay.equals("S"))
    		{
    			if (computerPlay.equals("P"))
    			System.out.println ("Scissor cuts paper. You win!!");
    			else if (computerPlay.equals("R"))
    			System.out.println ("Rock breaks scissors. You lose!!");
    		}
    		else
    		{
    			System.out.println ("Invalid user input.");
    		}
    		
    		
    	}
    }
    
    // Enter R for Rock, P for Paper, S for Scissors: 
    // P
    // Computer plays: P
    // It's a tie!

  5. #5
    AlbertoPL is offline Member
    Join Date
    Sep 2009
    Posts
    22
    Rep Power
    0

    Default

    Why not make your code even simpler by using && ?

    Like this:

    else if (personPlay.equals("R") && computerPlay.equals("S")) {
    System.out.println("Rock crushes scissors. You win!!");
    }
    Instead of having an if inside of an if. It will make your code much more readable and concise. Just something to consider for next time!

  6. #6
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,304
    Blog Entries
    7
    Rep Power
    20

    Default

    This:

    Java Code:
    switch (computerInt)
    	{
    		case 0:
    			{
    			computerPlay = "R";
    			break;
    			}
    		case 1:
    			{
    			computerPlay = "P";
    			break;
    			}
    		case 2:
    			{
    			computerPlay = "S";
    			break;
    			}
    		default:
    			{
    			computerPlay = "will not happen";
    			}	
    	}
    ... can be easier coded as:

    Java Code:
    String computerPlay= ""+"RPS".charAt(computerInt);
    Also see my previous reply.

    kind regards,

    Jos

  7. #7
    CYANiDE is offline Member
    Join Date
    Sep 2009
    Posts
    6
    Rep Power
    0

    Default

    Quote Originally Posted by AlbertoPL View Post
    Why not make your code even simpler by using && ?

    Like this:



    Instead of having an if inside of an if. It will make your code much more readable and concise. Just something to consider for next time!
    Good recommendation but since we are learning nested ifs, one of the requirements was to implement them in the coding of the game.

    Quote Originally Posted by JosAH View Post
    This:

    Java Code:
    switch (computerInt)
    	{
    		case 0:
    			{
    			computerPlay = "R";
    			break;
    			}
    		case 1:
    			{
    			computerPlay = "P";
    			break;
    			}
    		case 2:
    			{
    			computerPlay = "S";
    			break;
    			}
    		default:
    			{
    			computerPlay = "will not happen";
    			}	
    	}
    ... can be easier coded as:

    Java Code:
    String computerPlay= ""+"RPS".charAt(computerInt);
    Also see my previous reply.

    kind regards,

    Jos
    That is indeed much easier, but since we also learned switch during the class the assignment was given, it was a requirement to use the switch. Thanks for all your help!

    -E

Similar Threads

  1. Rock Paper Scissors
    By 54byler in forum Advanced Java
    Replies: 2
    Last Post: 04-23-2009, 06:23 AM
  2. trying to get code in paper form....
    By machine949 in forum New To Java
    Replies: 1
    Last Post: 04-22-2009, 05:09 PM
  3. 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
  4. Need help with Rock Paper Scissors Game
    By GettinGwap in forum New To Java
    Replies: 12
    Last Post: 10-19-2008, 06:15 PM
  5. Paper,Scissor,Rock If then Statements
    By Alberto in forum New To Java
    Replies: 2
    Last Post: 02-11-2008, 11:18 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
  •