This is the code currently. I\'m still not sure how to get the probability, as i
ID: 3535739 • Letter: T
Question
This is the code currently.
I'm still not sure how to get the probability, as i am not sure what math I am suppose to be using.
The main problem lies within the while loop, as i am not sure what I need to find in order to divide it by the n number of simulation. The x0,x1,x2 <- those are the value that need to be found somehow.
Any help/solutions would be appreciated.
#include <iostream>
#include <cstdlib>
#include <cmath>
#include <iomanip>
#include <ctime>
using namespace std;
int main()
{
int n;
cout << "What value should we hold at? ";
cin >> n;
cout << endl;
int sim;
cout << "Hold-at-N turn simulations? ";
cin >> sim;
cout << endl;
srand(time(0));
int score;
double x1 = 0,x2=0,x0 = 0,x3=0,x4=0,x5=0,x6=0;
for(int i=0;i<sim;i++)
{
int total = 0;
for (; total <score; total++)
{
while (total <n)
{
total = total + ((rand() % 6) + 1);
if (total == sim)
{
x1++;
}
if (total == sim+1)
{
x2++;
}
if (total == sim+2)
{
x3++;
}
if (total == sim+3)
{
x4++;
}
if(total == sim+4)
{
x5++;
}
if(total == sim+5)
{
x6++;
}
if(((rand() % 6) + 1) == 1)
{
x0++;
}
}
}
}
cout << "Score " <<"Probability"<< endl;
cout << 0 << " " << fixed << setprecision(6) << x0/sim << endl;
cout << n << " " << fixed << setprecision(6) << x1/sim <<endl;
cout << n+1<< " " << fixed << setprecision(6) << x2/sim << endl;
cout << n+2<< " " << fixed << setprecision(6) <<x3/sim << endl;
cout << n+3<< " " << fixed << setprecision(6) <<x4/sim << endl;
cout << n+4<< " " << fixed << setprecision(6) <<x5/sim << endl;
cout << n+5<< " " << fixed << setprecision(6) <<x6/sim << endl;
}
Explanation / Answer
#include <iostream>
#include <ctime>
#include <cstdlib>
#define LIMIT 100
using namespace std;
class GameOfPigs1 {
public:
int randomRoll;
int humanTotal;
int computerTotal;
int turn;
int roll;
char select;
string wins;
void initialize(int value, int humanTotalScore, int computerTotalScore);
int humanTurn(int);
int computerTurn(int);
int getRandomNumber();
};
GameOfPigs1 gameObj;
/*constructor initializing turn to 0 means human turn */
void GameOfPigs1::initialize(int value,int humanTotalScore,int computerTotalScore){
gameObj.turn = value;
gameObj.humanTotal = humanTotalScore;
gameObj.computerTotal = computerTotalScore;
}
/* Function that handles single turn for human user*/
int GameOfPigs1::humanTurn(int humanTotalScore){
int tempScore = 0;
cout << endl;
cout << "**************************************************************" << endl;
cout << "Human Player's turn :" << endl;
cout << "Press r|R to Roll the dice or h|H to Hold :";
cin >> select;
while(select != 'h' || select != 'H'){
roll = gameObj.getRandomNumber();
cout << "Number Rolled by Human :" << roll << endl;
if(roll == 1){
tempScore = 0;
gameObj.turn = 1;
return 0;
}
tempScore = tempScore + roll;
cout << "Temporary Score of Human in this Roll is : " << tempScore;
cout << " | ";
cout << "Total Score if Halt :" << gameObj.humanTotal + tempScore << endl;
if(gameObj.humanTotal + tempScore >= LIMIT){
cout << "Human Wins. Hurray !"<< endl;
cout << "Computer Total :" << gameObj.computerTotal << " Human Total :" << gameObj.humanTotal + tempScore << endl;
exit(1);
}
cout << "Press r|R to Roll the dice or h|H to Hold :";
cin >> select;
if(select == 'h' || select == 'H'){
gameObj.turn = 1;
return tempScore;
}
}
}
/*Function that handles single turn for computer*/
int GameOfPigs1::computerTurn(int computerTotalScore){
int tempScore =0;
cout << endl;
cout << "**************************************************************" << endl;
cout<< "Computer's turn - Computer's Temporary Score for the turn : " << tempScore << endl;
cout << endl;
while(gameObj.turn == 1){
roll = gameObj.getRandomNumber();
cout << "computer rolled number :" << roll << endl;
if(roll == 1 && tempScore < 20){
gameObj.turn = 0;
cout << "Rolled 1 computerTotal is :" << gameObj.computerTotal << endl;
return 0;
}else if(tempScore >=20){
if(gameObj.computerTotal >= LIMIT){
cout << "Computer wins :"<< endl;
cout << "computer Total :" << gameObj.computerTotal << " Human Total :" << gameObj.humanTotal << endl;
exit(1);
}
gameObj.turn = 0;
return tempScore;
}
tempScore = tempScore + roll;
cout << "Temporary Score of Computer in this Roll :" << tempScore;
cout << " | ";
cout << "Total temporary Score of Computer :" << gameObj.computerTotal + tempScore << endl;
if(gameObj.computerTotal + tempScore >= LIMIT){
cout << "Computer wins :"<< endl;
cout << "computer Total :" << gameObj.computerTotal + tempScore << " Human Total :" << gameObj.humanTotal << endl;
exit(1);
}
}
}
/*Function that simulates a Random Roll of a dice*/
int GameOfPigs1::getRandomNumber(){
sleep(1);
return (rand()% 6) + 1;
}
int main(){
gameObj.initialize(0,0,0);
srand((unsigned)time(0));
while(true) {
if(gameObj.turn == 0){ // human turn
gameObj.humanTotal = gameObj.humanTotal + gameObj.humanTurn(gameObj.humanTotal);
cout << "HumanTotal after this turn is :" << gameObj.humanTotal << endl;
}
if(gameObj.turn == 1){ // computer's turn
gameObj.computerTotal = gameObj.computerTotal + gameObj.computerTurn(gameObj.computerTotal);
cout << "computerTotal after this turn is :" << gameObj.computerTotal << endl;
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.