Game of Craps
eharetea

A simulation of the game craps in C.

Craps game in C.

Date Created:Saturday March 24th, 2007 04:48 PM
Date Modified:Sunday August 03rd, 2008 11:49 AM


/*
* Author: Dan Lynch
*/


#include <stdio.h>

/*
* prototypes
*/

int getrand(int minint max);
int dealerdecides(int aint bint *winsint *lossint *points_to_getint *points);
int throw(int *aint *bint *winsint *lossint *points_to_getint *points);

/*
* program
*/

int main()
{
    int iabwins=0loss=0points_to_getpoints=0throws=1;
    srand(time(NULL));

    for (i=0i<100i++)
    {
        a=0;b=0;
        points_to_get=0;
        while ( throw(&a, &b, &wins, &loss, &points_to_get, &points) ) throws+=1;
        
        printf("In %d throws a %d was rolled, totaling %d points,\n totaling %d wins and %d losses by rolling a %d\n\n\n"throws, (a+b),pointswinslossa+b);

    }
    printf("In %d games:\nTOTAL THROWS: %d\nTOTAL POINTS: %d\nTOTAL WINS: %d\nTOTAL LOSSES: %d\n\n",ithrowspointswinsloss);

    return 0;
}

/*
*
* functions
*
*/

int getrand(int minint max)
{

    return rand()%(max-min)+min+1;
}

int dealerdecides(int aint bint *winsint *lossint *points_to_getint *points)
{
    

    if (*points_to_get==0)
    /* WIN OF FIRST THROW if you get 7 or 11 on first try */
        if ( ( (a+b) == ) || ( (a+b) == 11 ) )
        {
            *wins+=1;
            *points+=(a+b);
            return 0;
        }
        /* LOSE if you get 2,3,12 on first try */
        else if ( ( (a+b) == ) || ( (a+b) == ) || ( (a+b) == 12 ) )
        {    
            *loss+=1;
            return 0;
        } else
        {
            *points_to_get=(a+b);
            return 1;
        }
    /* NOT FIRST THROW*/
    else
    {
        if ( *points_to_get == (a+b) )
        {
            *points+=(a+b);
            *wins+=1;    
            return 0;
        } else if ( (a+b) == )
        {
            *loss+=1;    
            return 0;
        } else
        {    
            return 1;
        }        

    
    }
}

/* Throw dice */
int throw(int *aint *bint *winsint *lossint *points_to_getint *points)
{
    
    *a=getrand(0,6);
    *b=getrand(0,6);
    return dealerdecides(*a, *bwinslosspoints_to_getpoints);
}





Downloads:
Download: craps.c 2 KB

Please login or Click Here to register for downloads
Creative Commons License
Game of Craps by Dan Lynch
is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 United States License
Based on a work at www.3daet.com
Permissions beyond the scope of this license may be available at http://www.3daet.com