We are going to write a program to play a common Casino game called Craps. The rules of craps are fairly simple:
Roll two dice. Each dice has six faces representing values 1, 2, . . ., and 6, respectively. Check the sum of the two dice. If the sum is 2, 3, or 12 (called craps), you lose; if the sum is 7 or 11 (called natural), you win; if the sum is another value (i.e., 4, 5, 6, 8, 9, or 10), a point is established. Continue to roll the dice until either a 7 or the same point value is rolled. If 7 is rolled, you lose. Otherwise, you win. Your program acts as a single player.
Here are some sample rounds.
You rolled 5 + 6 = 11
You win
You rolled 1 + 2 = 3
You lose
You rolled 4 + 4 = 8
point is 8
You rolled 6 + 2 = 8
You win
You rolled 3 + 2 = 5
point is 5
You rolled 2 + 5 = 7
You lose
During each round the Player places a bet. If the Player wins, then they receive their bet from the house, if they lose, they lose their bet.
Write a program to play this game. It should prompt for the bet, roll the dice as many times as needed, displaying the results to the Player. If the player enters a 0 bet, end the game. The player should start with $50.
Hints:
Design an algorithm for this program before you begin coding
You will probably need to use a while loop. If you start the project before we cover while loops in class, write the program so that it plays one game of craps, and then modify it once we've covered loops
You will need to be familiar with most of the topics we've covered in class to make this program work
Instructions for generating random numbers are given in your text. If you have problems, visit the Instructor's office hours or contact in wechat group.
Start the project early, not the night before the deadline! If you have difficulty, do not hesitate to ask for help.
README, containing the following information: your name, where you developed the project, your compiler, instructions on your program's use, and anything else you think we should know in grading your project.
二、代码示例
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int roll();
int main()
{
int bet=50,money=50,flag;
printf("You start with $50.\\n");
begin: while (money>0)
{
printf("Pls place a bet: ");
scanf("%d",&bet);
if (bet<0||bet>money)
printf("Error! Pls place a bet again: ");
else if(bet==0) break;
else
{
flag=roll();
if (flag==1) money+=(2*bet);
else money-=bet;
printf("You now have $%d.\\n\\n",money);
}
goto begin;
}
printf("You end with $%d.\\n",money);
return 0;
}
int roll()
{
int dice1, dice2,sum,point,flag;
srand(time(NULL)+rand());
dice1=1+rand()%6;
dice2=1+rand()%6;
sum=dice1+dice2;
printf("You rolled %d + %d = %d\\n",dice1,dice2,sum);
if ((sum==2)||(sum==3)||(sum==12))
{
printf("You lose\\n");
flag=0;
}
if ((sum==7)||(sum==11))
{
printf("You win\\n");
flag=1;
}
else
{
point=sum;
printf("point is %d\\n",point);
sum=0;
while(1)
{
srand(time(NULL)+rand());
dice1=1+rand()%6;
dice2=1+rand()%6;
sum=dice1+dice2;
printf("You rolled %d + %d = %d\\n",dice1,dice2,sum);
if(point==sum)
{
printf("You win\\n");
flag=1;
break;
}
else if(sum==7)
{
printf("You lose\\n");
flag=0;
break;
}
else
{
point=sum;
printf("point is %d\\n",point);
sum=0;
}
}
}
return flag;
}
总结