Thread: Simple Quest.

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

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. Senior Member
Join Date
Oct 2010
Location
Newark,nj
Posts
111
Rep Power
0
I was just thinking that , i'll write it up Right now and paste what i can come up with ):p:p:p:p

3. 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

4. Originally Posted by Bgreen7887
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.

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

kind regards,

Jos

6. Originally Posted by Eranga
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

7. Senior Member
Join Date
Oct 2010
Location
Newark,nj
Posts
111
Rep Power
0
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");

}
}```

8. Senior Member
Join Date
Oct 2010
Location
Newark,nj
Posts
111
Rep Power
0
i know you guys write super long code and it matters most there, but is my code structurally proper?

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

10. Originally Posted by JosAH
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.

11. Senior Member
Join Date
Oct 2010
Location
Newark,nj
Posts
111
Rep Power
0
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]

12. Senior Member
Join Date
Oct 2010
Location
Newark,nj
Posts
111
Rep Power
0
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;

}
}
}
}```

13. 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

14. Senior Member
Join Date
Oct 2010
Location
Newark,nj
Posts
111
Rep Power
0
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.

15. 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

16. Senior Member
Join Date
Oct 2010
Location
Newark,nj
Posts
111
Rep Power
0
HAHAHAHAHA. I 'LL MAKE THE LOOP RUN 1000.. (assumes your a gambler:D j/k)

Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•