Friday, June 01, 2012

Game of Pig program in C++

Game of Pig in C++


The following program simulates game of Pig between a human and a computer. Human player starts first and rolls a six faced dice. If the output of a dice is between 2 to 6, he is allowed to roll it again until he chooses to hold. If he holds then the total sum of his rolls is added to his total score and the turn is passed to the computer. If the output of the dice is 1 then his turn ends immediately and he earns a 0 score in this turn. Rolling a 1 after rolling any number wont let any points to add to his total..

When in computer's turn, It keeps on rolling until it's total sum of the rolls becomes greater than or equal to 20. The computer's score is then added to his total score. After this it stops and turn passes to human user. In between the roll if the computer rolls a 1 then his turn ends immediately without adding any points to his total score and the turn subsequently passes to the human user. 

This process continues until one of the user reaches a total score of 100 points at which it wins the game. This program is a simple console based C++ program which exits when any user reaches the total score of 100 or more first.

The program simulates Rolling of a dice by a generating a Random Number from 1 to 6 by using standard rand function of C++

  /**   
  * @author Bishal Acharya   
  * A simple program implementing Game Of Pigs where, first player   
  * to reach a total of 100 points wins the game.Players take a turn   
  * in rolling a dice containing six sides.   
  */  
 #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;     
   }    
  }   
 }  

Output of the given Program :-

gameOfPig



























No comments: