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

Thread: Help Improve My Rock,Paper,Scissors

  1. #1
    Member Emperor_Xyn's Avatar
    Join Date
    Dec 2011
    Posts
    66
    My Mood
    Devilish
    Thanks
    21
    Thanked 2 Times in 2 Posts

    Default Help Improve My Rock,Paper,Scissors

    Well I finally had the logic to make a rock paper scissors, but i'm very sure i'm taking long steps.

    Heres the code:
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    package rps;
    import java.util.*;
     
    public class RPS {
     
    static int x;  
    static boolean ock=false;
    static boolean aper=false;
    static boolean cissors=false;
     
        public static void main(String[] args) {
            rock da = new rock();
           da.say();
     
     
           for (int x=0;x<5;x=x+1){
           double crn = Math.random()*10;
          if(crn>7){
              ock=true;}
           if(crn>3&&crn<7){
               aper=true;}
     
          if(crn>0&&crn<3){
               cissors=true;
           }
     
     
           Scanner keyboard = new Scanner(System.in);
           String choice = keyboard.next();
     
     
     
     
     
           if (choice.equals("rock")){
               if(ock){
                   System.out.println("Computer picked rock, you tie.");
               }
               if(aper){
                   System.out.println("Computer picked paper, you lose!");
               }
                if(cissors){
                   System.out.println("Computer picked scissors, you win!");
               }
           }
        if (choice.equals("paper")){
               if(ock){
                   System.out.println("Computer picked rock, you win.!");
               }
               if(aper){
                   System.out.println("Computer picked paper, you tie!");
               }
                if(cissors){
                   System.out.println("Computer picked scissors, you win!");
               }
           }
     
             if (choice.equals("scissors")){
               if(ock){
                   System.out.println("Computer picked rock, you lose.");
               }
               if(aper){
                   System.out.println("Computer picked paper, you win!");
               }
                if(cissors){
                   System.out.println("Computer picked scissors, you tie!");
               }
           }
           }
     
        }}

    Question 1: How can I improve the randomization of 3 different possibilities.

    Question 2: How can I improve all the if statements, just throw out some google keywords if you like.


  2. #2
    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 Improve My Rock,Paper,Scissors

    randomization of 3 different possibilities
    Do you want the 3 possibilities equally likely? Your code does not generate equally likely outcomes.

    Write a loop for 1000 times and count each of the outcomes with your code and print out the three counts. Then change the logic so the three outcomes are nearly equal.

  3. #3
    Member Emperor_Xyn's Avatar
    Join Date
    Dec 2011
    Posts
    66
    My Mood
    Devilish
    Thanks
    21
    Thanked 2 Times in 2 Posts

    Default Re: Help Improve My Rock,Paper,Scissors

    Quote Originally Posted by Norm View Post
    Do you want the 3 possibilities equally likely? Your code does not generate equally likely outcomes.
    Yes, norm, exactly. I'm guessing somewhere in the vast supply of API, there's a method already made that can do something like this? I just don't know it other than making Math.random();

  4. #4
    Forum VIP
    Join Date
    Jun 2011
    Posts
    317
    My Mood
    Bored
    Thanks
    47
    Thanked 89 Times in 74 Posts
    Blog Entries
    4

    Default Re: Help Improve My Rock,Paper,Scissors

    Question 1: How can I improve the randomization of 3 different possibilities.
    There is a package called java.util.Random that will help you with pseudo-random numbers. In particular, check out nextInt(n).

    You can easily use this to generate a random integer in a given range. Just be sure to check out what 'seeding' a random number generator means (google), otherwise you will get the same set of results each time you run the program.

    Personally, I prefer to generate a full random primitive and use the modulo operator to trim it to size (read up on modulo because it is useful for all kinds of reasons).

    Question 2: How can I improve all the if statements, just throw out some google keywords if you like.
    You could have used string concatenation to solve this without so much typing. Consider, you only need one little bit of logic to say "Computer choose xxx" and another separate bit of logic to say "you win" or "you loose". Semantically this way is identical to the way you have done it but it is cleaner and displays a better understanding of logic.

    A good exercise here would be turn the check for win/loss into a function that returns true/false. Also, I would take a look at the many, many ways in which this code can fail:

    if (choice.equals("rock"))


    What if I type "Rock" or " rock" or "rock " or "rrock". I would go for a menu choice where the user has to press '1' '2' or '3' and asks them to try again if the input is bad.

Similar Threads

  1. Rock paper scissors project
    By katie_gsu in forum Loops & Control Statements
    Replies: 1
    Last Post: November 28th, 2011, 02:34 PM
  2. rock paper scissor. whats wrong here ??
    By bjorn10 in forum Loops & Control Statements
    Replies: 1
    Last Post: November 10th, 2011, 09:13 AM
  3. rock paper si
    By robingeldolf in forum What's Wrong With My Code?
    Replies: 4
    Last Post: November 4th, 2011, 06:41 AM
  4. Rock Paper Scissors Spock Lizard Player Problem
    By flyingcurry in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 3rd, 2011, 10:53 AM
  5. MyThesis: rock,paper,scissor..on mobile
    By Cross`17 in forum Java ME (Mobile Edition)
    Replies: 2
    Last Post: April 22nd, 2010, 08:33 AM