Sign in to follow this  
compscience_student

Please help with game of pig!! this is urgent!!

Recommended Posts

The game of Pig is a simple two player dice game in which the first player to reach 100 or more points wins. Players take turns. On each turn a player rolls a six-sided die. After each roll, If the player rolls a 2-6 then he can either: ROLL AGAIN or HOLD. At this point the sum of all rolls made this turn is added to the player's total score and it becomes the other player's turn. If the player rolls a 1 then the player loses his turn. He gets no new points and it becomes the opponent's turn.If a player reaches 100 or more points after holding then the player wins. Write a program that plays the game of Pig, where one player is a human and the other is the computer. Allow the human to input `r' (or `R') to roll again or `h' (or `H') to hold. The computer program should play according to the following rule: Keep rolling on the computer's turn until it has accumulated 20 or more points, then hold. Of course, if the computer wins or rolls a 1 then the turn ends immediately. Allow the human to roll first. So here is my code:
// this game is played with a six sided die, on each player's turn he may roll
// until he gets a 1 from the die, at which point, all points accumulated to 
// that point are wiped, he may hold and add the accumulation to his score at 
// any time.
#include<fstream>
#include<iostream>
#include<cstdlib>
#include<stdlib.h>

using namespace std;

const int score_limit = 100;
 
int humanTurn(int& humanTotalScore);
int computerTurn(int& computerTotalScore);

int main ()
{
        bool continue_game = 1;
        int humanTotalScore = 0, computerTotalScore = 0;
        cout << "This is the Game of Pig." << endl;
        cout << "It is your turn. " << endl << endl;
         
        // Determine whether or not the game is over (whether or not a score of 100 has been reached)
        if ((humanTotalScore < score_limit) && (computerTotalScore < score_limit))
        {
              continue_game = 1;
        }
        else
        {
              continue_game = 0;
        }
         
        // Play Game    
        do
        {                
                humanTurn(humanTotalScore);
                computerTurn(computerTotalScore);  
        }
        while (continue_game = 1);
         
         // Determine who won the game 
         if (continue_game = 0 && (humanTotalScore > computerTotalScore))
         {
                cout << "Congratulations! You Win!";
         }
         else
         {
                cout << "You Lose, Computer Wins .";
         }
         
         getchar();
         getchar();
     
         return 0;
}
        
int humanTurn(int& humanTotalScore)
{
       int accumulatedScore = 0;            //score from this turn 
       char ans;                            //does player want to roll again or hold 
       int lastRoll;                        //value of dice roll
       cout << "Your current score is " << humanTotalScore << "." << endl;
       cout << "Would you like to roll the dice?" << endl;
       cout << "R(r) to Roll, H(h) to hold your score" << endl;
       cout << "Accumulation from this set of rolls is " << accumulatedScore << endl << endl;
       cin >> ans;
       
       //If player decides to roll again 
       while (ans == 'R' || ans == 'r')
       {
           lastRoll = ((rand()%6) + 1);
           if (lastRoll == 1)
           {
              cout << "You rolled a 1. Your score is 0 and your turn is over." << endl; 
              break; 
           }
           else
           {
               accumulatedScore += lastRoll;
               cout << "You rolled a " << lastRoll << endl;
               cout << "R(r) to roll Again. H(h) to hold. Current Accumulation:" << accumulatedScore << endl;
               cin >> ans;
           }
       }
       
       //If player decides to hold 
       while (ans == 'H' || ans == 'h')
       {
           humanTotalScore += accumulatedScore;
           cout << "Your score is " << humanTotalScore << endl; 
       }
       
       //If player enters a key that is not R, r, H, or h
       while ( (ans != 'H') && (ans != 'h') && (ans != 'R') && (ans != 'r') ) //not sure why this is not working
       {
           cout << "Invalid input, please enter either R(r) or H(h)" << endl;
           cin >> ans;
       }
       
       return humanTotalScore;
}
  
int computerTurn (int& computerTotalScore)
{
       int accumulatedScore = 0;                                 //score from this turn
       int lastRoll;                                             //value of dice roll
       
       
       lastRoll = ((rand()%6) + 1);
       if (lastRoll == 1)
       {
              cout << "The computer rolled a 1. It's score is 0 and your turn is over." << endl;  
       }
       else
       {
               accumulatedScore += lastRoll;
               cout << "The computer rolled a " << lastRoll << endl;
               while (accumulatedScore < 20)
               {
                     lastRoll = ((rand()%6) + 1);
                     accumulatedScore += lastRoll;
                     computerTotalScore += accumulatedScore;
               }
               
               while (accumulatedScore >= 20)
               { 
                     accumulatedScore += lastRoll;
                     cout << "Computer's score is " << computerTotalScore << endl; 
                     cout << "It is your turn again.\n"; 
               }
       }   
       
      return computerTotalScore;
}

[Read the FAQ and learn to format your code properly for posting - Zahlman] I'm having problems with the Hold. Whenever I input h or H as the answer, it outputs a very high number repeatedly and wont stop. Can anyone tell me what i'm doing wrong?!?! [Edited by - Zahlman on April 3, 2010 10:53:26 AM]

Share this post


Link to post
Share on other sites
Neither will this one, if it's ever entered:
//If player decides to hold
while (ans == 'H' || ans == 'h')
{
humanTotalScore += accumulatedScore;
cout << "Your score is " << humanTotalScore << endl;
}


Notice that `ans' doesn't change value inside the loop, so if the condition was true the first time around, it will always be true.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this