For the firrst part of this project, you will be implementing the game of Craps.
ID: 3550312 • Letter: F
Question
For the firrst part of this project, you will be implementing the game of Craps. For those unfamiliar
with the rules, Craps is played with two dice which are rolled together. The sum of these two dice
is computed and if the sum is:
2, 3, or 12 the player immediately loses.
7 or 11 the player immediately wins.
Any other number is referred to as the point. The goal then becomes to keep rolling the dice
until:
{ The player rolls a 7 the player loses
{ The player rolls the point the player wins.
Your job on this assignment is to make a simple craps game. In this game the player will enter his
or her name, the program will display a welcome message, and ask if the player would like to play
or quit. The program will then roll two dice, and display them and their total. If the player has
won, display a message congratulating them. If the player has lost, report it to them. Otherwise,
store this rst roll as the point roll and keep rolling until the player wins or loses. When the game
ends, ask if the player would like to play again. Example:
Welcome to Jons Casino!
Please enter your name: Jonathan
Jonathan, would you like to Play or Quit? play
You have rolled 5 + 2 = 7
You Win!
Would you like to play again? no
Goodbye, Jonathan!
1Hints
Generating random numbers in C is a two-step part. First, we need to seed the random
number generator once per program. The idiom to do this is: srand((unsigned int)time(NULL));
When we need random numbers, we can use the rand() function. It returns an unsigned
integer between 0 and RAND MAX. We can use modulus to reduce it to the range we need:
int value = rand() % (high - low + 1) + low;
Remember that rolling two 6-sided dice is dierent than rolling one 12-sided die.
You must do string input, do not make an integer menu or use single characters.
Explanation / Answer
please rate - thanks
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <time.h>
int playgame();
int main()
{char yesno[8],name[10];
srand(time(0));
printf("Welcome to Jons Casino! Please enter your name: ");
scanf("%s",&name);
printf("%s, would you like to Play or Quit? ",name);
scanf("%s",&yesno);
if(yesno[0]=='p')
yesno[0]='y';
while(yesno[0]=='y')
{if(playgame()==0)
printf("You win ");
else
printf("You lost ");
printf("Would you like to play again? ");
scanf("%s",&yesno);
}
printf("Goodbye, %s ",name);
getch();
return 0;
}
int playgame()
{int gameover=0,yes;
int lost;
int die,roll=0,point;
while(gameover==0)
{die=getdie();
printf(" %d ",die);
roll++;
if(roll==1)
{ if(die==2||die==3||die==12)
{gameover=1;
lost=1;
}
else if(die==7||die==11)
{gameover=1;
lost=0;
}
else
{point=die;
printf("Point=%d ",point);
}
}
else
{if(point==die)
{gameover=1;
lost=0;
}
else
if(die==7)
{gameover=1;
lost=1;
}
}
}
return lost;
}
int getdie(void)
{int die1,die2;
die1=rand()%6+1;
die2=rand()%6+1;
printf("you rolled: %d & %d for a total of ",die1,die2);
return (die1+die2);
}