Results 1 to 18 of 18

Thread: Simple Quest.

  1. #1
    Bgreen7887 is offline Senior Member
    Join Date
    Oct 2010
    Location
    Newark,nj
    Posts
    111
    Rep Power
    0

    Default Simple Quest.

    Hey guys how's everything? Simple game to roll 2 dice 100 times and see who rolls heads 3 times in a row first. Codes not finished, just started .with that being said i have it rolling the dice 100 times and counting how many times it rolls heads, how would i check for 3 in a row???
    Java Code:
    public class FlipRace {
    public static void main(String[] args) {
    
    Coin gucci = new Coin();
    Coin blitz = new Coin();
    int count = 0;
    int holder = 0;
    int i = 0;
    int z = 0;
    
    
    for (i=0;i <=100;i++) {
    			gucci.flip();
    			holder = gucci.flip();
    			if (holder == 0) {
    				count ++;
    	 			z = count;
    	} 
    }
    
    	System.out.println("number of Heads :" + z);
    	}
    }
    (the 2nd die isnt made yet , i figured once i write the first die i'd copy and paste and edit the first) ,

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

  3. #3
    Bgreen7887 is offline Senior Member
    Join Date
    Oct 2010
    Location
    Newark,nj
    Posts
    111
    Rep Power
    0

    Default

    I was just thinking that , i'll write it up Right now and paste what i can come up with ):p:p:p:p

  4. #4
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,786
    Blog Entries
    7
    Rep Power
    21

    Default

    Have a look at this: let H be head and T be tail; this is a sample run of coin flips:

    Java Code:
    HHTTHHTTTHTTTHHTHTHTTTHHH
    1200120001000120101000123
    The second row indicates the 'length' of the current run of heads. At the end you have reached a run length of three. As you can see every time you have thrown a head you increment the counter and when you have thrown a tail you reset the counter again. That should solve your problem.

    kind regards,

    Jos

  5. #5
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    Quote Originally Posted by Bgreen7887 View Post
    I was just thinking that , i'll write it up Right now and paste what i can come up with ):p:p:p:p
    That's better, give a try and let us know.

  6. #6
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,786
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by Eranga View Post
    How about a multi-dimensional array?
    Why? I don't see a need for even a single dimension array ...

    kind regards,

    Jos

  7. #7
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

  8. #8
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,786
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by Eranga View Post
    Because in an instance it may need to find the count.
    Read my reply #4 how to do it without using arrays.

    kind regards,

    Jos

  9. #9
    Bgreen7887 is offline Senior Member
    Join Date
    Oct 2010
    Location
    Newark,nj
    Posts
    111
    Rep Power
    0

    Thumbs up

    I choose to continue w/o arrays so i didn't have to start over.We're just learning arrays in class but the point of this was to get more familiar with loops. This compiles and runs ..
    Java Code:
    public class FlipRace {
    public static void main(String[] args) {
    
    Coin gucci = new Coin();
    Coin blitz = new Coin();
    int count = 0;
    int holder = 0;
    int i = 0;
    int z = 0;
    
    
    for (i=0;i <=100;i++) {
    			gucci.flip();
    			holder = gucci.flip();
    			if (holder == 0) {
    				count ++;
    	 			z = count;
    		}	else if (holder ==1){ //coin landed on tails (reset count)
    					count = 0;
    			}if ( count == 3 ) {
    					break;
    	}					
    }
    System.out.println("Three in a row");
    			
    	}
    }

  10. #10
    Bgreen7887 is offline Senior Member
    Join Date
    Oct 2010
    Location
    Newark,nj
    Posts
    111
    Rep Power
    0

    Default

    i know you guys write super long code and it matters most there, but is my code structurally proper?

  11. #11
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,786
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by Bgreen7887 View Post
    I choose to continue w/o arrays so i didn't have to start over.We're just learning arrays in class but the point of this was to get more familiar with loops. This compiles and runs ..
    Java Code:
    public class FlipRace {
    public static void main(String[] args) {
    
    Coin gucci = new Coin();
    Coin blitz = new Coin();
    int count = 0;
    int holder = 0;
    int i = 0;
    int z = 0;
    
    
    for (i=0;i <=100;i++) {
    			gucci.flip();
    			holder = gucci.flip();
    			if (holder == 0) {
    				count ++;
    	 			z = count;
    		}	else if (holder ==1){ //coin landed on tails (reset count)
    					count = 0;
    			}if ( count == 3 ) {
    					break;
    	}					
    }
    System.out.println("Three in a row");
    			
    	}
    }
    You're flipping your 'gucci' coin twice each time your for-loop body executes and you don't need variable z; clean up your code.

    kind regards,

    Jos

  12. #12
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    Quote Originally Posted by JosAH View Post
    Read my reply #4 how to do it without using arrays.

    kind regards,

    Jos
    I didn't say that it's not possible. What I suggest was to use a more detailed way, could useful in later time.

  13. #13
    Bgreen7887 is offline Senior Member
    Join Date
    Oct 2010
    Location
    Newark,nj
    Posts
    111
    Rep Power
    0

    Default

    oK, Heres my "Washed up" code. I will continue and add the second coin..You guys set me free!! :p:p:p:p:p[/CODE]public class FlipRace {
    public static void main(String[] args) {

    Coin gucci = new Coin();
    Coin blitz = new Coin();
    int count = 0;
    int holder = 0;
    int i = 0;
    for (i=0;i <=100;i++) {
    holder = gucci.flip();
    if (holder == 0) {
    count ++;
    } else if (holder ==1){ //coin landed on tails (reset count)
    count = 0;
    } if ( count == 3 ) {
    break;
    }
    }
    System.out.println("Three in a row");
    }
    }


    [/CODE]

  14. #14
    Bgreen7887 is offline Senior Member
    Join Date
    Oct 2010
    Location
    Newark,nj
    Posts
    111
    Rep Power
    0

    Default

    Long lasted joy.. This now wont even print anything to the screen....

    Java Code:
    public class FlipRace {
    public static void main(String[] args) {
    
    Coin gucci = new Coin();
    Coin blitz = new Coin();
    int count = 0;
    int holder = 0;
    int i = 0;
    for (i=0;i <=100;i++) {
    			holder = gucci.flip();
    			if (holder == 0) {
    				count ++;
    		}	else if (holder ==1){ //coin Gucci landed on tails (reset count)
    					count = 0;
    		  } if ( count == 3 ) {
    					//break;
    					System.out.println("Gucci Thats Cool 3 in a row");
    					//break;
    		}				
    for (i=0;i <=100;i++) 
    			holder = blitz.flip();
    			if (holder == 0) {
    				count ++;
    		}	else if (holder ==1){ //coin Blitz landed on tails (reset count)
    					count = 0;
    		}   if ( count == 3 ) {
    					//break;
    			System.out.println("Blitz Thats Cool 3 in a row");
    			//break;
    		
    	}
    	}
    	}
    	}

  15. #15
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,786
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by Bgreen7887 View Post
    Long lasted joy.. This now wont even print anything to the screen....
    Maybe never three heads were thrown; b.t.w. you should reset the variable 'count' before your second loop starts and your indentation is a mess.

    kind regards,

    Jos

  16. #16
    Bgreen7887 is offline Senior Member
    Join Date
    Oct 2010
    Location
    Newark,nj
    Posts
    111
    Rep Power
    0

    Default

    Sorry about that didn't clean it up before i inserted code.Don't you believe its impossible for a coin to flip 100 times and not be head 3times in a row atleast one time?? I'll clean up and add a statement to print if both didnt do it though.seems impossible for it not to happen a couple times to me thought.

  17. #17
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,786
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by Bgreen7887 View Post
    Sorry about that didn't clean it up before i inserted code.Don't you believe its impossible for a coin to flip 100 times and not be head 3times in a row atleast one time?? I'll clean up and add a statement to print if both didnt do it though.seems impossible for it not to happen a couple times to me thought.
    You should never go to a casino because it is very well possible not to throw three heads in a row. For three throws the probability of throwing three heads in a row is just 1/8 ;-)

    kind regards,

    Jos

  18. #18
    Bgreen7887 is offline Senior Member
    Join Date
    Oct 2010
    Location
    Newark,nj
    Posts
    111
    Rep Power
    0

    Default

    HAHAHAHAHA. I 'LL MAKE THE LOOP RUN 1000.. (assumes your a gambler:D j/k)

Similar Threads

  1. WHY is it not def? PLZ HELP IT IS VERY SIMPLE!!
    By just_java in forum New To Java
    Replies: 9
    Last Post: 02-07-2010, 10:01 AM
  2. Simple AI?
    By Atriamax in forum New To Java
    Replies: 4
    Last Post: 12-23-2009, 11:35 PM
  3. New simple application using a simple database
    By webbusiness23 in forum New To Java
    Replies: 9
    Last Post: 08-03-2009, 03:55 AM
  4. Replies: 2
    Last Post: 05-04-2008, 06:27 PM
  5. simple GUI
    By dim_ath in forum New To Java
    Replies: 3
    Last Post: 01-07-2008, 04:00 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
  •