/* ------------------------------------------------------------------
Pick marbles from a jar that contains a mixture of 100 red marbles, 100 blue marbles, and 100 green marbles.
Pick a marble:
If the marble you picked was red, pick marbles until you pick a blue marble.
If the marble you picked was green, pick marbles until you pick a two marbles of the same color
If the marble you picked was blue, return all red marbles to the jar
Continue until you have picked all the red marbles
-------------------------------------------------------------------------*/
#include
#include
#include
int redMarbles=100;
int greenMarbles=100;
int blueMarbles=100;
char nextMarble() {
int tot = redMarbles + greenMarbles + blueMarbles;
int choice = rand()%tot; // Choice is a random number 0 <= choice < tot
if (choice1) { srand(atoi(argv[1])); }
char picked;
int turn=1;
while(myRed<100) {
picked = nextMarble();
if (picked=='r') {
myRed++;
while(picked!='b') {
picked=nextMarble();
if (picked=='r') myRed++;
else if (picked=='g') myGreen++;
else myBlue++;
}
} else if (picked=='g') {
char prev;
myGreen++;
do {
prev=picked;
picked=nextMarble();
if (picked=='r') myRed++;
else if (picked=='g') myGreen++;
else myBlue++;
} while(picked!=prev);
} else { // picked is blue
myBlue++;
redMarbles += myRed;
myRed=0;
}
printf("After turn %d, I have %d red marbles, %d green and %d blue.\n",turn,myRed,myGreen,myBlue);
turn=turn+1;
}
return 0;
}