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

Thread: Rock Paper Sissor

  1. #1
    Member
    Join Date
    Feb 2014
    Posts
    38
    Thanks
    17
    Thanked 0 Times in 0 Posts

    Default Rock Paper Sissor

    Well, this code is kind of messy. But I think the only problem in this code is the int bot =(int)(Math.random()*3); . Did I do it right? Btw, if there is a way better way to make a Rock Paper Sissor code, can you give me a hint on how I could better code this?

    import java.util.Scanner;
     
    public class Application2 {
     
     
    public static void main(String[] args) 
    {
     System.out.println("Let's play a game of Rock Paper Sissor!");
     Scanner scanner = new Scanner (System.in);
     System.out.println("Rock Paper Sissor: ");
     String rps=scanner.nextLine();
     int bot = (int) (Math.random()*3);   //Get a number from 1-3. 
     if (rps == "Rock")
     {
    	 if (bot==1) //if number from Math.random is 1.
    	 {
    		 System.out.println ("Bot: Rock. TIE!");
    	 }
    	 if (bot==2)
    	 {
    		 System.out.println ("Bot: Paper. You Lose!");
    	 }
    	 if (bot==3)
    	 {
    		 System.out.println ("Bot: Sissor. You Win!");
    	 }
     }
     if (rps == "Paper")
     {
    	 if (bot==1)
    	 {
    		 System.out.println ("Bot: Rock. You Win!");
    	 }
    	 if (bot == 2)
    	 {
    		 System.out.println ("Bot: Paper. TIE!");
    	 }
    	 if (bot == 3)
    	 {
    		 System.out.println ("Bot: Sissor. You Lose!");
    	 }
     if (rps == "Sissor")
     {
    	 if (bot==1)
    	 {
    		 System.out.println ("Bot: Rock. You Lose!");
    	 }
    	 if (bot==2)
    	 {
    		 System.out.println ("Bot: Paper. You Win!");
    	 }
    	 if (bot==3)
    	 {
    		 System.out.println ("Bot: Sissor. TIE!");
    	 }
     }
     }
     
     
    }
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,064
    Thanks
    52
    Thanked 2,282 Times in 2,253 Posts

    Default Re: Rock Paper Sissor

    One problem I see is the use of == to compare Strings. You should use the equals() method.

    Another problem is the series of if statements where if one of them is true, then none of the others can be true. For that kind of logic, the code should use a chain of if/else if statements. Or a switch statement could be used.
    If you don't understand my answer, don't ignore it, ask a question.

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

    hkfrenchtoast (February 16th, 2014)

  4. #3
    Member
    Join Date
    Feb 2014
    Posts
    38
    Thanks
    17
    Thanked 0 Times in 0 Posts

    Default Re: Rock Paper Sissor

    Quote Originally Posted by Norm View Post
    One problem I see is the use of == to compare Strings. You should use the equals() method.

    Another problem is the series of if statements where if one of them is true, then none of the others can be true. For that kind of logic, the code should use a chain of if/else if statements. Or a switch statement could be used.
    Followed what you said. Here is my new code:
    import java.util.Scanner;
     
     
    public class Application2 {
     
     
    public static void main(String[] args) 
    {
     System.out.println("Let's play a game of Rock Paper Sissor!");
     Scanner scanner = new Scanner (System.in);
     System.out.println("Rock Paper Sissor: ");
     String rps=scanner.nextLine();
     int bot = (int) (Math.random()*3);
     if (rps.equals("Rock"))
     {
    	 if (bot==1)
    	 {
    		 System.out.println ("Bot: Rock. TIE!");
    	 }
    	 else if (bot==2)
    	 {
    		 System.out.println ("Bot: Paper. You Lose!");
    	 }
    	 else if (bot==3)
    	 {
    		 System.out.println ("Bot: Sissor. You Win!");
    	 }
     }
     else if (rps.equals("Paper"))
     {
    	 if (bot==1)
    	 {
    		 System.out.println ("Bot: Rock. You Win!");
    	 }
    	 else if (bot == 2)
    	 {
    		 System.out.println ("Bot: Paper. TIE!");
    	 }
    	 else if (bot == 3)
    	 {
    		 System.out.println ("Bot: Sissor. You Lose!");
    	 }
     }
     else if (rps.equals("Sissor"))
     {
    	 if (bot==1)
    	 {
    		 System.out.println ("Bot: Rock. You Lose!");
    	 }
    	 else if (bot==2)
    	 {
    		 System.out.println ("Bot: Paper. You Win!");
    	 }
    	 else if (bot==3)
    	 {
    		 System.out.println ("Bot: Sissor. TIE!");
    	 }
     }
     }
    }

    Sometimes it would work, sometimes it wouldn't. It would sometimes terminate without anything after I inputted something.
    Here is a list of output:
    Let's play a game of Rock Paper Sissor!
    Rock Paper Sissor:
    Rock
    Bot: Paper. You Lose!
    Let's play a game of Rock Paper Sissor!
    Rock Paper Sissor:
    Rock
    Bot: Rock. TIE!
    Let's play a game of Rock Paper Sissor!
    Rock Paper Sissor:
    Paper
    Let's play a game of Rock Paper Sissor!
    Rock Paper Sissor:
    Sissor

  5. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,064
    Thanks
    52
    Thanked 2,282 Times in 2,253 Posts

    Default Re: Rock Paper Sissor

    One more item: always add an else statement at the end of a chain of if/else if to print out a message about unexpected value because the data didn't match. The message should contain the unexpected value.
    If you don't understand my answer, don't ignore it, ask a question.

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

    hkfrenchtoast (February 16th, 2014)

  7. #5
    Member
    Join Date
    Feb 2014
    Posts
    38
    Thanks
    17
    Thanked 0 Times in 0 Posts

    Default Re: Rock Paper Sissor

    public class Application2 {
     
     
    public static void main(String[] args) 
    {
     System.out.println("Let's play a game of Rock Paper Sissor!");
     Scanner scanner = new Scanner (System.in);
     System.out.println("Rock Paper Sissor: ");
     String rps=scanner.nextLine();
     int bot = (int) (Math.random()*3);
     if (rps.equals("Rock"))
     {
    	 if (bot==1)
    	 {
    		 System.out.println ("Bot: Rock. TIE!");
    	 }
    	 else if (bot==2)
    	 {
    		 System.out.println ("Bot: Paper. You Lose!");
    	 }
    	 else if (bot==3)
    	 {
    		 System.out.println ("Bot: Sissor. You Win!");
    	 }
    	 else
    		 System.out.println("error");
     }
     else if (rps.equals("Paper"))
     {
    	 if (bot==1)
    	 {
    		 System.out.println ("Bot: Rock. You Win!");
    	 }
    	 else if (bot == 2)
    	 {
    		 System.out.println ("Bot: Paper. TIE!");
    	 }
    	 else if (bot == 3)
    	 {
    		 System.out.println ("Bot: Sissor. You Lose!");
    	 }
    	 else
    		 System.out.println("error");
     }
     else if (rps.equals("Sissor"))
     {
    	 if (bot==1)
    	 {
    		 System.out.println ("Bot: Rock. You Lose!");
    	 }
    	 else if (bot==2)
    	 {
    		 System.out.println ("Bot: Paper. You Win!");
    	 }
    	 else if (bot==3)
    	 {
    		 System.out.println ("Bot: Sissor. TIE!");
    	 }
    	 else
    		 System.out.println("error");
     }
     else
    	 System.out.println("error");
     }
     
     
    }

    Okay, did what you said again. Sometimes I would get "error", that means the Math.random isn't doing 1-3 (inclusive). I think it would sometimes go 0, giving me the error output. How do I fix Math.random so it wouldn't hit 0?

  8. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,064
    Thanks
    52
    Thanked 2,282 Times in 2,253 Posts

    Default Re: Rock Paper Sissor

    Add 1 to it.
    If you don't understand my answer, don't ignore it, ask a question.

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

    hkfrenchtoast (February 16th, 2014)

  10. #7
    Member
    Join Date
    Feb 2014
    Posts
    38
    Thanks
    17
    Thanked 0 Times in 0 Posts

    Default Re: Rock Paper Sissor

    Okay, I got it to work, but I don't understand why it worked:

    This is my new Math.random.
    int bot = (int) (1+Math.random()*3);
    I understand that it would add 1 when Math.random outputs a 0, so the 0 turns to a 1. But in that logic, wouldn't it add 1 to every Math.random? Like, when Math.random gives out a 3, wouldn't the +1 turn it to a 4, giving an error output?

    Is there something I am not understanding here? :/

  11. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,064
    Thanks
    52
    Thanked 2,282 Times in 2,253 Posts

    Default Re: Rock Paper Sissor

    Try different equations to see what works.
    Write a small test program with a loops many times and creates random numbers and prints them so you can see if the equation is correct.
    If you don't understand my answer, don't ignore it, ask a question.

  12. #9
    Member
    Join Date
    Feb 2014
    Location
    India
    Posts
    47
    My Mood
    Bored
    Thanks
    0
    Thanked 7 Times in 7 Posts

    Default Re: Rock Paper Sissor

    (int)(Math.random() * (max - min) + min).

    This is the formula for having value in the range min-max. The minimum number here is min and the max number is max-1.

    I hope this helps.

  13. The Following User Says Thank You to ankurt For This Useful Post:

    hkfrenchtoast (February 17th, 2014)

Similar Threads

  1. ROCK, PAPER, SCISSORS GAME.
    By kofiachie in forum What's Wrong With My Code?
    Replies: 3
    Last Post: December 21st, 2013, 06:24 AM
  2. I REALLY NEED HELP IN SOLVING THIS ROCK PAPER SCISSOR
    By John93 in forum Java Theory & Questions
    Replies: 12
    Last Post: April 8th, 2012, 08:55 PM
  3. Help Improve My Rock,Paper,Scissors
    By Emperor_Xyn in forum Java Theory & Questions
    Replies: 3
    Last Post: December 16th, 2011, 09:34 PM
  4. Rock paper scissors project
    By katie_gsu in forum Loops & Control Statements
    Replies: 1
    Last Post: November 28th, 2011, 01:34 PM
  5. rock paper si
    By robingeldolf in forum What's Wrong With My Code?
    Replies: 4
    Last Post: November 4th, 2011, 06:41 AM