# Simple Quest.

• 11-06-2010, 01:10 PM
Bgreen7887
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???
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) ,
• 11-06-2010, 01:16 PM
Eranga
• 11-06-2010, 01:18 PM
Bgreen7887
I was just thinking that , i'll write it up Right now and paste what i can come up with :D:):p:p:p:p
• 11-06-2010, 01:22 PM
JosAH
Have a look at this: let H be head and T be tail; this is a sample run of coin flips:

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
• 11-06-2010, 01:26 PM
Eranga
Quote:

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

That's better, give a try and let us know.
• 11-06-2010, 01:29 PM
JosAH
Quote:

Originally Posted by Eranga

Why? I don't see a need for even a single dimension array ...

kind regards,

Jos
• 11-06-2010, 01:36 PM
Eranga
Because in an instance it may need to find the count.
• 11-06-2010, 01:39 PM
JosAH
Quote:

Originally Posted by Eranga
Because in an instance it may need to find the count.

kind regards,

Jos
• 11-06-2010, 01:40 PM
Bgreen7887
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 ..
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");                                 } }```
• 11-06-2010, 01:42 PM
Bgreen7887
i know you guys write super long code and it matters most there, but is my code structurally proper?
• 11-06-2010, 01:43 PM
JosAH
Quote:

Originally Posted by Bgreen7887
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 ..
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
• 11-06-2010, 01:46 PM
Eranga
Quote:

Originally Posted by JosAH

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.
• 11-06-2010, 01:49 PM
Bgreen7887
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]
• 11-06-2010, 02:02 PM
Bgreen7887
Long lasted joy.. This now wont even print anything to the screen....

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;                         }         }         }         }```
• 11-06-2010, 02:10 PM
JosAH
Quote:

Originally Posted by Bgreen7887
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
• 11-06-2010, 02:15 PM
Bgreen7887
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.
• 11-06-2010, 02:20 PM
JosAH
Quote:

Originally Posted by Bgreen7887
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
• 11-06-2010, 02:24 PM
Bgreen7887
HAHAHAHAHA. I 'LL MAKE THE LOOP RUN 1000.. (assumes your a gambler:D j/k)