code of Game of Chance
A Game of Chance
One of the most popular games o in casinos and back alleys triangular
games of chance is a dice game known as “craps,”
It is played in hack alleys throughout the world.
Following are rules of the Game:
roll two dice. Each die has six faces. These faces contain one, two, three, four, five, and six spots.
the dice to rest, the sum of the spots on the two upward faces is calculate. If
sum is 7 or 11 on the first throw, the player wins.
If the sum is two, three, or twelve on the first throw Called "craps"),
the player loses (mean the "house" wins).
If the sum is four, five, six, eighth, nine, or ten on the first throw, then that sum becomes the player's "point.”
To win, you must continue rolling
the dice until you make your points. The player loses by rolling a 7 before making the point.
Code of the game:
#include<stdio.h>
#include<stdlib.h>
#include<time.h> /*contains prototype for function time */
enum Status { CONTINUE, WON, LOST };
int rolldice( void ); /* function prototype */
/* function main begins program execution */
int main() {
Int sum;
* sum of rolled dice / int sum;
/////////////////////////
int myPoint;
/* point earned */
enum Status gameStatus */ can contain CONTINUE, WON, or LOST */
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 );
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 77*/
gameStatus = LOST;
/* game over, player Tost » */
} /* end if */
} /* end else */
} /* end while */
/* display won or lost message */
if (gameStatus == WON )
{
/* did player win? */
printf( "Player wins\n");
}
/* end if */
printf( "plave }
/* end else */
else {
/* player lost */
intf( "Player loses\n");
return 0;
} /* end main */ */ roll dice, calculate sum and display calclate sum and display results */
int rolldice( void ) {int diel;
/* first die */ int die2;
/* second die */ int workSum;
/* sum of dice */ diel = 1 + (rand() % 6 );
/* pick random diel value */
die2 = 1 + (rand() % 6);
/* pick random diez value */
workSum = diel + die2;
/* sum diel and die2 */ /* display results of this roll */
printf( "Player rolled %d %d = %d\n", diel, die2, workSum );
return workSum;
/* return sum of dice */ 93 95 } /* end function roll dice
Comments
Post a Comment