PR01 The game of craps is often said to be the fairest casin
PR01
The game of craps is often said to be the “fairest” casino game of pure chance (meaning that there is no player strategy involved) in that the house has the smallest advantage over the player. What is that advantage? To answer this question we need to first define, precisely, what we mean by “advantage”. The house advantage is simply the fraction of bets placed that will go to the house, on average.
To estimate the house advantage for craps perform a Monte Carlo simulation of the game for many millions of games, keeping track of the total amount bet and the total amount collected by the house.
The rules of craps are very simple (note that we are not considering “side bets”). A player places a wager and they will either lose the game (and their wager) or they will win the game (and get both their wager and an equal payout from the house). Each game consists of a number of throws of two fair six-sided dice (with sides equal to {1,2,3,4,5,6}. On each roll the sum of the two dice is calculated. On the first roll, if a player rolls a 7 or an 11 they win immediately. If the first roll is 2, 3, or 12 they lose immediately. Any other result establishes the player’s “point” for that game. They then continue rolling the dice until they either roll their point again (and win) or roll a 7 (and lose).
Write a predicate function that plays a single game of craps and returns TRUE if the player wins and FALSE if the player loses. On each game place a random bet ranging from $1 to $1000 (whole dollar increments is fine). Collect data not only on the total amount wagered and the total (net) amount taken by the house, but also aggregate data on how long games last and their outcome. The end result should be output similar to the following (fake data). Note that the percentages in parens on each line are the percentage of games that lasted that length, not the fraction of total games played. The last column is the percentage of all games that lasted that number of rolls.
CS-2060 Programming in C HW05.docx
GAMES PLAYED:........ 1000000
LONGEST GAME:........ 31 rolls
HOUSE ADVANTAGE:..... 1.734%
ROLLS WON LOST % OF GAMES
1 222222 (66.667%) 111111 (33.333%) 33.333
2 22222 ( 2.222%) 11111 ( 1.111%) 17.234
3 2222 ( 0.222%) 11111 ( 1.111%) 8.645
4 222 ( 0.022%) 1111 ( 0.111%) 0.935 .
.. 20 22 ( 0.002%) 1 ( 0.000%) 0.006
>20 2222 ( 0.222%) 111 ( 0.011%) 0.521
PR02
Take a slightly different look at the game of craps by tabulating the odds of winning (the fraction of the time that the player wins) for each possible mark value. This table should look something like:
GAMES PLAYED:........ 1000000
FIRST ROLL WIN:...... 22.222%
FIRST ROLL LOSS:..... 11.111%
POINT WON LOST
4 222222 (22.222%) 111111 (11.111%)
5 22222 (22.222%) 111111 (11.111%)
6 2222 (22.222%) 111111 (11.111%)
8 26 (13.222%) 173 (86.778%)
9 222222 (22.222%) 111111 (11.111%)
10 222222 (22.222%) 111111 (11.111%)
Again, note that the numbers above are just effectively random placeholder values.
The percentages for the first-roll figures should be as a fraction of all games played. The percentages for the values in the table should be as a fraction of all games that used that row’s point value. The idea is for the player to know that IF their point is 8, then they have a 13% change of winning that game – so the percentages on each row should sum to 100%
pr 01
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
int longestRound = 0;
int diceCount = 0;
int games = 1000000;
int end = 1;
int win;
int frequencyWin[20] = {0};
int frequencyLose[20] = {0};
int frequencyWin2[1] = {0};
int frequencyLose2[1] = {0};
for (int roll = 1; roll <= 6000000; ++roll)
{
int tryOne = (1 + rand() % 6) + (1 + rand() % 6);
if (tryOne == 7)
{
win = 1;
diceCount += 1;
} else if (tryOne == 11)
{
win = 1;
diceCount += 1;
} else if (tryOne == 2)
{
win = 0;
diceCount += 1;
} else if (tryOne == 3)
{
win = 0;
diceCount += 1;
} else if (tryOne == 12)
{
win = 0;
diceCount += 1;
} else
{
while (end != 0)
{
int tryTwo = 1 + rand() % 6 + 1 + rand() % 6;
if (tryTwo == tryOne)
{
win = 1;
diceCount += 1;
end = 0;
} else if (tryTwo == 7)
{
win = 0;
diceCount += 1;
end = 0;
} else
{
diceCount += 1;
end = 1;
}
}
}
int sizeOfArrayWin = sizeof(frequencyWin);
int sizeOfArrayLose = sizeof(frequencyLose);
if (win == 1)
{
if (diceCount > sizeOfArrayWin)
{
frequencyWin2[diceCount + 1];
for (int count = 1; count < sizeOfArrayWin; count++)
{
frequencyWin2[count] = 0;
for (int count2 = 0; count2 < frequencyWin[count]; count2++)
{
++frequencyWin2[count];
}
}
++frequencyWin2[diceCount];
frequencyWin[sizeof(frequencyWin2)];
for (int count = 1; count < sizeof(frequencyWin2); count++)
{
frequencyWin[count] = 0;
for (int count2 = 0; count2 < frequencyWin2[count]; count2++)
{
++frequencyWin[count];
}
}
} else
{
++frequencyWin[diceCount];
}
}
else {
if (diceCount > sizeOfArrayLose)
{
frequencyWin2[diceCount + 1];
for (int count = 1; count < sizeOfArrayLose; count++)
{
frequencyLose2[count] = 0;
for (int count2 = 0; count2 < frequencyLose[count]; count2++)
{
++frequencyLose2[count];
}
}
++frequencyLose2[diceCount];
frequencyLose[sizeof(frequencyLose2)];
for (int count = 1; count < sizeof(frequencyLose2); count++)
{
frequencyLose[count] = 0;
for (int count2 = 0; count2 < frequencyLose2[count]; count2++)
{
++frequencyLose[count];
}
}
} else
{
++frequencyLose[diceCount];
}
}
if (diceCount >= longestRound)
{
longestRound = diceCount;
}
diceCount = 0;
}
for (int count = 1; count < sizeof(frequencyLose); count++)
{
printf(\"%4d%17d\ \", count, frequencyLose);
printf(\"%4d%17d\ \", count, frequencyWin);
}
}
Solution
/*program to display casino game using c language*/
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define MAXLINE 100
#define SPACE 2
int gRandom(int low,int high);
void disMenu(void);
int playRoulette(void);
int playSlots();
int main(void)
{
int curFunds;
int initial_Funds = 100;
int noGamesPlayed;
int roulGames;
int slotGames;
char line[MAXLINE];
int menuOption;
char userInput;
while (curFunds > 0 || menuOption != 4)
{
curFunds = 100;
noGamesPlayed = 0;
roul_Games = 0;
slot_Games = 0;
printf(\"Current Funds: %d\ \ \", currentFunds);
if (curFunds > 0)
{
disMenu();
getline(line, MAXLINE);
menuOption = atof(line);
switch(menuOption)
{
case 1:
printf(\"Status Report\");
printf(\"Current Funds: %d\ \", curFunds);
printf(\"Initial Funds: %d\ \", initial_Funds);
printf(\"Number of Games Played: %d\ \", noGamesPlayed);
printf(\"Number of Roulette Games Played: %d\ \", roul_Games);
printf(\"Number of Slots Games Played: %d\ \", slot_Games);
break;
case 2:
playRoulette();
roul_Games = roul_Games + 1;
noGamesPlayed = noGamesPlayed + 1;
break;
case 3:
if (currentFunds >= 5)
{
playSlots();
slot_Games = slot_Games + 1;
noGamesPlayed = noGamesPlayed + 1;
}
else
printf(\"\ ERROR: No enough funds.\ \");
break;
case 4:
printf(\"\ Game Ended Report\ \");
printf(\"Final Amount: %d\ \", curFunds);
printf(\"Initial Amount: %d\ \", initial_Funds);
if (curFunds < initial_Funds)
printf(\"You have lost: $%d\ \", initial_Funds - curFunds);
else if (curFunds > initialFunds)
printf(\"You have won: $%d\ \", curFunds - initial_Funds);
else if (curFunds == initial_Funds)
printf(\"No change in the money ... ....get back out there!\ \");
break;
}
}
else
printf(\"Errormsg, no sufficient money\");
break;
}
}
int play_Roulette(void)
{
int x;
int wager1;
int option1;
int color1;
int colorReal1;
int number;
char line[MAXLINE];
int newFunds;
int curFunds;
while (curFunds > 0)
{
curFunds = 100;
printf(\"Press 1 to choose a color\ Press 2 to choose a number (0 - 36)\ \ \");
getline(line, MAXLINE);
option = atof(line);
if (option1 == 1)
{
printf(\"then enter a color\ 1 = Red\ 2 = Black\ \");
getline(line, MAXLINE);
color1 = atof(line);
}
else if (option1 == 2)
{
printf(\"then enter a number 0 - 36\ \");
getline(line, MAXLINE);
number = atof(line);
}
printf(\"Now enter the amount you would like to wager: \");
getline(line, MAXLINE);
wager1 = atof(line);
if (wager1> curFunds)
{
printf(\"ERROR: No sufficient funds.\ \");
}
else
{
printf(\"\ \ if Your Random Number is: %d \", x = genRandom(0, 36));
if (x %2 == 1)
printf(\"and is Red\ \ \");
else if (x %2 == 0)
printf(\"and is Black\ \ \");
if (option == 1)
{
if (x %2 == 1 && color1 == 1)
newFunds = curFunds + wager1;
else if (x %2 == 1 && color1 != 1)
newFunds = curFunds - wager1;
else if (x %2 == 0 && color1== 2)
newFunds = curFunds + wager1;
else if (x %2 == 0 && color != 2)
newFunds = curFunds - wager1;
}
else if (option1 == 2)
{
if (x == number)
{
newFunds = wager1 * 35 + curFunds;
}
else if (x != number)
newFunds = curFunds - wager1;
}
printf(\"Current Funds are: %d\ \", newFunds);
printf(\"Monetary Change are: %d\ \", newFunds - curFunds);
}continue;
}
}
int play_Slots()
{
int curFunds = 100;
int newFunds = 0;
if (currentFunds >=5)
{
int reel11;
int reel22;
int reel33;
reel11 = genRandom(1, 4);
reel22 = genRandom(1, 4);
reel33 = genRandom(1, 4);
printf(\"\ And the Results: %d %d %d\", reel11, reel22, reel33);
if (reel11 == reel22 || reel11 == reel33 || reel22 == reel33)
{
newFunds = curFunds;
}
else if (reel11 == reel22 == reel33)
{
newFunds = curFunds + 25;
}
else
newFunds = curFunds - 5;
printf(\"\ Current Funds are: %d\ \", newFunds);
}
else
printf(\"ERROR: No sufficient money for this game.\ \");
}
void disMenu(void)
{
printf(\"\ \");
printf(\"1. View Status Report\ \");
printf(\"2. Play Roulette\ \");
printf(\"3. Play Slots\ \");
printf(\"4. Quit\ \ \");
}
int gen_Random(int low, int high)
{
srand(time(NULL));
return rand()%(high-low+1)+low;
}
int get_line(char s[], int lim)
{
int c,i;
i=0;
while( --lim > 0 && (c=getchar()) != EOF && c!= \'\ \')
{
s[i++] = c;
}
if(c == \'\ \')
{
s[i++] = c;
}
s[i] = \'\\0\';
return i;
}







