Pig dice game is a game that I made that user plays it against computer
it gives the user to either roll or pass
and the computer will try to roll until they’re more than 20
The program works this way
get random number and seed it
then get random start for who will start first and seed
if who ever rolled ’1′ will lose his points and starts again at ’0′
if other than ’0′ it will keep adding points
who ever reach 100 is the winner
let us look at the code and then I will be including screenshots of the game
#include <cstdlib>
#include <ctime>
#include <iostream>
#include <string>
using namespace std;
int player1 (int & playerscore );//to declare the player function
int player2(int & computerscore);//Player 2 function declartion
int again();
bool holded = false; // boolean to use when hold
int rolled(); /// declaration of a random roll (get random number between 1 to 6
int main(){ //// The main function to run the functions
int max = 100;
int randomplayer; // get random player to start with
int playerscore = 0;
int computerscore= 0;
randomplayer = rand() % 2 +1;
while((playerscore != max) && (computerscore != max)){
if (randomplayer ==1){ // do the random player
player1(playerscore);
randomplayer =2;
}
else if (randomplayer==2){
player2(computerscore);
randomplayer = 1;
cout<<”You want to (R)oll or (H)old : “<<endl;
}
if (playerscore >= max){
cout<<”PLAYER WON”<<endl;
break;
}
else if (computerscore >= max){
cout<< “COMPUTER WON”<<endl;
break;
}
}
cout<<”You wana again?????????”<<endl;
char yes = ‘y’;
cin>>yes;
if (yes == ‘y’ or yes == ‘Y’){
cout<<”You want to (R)oll or (H)old : “<<endl;
return main();
}
return 0;
}
int rolled()/// roll function
{
int roll;
roll = rand()% 6 + 1; // get random numbers between 1 to 6
return roll;
}
int player1 (int & playerscore){ // player 1 function
char user; // do char to get the letter ‘r’ or ‘h’
cin>>user; // get the input
int player1roll; // declare the roll and assign it to the roll function
while (user==’r’or user == ‘R’){ // if user input is ‘R’ or ‘r’ do the below
player1roll = rolled();
cout<<”The Player roll is : “<<player1roll<<endl;
if (player1roll == 1){ // if roll is ’1′ it user will lose all points
cout<<”You rolled 1 you lost your points!”<<endl;
playerscore =0;
holded = true; // and is holded
break;
}
else if (player1roll != 1){ // if it is not then add the socres and move on
playerscore += player1roll;
cout<<”You have a score of : “<<playerscore<<endl;
cout<<”You want to (R)oll or (H)old : “;
cin>>user;
}
}
while (user== ‘h’ or user == ‘H’){// if user hold then just hold and break and go to the computer turn
holded = true;
break;
}
return playerscore;
}
int player2 (int & computerscore){
int computeroll;
computeroll = rolled();
if (computeroll != 1){
computerscore += computeroll; /// add the computer roll to the computer score
cout<<”The computer rolled : “<<computeroll<<endl; /// cout it
cout<< “Computer score is : “<<computerscore<<endl;// cout the score
while (computerscore < 20){ // if the computer score is less than 20 it will keep rolling, this should be a bad practice, and I think I need
// to find a better way, because once the computer score is ’20′ it will no longer do anything
computerscore+=computeroll;// add scores
cout<<”The computer rolled again : “<<computeroll<<” the score of the computer is now : “<<computerscore<<endl;
if (computerscore >= 20){// other wise ‘if its 20′ then do nothing give player chance
break;
}
}
while ((holded == true) && (computerscore >=20)){
computerscore+=computeroll;
cout<<”The computer rolled again : “<<computeroll<<” the score of the computer is now : “<<computerscore<<endl;
if( computerscore >= 40){
break;
}
}
}
else if (computeroll == 1){/// if computer roll is 1, then computer loses all points
cout<<”The computer rolled ’1′ it lost all its points, its your turn now : “<<endl;
computerscore = 0;
}
return computerscore;
}
This is how the game looks like