// Arup Guha
// 8/27/09
// Craps simulation

#include <stdio.h>
#include <time.h>

#define NUMTRIALS 10000000

int craps();

int main() {
    
    // Seed random number generator.
    srand(time(0));

    int i, numwins = 0;
    
    // Play craps NUMTRIALS times, accumulating the number of wins.
    for (i=0; i<NUMTRIALS; i++)
        numwins = numwins + craps();
        
    // Output your winning percentage.
    printf("Your winning percentage is %lf.\n", 100.0*numwins/NUMTRIALS);
    
    system("PAUSE");
    return 0;
}

int craps() {

    // First roll here.
    int die1 = rand()%6 + 1;
    int die2 = rand()%6 + 1;
    int point = die1+die2;
    
    // We win!
    if (point == 7 || point == 11) return 1;
    
    // We lose =(
    if (point == 2 || point == 3 || point == 12) return 0;
    
    int nextroll = 0;
    
    // Keep on playing until we roll a 7 or the point.
    /* Potentially infinite loop, but that happens with probability 0. */
    while (nextroll != 7 && nextroll != point) {
    
        // Get the next roll.
        die1 = rand()%6 + 1;
        die2 = rand()%6 + 1;
        nextroll = die1 + die2;
        
        if (nextroll == 7) return 0;
        if (nextroll == point) return 1;
    }
}
    
    
