our main game dice pig game, assumed that as a computer the best way to victory
ID: 3739668 • Letter: O
Question
our main game dice pig game, assumed that as a computer the best way to victory was to stop after 20 points or more perturn. you are going to analyze if this is the best value by using trial and error. redisgn your program so that it is computer one against computer two. alter the total per turn that is nessary for one of the computers players, while leaving the other 20. (example 15 or 20) for your test value try one number between 15 and 25. now place your game in a loop and play 1000 times and report which computer won more. you must remove all of the ouput statements once you are sure it is working otherwise it will take to long to run.
Explanation / Answer
Answer:
#include <iostream>
#include <ctime>
#include <cstdlib>
#define LIMIT 100
using namespace std;
class dice1 {
public:
int test1;
int counthuman;
int countcomputer;
int turn;
int roll;
char choice;
string wins;
void initialize(int value, int counthumanScore, int countcomputerScore);
int chancehuman(int);
int chancecomputer(int);
int getRandomNumber();
};
dice1 objectvalue;
void dice1::initialize(int value,int counthumanScore,int countcomputerScore){
objectvalue.turn = value;
objectvalue.counthuman = counthumanScore;
objectvalue.countcomputer = countcomputerScore;
}
int dice1::chancehuman(int counthumanScore){
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 >> choice;
while(choice != 'h' || choice != 'H'){
roll = objectvalue.getRandomNumber();
cout << "Number Rolled by Human :" << roll << endl;
if(roll == 1){
tempScore = 0;
objectvalue.turn = 1;
return 0;
}
tempScore = tempScore + roll;
cout << "Temporary Score of Human in this Roll is : " << tempScore;
cout << " | ";
cout << "Total Score if Halt :" << objectvalue.counthuman + tempScore << endl;
if(objectvalue.counthuman + tempScore >= LIMIT){
cout << "Human Wins. Hurray !"<< endl;
cout << "Computer Total :" << objectvalue.countcomputer << " Human Total :" << objectvalue.counthuman + tempScore << endl;
exit(1);
}
cout << "Press r|R to Roll the dice or h|H to Hold :";
cin >> choice;
if(choice == 'h' || choice == 'H'){
objectvalue.turn = 1;
return tempScore;
}
}
}
int dice1::chancecomputer(int countcomputerScore){
int tempScore =0;
cout << endl;
cout << "**************************************************************" << endl;
cout<< "Computer's turn - Computer's Temporary Score for the turn : " << tempScore << endl;
cout << endl;
while(objectvalue.turn == 1){
roll = objectvalue.getRandomNumber();
cout << "computer rolled number :" << roll << endl;
if(roll == 1 && tempScore < 20){
objectvalue.turn = 0;
cout << "Rolled 1 countcomputer is :" << objectvalue.countcomputer << endl;
return 0;
}else if(tempScore >=20){
if(objectvalue.countcomputer >= LIMIT){
cout << "Computer wins :"<< endl;
cout << "computer Total :" << objectvalue.countcomputer << " Human Total :" << objectvalue.counthuman << endl;
exit(1);
}
objectvalue.turn = 0;
return tempScore;
}
tempScore = tempScore + roll;
cout << "Temporary Score of Computer in this Roll :" << tempScore;
cout << " | ";
cout << "Total temporary Score of Computer :" << objectvalue.countcomputer + tempScore << endl;
if(objectvalue.countcomputer + tempScore >= LIMIT){
cout << "Computer wins :"<< endl;
cout << "computer Total :" << objectvalue.countcomputer + tempScore << " Human Total :" << objectvalue.counthuman << endl;
exit(1);
}
}
}
int dice1::getRandomNumber(){
sleep(1);
return (rand()% 6) + 1;
}
int main(){
objectvalue.initialize(0,0,0);
srand((unsigned)time(0));
while(true) {
if(objectvalue.turn == 0){
objectvalue.counthuman = objectvalue.counthuman + objectvalue.chancehuman(objectvalue.counthuman);
cout << "counthuman after this turn is :" << objectvalue.counthuman << endl;
}
if(objectvalue.turn == 1){
objectvalue.countcomputer = objectvalue.countcomputer + objectvalue.chancecomputer(objectvalue.countcomputer);
cout << "countcomputer after this turn is :" << objectvalue.countcomputer << endl;
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.