1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 |
/* Game of Craps */ #include <stdio.h> #include <stdlib.h> #include <conio.h> #include <time.h> /* contains prototype for function time */ /* enumeration constants represent game status */ enum Status { CONTINUE, WON, LOST}; int rollDice( void ); /* function prototype */ /* function main begins program execution */ int main( void ) { int sum; /* sum of rolled dice */ int myPoint; /* point earned */ enum Status gameStatus; /* can contain CONTINUE, WON, or LOST */ printf("\t\t\t\t\tWelcome to"); printf("\t\t\t\t\tGame of Craps"); delay(2000); clrscr(); printf("\t\t\t\t\tCoded By:"); printf("\t\t\t\t\twww.bestengineeringprojects.com"); delay(2000); clrscr(); /* randomize random number generator using current time */ srand( time( NULL ) ); sum = rollDice(); /* first roll of the dice */ /* determine game status based on sum of dice */ switch( sum ) { /* win on first roll */ case 7: case 11: gameStatus = WON; break; /* lose on first roll */ case 2: case 3: case 12: gameStatus = LOST; break; /* remember point */ default: gameStatus = CONTINUE; myPoint = sum; printf( "Point is %d\n", myPoint ); getch(); break; /* optional */ } /* end switch */ /* while game not complete */ while ( gameStatus == CONTINUE ) { sum = rollDice(); /* roll dice again */ /* determine game status */ if ( sum == myPoint ) { /* win by making point */ gameStatus = WON; /* game over, player won */ } /* end if */ else { if ( sum == 7 ) { /* lose by rolling 7 */ gameStatus = LOST; /* game over, player lost */ } /* end if */ } /* end else */ } /* end while */ /* display won or lost message */ if ( gameStatus == WON ) { /* did player win? */ printf( "Player wins\n" ); getch(); } /* end if */ else { /* player lost */ printf( "Player loses\n" ); getch(); } /* end else */ return 0; /* indicates successful termination */ } /* end main */ /* roll dice, calculate sum and display results */ int rollDice( void ) { int die1; /* first die */ int die2; /* second die */ int workSum; /* sum of dice */ die1 = 1 + ( rand() % 6 ); /* pick random die1 value */ die2 = 1 + ( rand() % 6 ); /* pick random die2 value */ workSum = die1 + die2; /* sum die1 and die2 */ /* display results of this roll */ printf( "Player rolled %d + %d = %d\n", die1, die2, workSum ); getch(); return workSum; /* return sum of dice */ } /* end function rollRice */ |
Click Here to download Compiled Software
The output of the Program are shown in figure below: