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 min, int max);
int dealerdecides(int a, int b, int *wins, int *loss, int *points_to_get, int *points);
int throw(int *a, int *b, int *wins, int *loss, int *points_to_get, int *points);
/*
* program
*/
int main()
{
int i, a, b, wins=0, loss=0, points_to_get, points=0, throws=1;
srand(time(NULL));
for (i=0; i<100; i++)
{
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),points, wins, loss, a+b);
}
printf("In %d games:\nTOTAL THROWS: %d\nTOTAL POINTS: %d\nTOTAL WINS: %d\nTOTAL LOSSES: %d\n\n",i, throws, points, wins, loss);
return 0;
}
/*
*
* functions
*
*/
int getrand(int min, int max)
{
return rand()%(max-min)+min+1;
}
int dealerdecides(int a, int b, int *wins, int *loss, int *points_to_get, int *points)
{
if (*points_to_get==0)
/* WIN OF FIRST THROW if you get 7 or 11 on first try */
if ( ( (a+b) == 7 ) || ( (a+b) == 11 ) )
{
*wins+=1;
*points+=(a+b);
return 0;
}
/* LOSE if you get 2,3,12 on first try */
else if ( ( (a+b) == 2 ) || ( (a+b) == 3 ) || ( (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) == 7 )
{
*loss+=1;
return 0;
} else
{
return 1;
}
}
}
/* Throw dice */
int throw(int *a, int *b, int *wins, int *loss, int *points_to_get, int *points)
{
*a=getrand(0,6);
*b=getrand(0,6);
return dealerdecides(*a, *b, wins, loss, points_to_get, points);
}
Downloads:
Download: craps.c 2 KB
Please login or Click Here to register for downloads
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
