Ask your own question, for FREE!
Computer Science 8 Online
OpenStudy (anonymous):

hiii can someone help me with a c++ assignment? i have to simulate a craps card game...

OpenStudy (anonymous):

and i need to get the statistics of winning or losing. can someone just read over my code and tell me what is wrong ?

OpenStudy (anonymous):

I see one problem...craps is a dice game. And are you trying to cover the pass or don't pass --- craps has about a million bets that can be made wikipedia has a pretty good write up actually http://en.wikipedia.org/wiki/Craps This has stats on dice alone: http://hyperphysics.phy-astr.gsu.edu/hbase/math/dice.html

OpenStudy (anonymous):

well this was the specific assignment:

OpenStudy (anonymous):

Write a program to simulate the casino game "craps". It should execute 10000 times so that you can compute the probability(%) of the "player" winning and the "house" winning. The rules are: Player rolls two dice. When the sum is 7 or 11 on first throw, player wins. When the sum is 2, 3, or 12 on first throw, "house" wins. When the sum is 4,5,6,8,9, or 10 on first throw, that sum becomes the player's "point". Now, to win the player must continue rolling the dice until he makes "point"; however should he roll a 7 then the "house" wins. That concludes the game.

OpenStudy (anonymous):

Ok so what do you have....and most of my programming has been in Visual basic so specific C++ I won't be much good at but I can try

OpenStudy (anonymous):

ima post what i have, judge all you want lol #include <iostream> #include <iomanip> #include <ctime> #include <cmath> #include <cstdlib> using namespace std; void main () { srand(static_cast<unsigned int>(time(NULL))); int firstDie, secondDie, sum; int win, lose, point, loss; int firstWin=0; int secondWin=0; int firstLose=0; int secondLose=0; int totalWin=0; int totalLose=0; int cnt=1; while (cnt<=10000) { firstDie=rand()%6+1; secondDie=rand()%6+1; sum=firstDie+secondDie; if (sum==7 || sum==11) { win=1; firstWin+=win; win++; } else if ((sum==2)||(sum==12)||(sum==3)) { lose=1; firstLose+=lose; lose++; } else { int roll, rollTwo, secondSum; roll=rand()%6+1; rollTwo=rand()%6+1; secondSum=roll+rollTwo; if (secondSum==sum) { point=1; secondWin+=point; point++; } else if (secondSum==7) { loss=1; secondLose+=loss; loss++; } else; } cnt++; totalWin=firstWin+secondWin; totalLose=firstLose+secondLose; } double winStat, loseStat; winStat=(static_cast<double>(totalWin)/10000)*100; loseStat=(static_cast<double>(totalLose)/10000)*100; cout<<"Probability of Player Winning: %"<<winStat<<endl <<"Probability of House Winning: %"<<loseStat<<endl; }

OpenStudy (anonymous):

kk (and weirdly enough I have an old project called craps but have no memory of doing it) but what does yours do or not do?

OpenStudy (anonymous):

so when i debug or whatever, the prob of player is 29% and house is 22.3%. what the heckkkkk.

OpenStudy (anonymous):

do your win lose counters get the right totals?

OpenStudy (anonymous):

probably not, but i have no idea how to fix it. i feel like i am not placing certain things in the right places maybe?

OpenStudy (anonymous):

when I hit these problems it is because im not in the loop i think i am

OpenStudy (anonymous):

or maybe something to do with integers or decimals cause the percentages add up coincidentally to ~50% ?

OpenStudy (anonymous):

do they always add up to 50? Can you put the totalWin and totalLose figures in a watch window?

OpenStudy (anonymous):

yes always. and i dont know what that is?

OpenStudy (anonymous):

hehe its a visual basic thing I guess -- just a way to view the variables during runtime...I figure C would have the same kind of thing

OpenStudy (anonymous):

nahh with this srand thing hes making us do, which i barely understand, we dont even see it. the only thing we see is the cout. someone told me c++ is pretty obsolete now. idk. crap. thanks anyway !

OpenStudy (anonymous):

no trouble maybe try cout for those variables to see if they are incrementing like you want and I didn't know what the static_cast thing was so I looked it up -- maybe try dynamic_cast instead?

OpenStudy (anonymous):

alrighty.

OpenStudy (anonymous):

hehe I think the odds of winning and losing are those numbers is all...

OpenStudy (anonymous):

yeah yeah - it is true about 50% of the time shooter rolls again and those were not counted in the ummm count: I didnt do the math because, well, I didn't really understand the C++ ==. =+, ++ stuff but I did do a count of when shooter had to try to make point and that was just under 5000. (new stats in else of second roll) // dicehelp.cpp : Defines the entry point for the application. // #include "stdafx.h" #include <iostream> #include <iomanip> #include <ctime> #include <cmath> #include <cstdlib> using namespace std; void main () { srand(static_cast<unsigned int>(time(NULL))); int firstDie, secondDie, sum; int win, lose, point, loss; int firstWin=0; int secondWin=0; int firstLose=0; int secondLose=0; int totalWin=0; int totalLose=0; int cnt=1; int nowin=0; int nowintot=0; while (cnt<=10000) { firstDie=rand()%6+1; secondDie=rand()%6+1; sum=firstDie+secondDie; if (sum==7 || sum==11) { win=1; firstWin+=win; win++; } else if ((sum==2)||(sum==12)||(sum==3)) { lose=1; firstLose+=lose; lose++; } else { int roll, rollTwo, secondSum; roll=rand()%6+1; rollTwo=rand()%6+1; secondSum=roll+rollTwo; if (secondSum==sum) { point=1; secondWin+=point; point++; } else if (secondSum==7) { loss=1; secondLose+=loss; loss++; } else { nowin=1; nowintot+=nowin; nowin++; } } cnt++; totalWin=firstWin+secondWin; totalLose=firstLose+secondLose; double winStat, loseStat; winStat=(static_cast<double>(totalWin)/10000)*100; loseStat=(static_cast<double>(totalLose)/10000)*100; cout<<"Probability of Player Winning: %"<<winStat<<endl <<"Probability of House Winning: %"<<loseStat<<endl <<"second wins"<<secondWin<<endl <<"no winner roll again"<<nowintot<<endl; } system("pause"); } //good luck and how about a medal? lol

OpenStudy (anonymous):

thank you sir .

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!