SHARE
SPREAD
HELP

The Tradition of Sharing

Help your friends and juniors by posting answers to the questions that you know. Also post questions that are not available.


To start with, Sr2Jr’s first step is to reduce the expenses related to education. To achieve this goal Sr2Jr organized the textbook’s question and answers. Sr2Jr is community based and need your support to fill the question and answers. The question and answers posted will be available free of cost to all.

 

#
Authors:
Walter Savitch ,kenrick Mock
Chapter:
Function Basics
Exercise:
Programming Projects
Question:11 | ISBN:9780132846813 | Edition: 5

Question

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:

• If the player rolls a 2–6 then he or she 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 or her turn. The player 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” to roll again 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.

Write your program using at least two functions:

int humanTurn(int humanTotalScore);

int computerTurn(int computerTotalScore);

These functions should perform the necessary logic to handle a single turn for either the computer or the human. The input parameter is the total score for the human or computer. The functions should return the turn total to be added to the total score upon completion of the turn. For example, if the human rolls a 3 and 6 and then holds, then humanTurn should return 9. However, if the human rolls a 3 and 6 and then a 1, then the function should return 0.


TextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbook

Answer

C++ program code:

//header section
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
// Generate a random number between 1 and 6
int rollDice() 
{
    return rand() % 6 + 1;  
}
//Function humanTurn to handle a single turn for the human
//and return the turn total to be added to the total score.
int humanTurn(int humanTotalScore) 
{
    int turnTotal = 0;
    char choice;

    do {
        int roll = rollDice();

        if (roll == 1) {
            turnTotal = 0;
            cout << "You rolled a 1. Turn ends." << endl;
            return turnTotal;
        } else {
            turnTotal += roll;
            cout << "You rolled a " << roll << endl;
            cout << "Turn total: " << turnTotal << endl;
            cout << "Roll again (r) or hold (h)? ";
            cin >> choice;
        }
    } while (choice == 'r');

    return turnTotal;
}
//Function computerTurn to handle a single turn for the computer
//and return the turn total to be added to the total score.
int computerTurn(int computerTotalScore) {
    int turnTotal = 0;

    while (turnTotal < 20) {
        int roll = rollDice();

        if (roll == 1) {
            turnTotal = 0;
            cout << "Computer rolled a 1. Turn ends." << endl;
            return turnTotal;
        } else {
            turnTotal += roll;
            cout << "Computer rolled a " << roll << endl;
            cout << "Turn total: " << turnTotal << endl;
        }
    }

    return turnTotal;
}
//main method
int main() 
{
    srand(static_cast<unsigned int>(time(nullptr)));
    //Initialize the variables
    int humanTotalScore = 0;
    int computerTotalScore = 0;
 
    while (humanTotalScore < 100 && computerTotalScore < 100) {
        cout << "Human's turn" << endl;
        int humanTurnScore = humanTurn(humanTotalScore);
        humanTotalScore += humanTurnScore;
        cout << "Human total score: " << humanTotalScore << endl;

        if (humanTotalScore >= 100) {
            cout << "Human wins!" << endl;
            break;
        }

        cout << "Computer's turn" << endl;
        int computerTurnScore = computerTurn(computerTotalScore);
        computerTotalScore += computerTurnScore;
        cout << "Computer total score: " << computerTotalScore << endl;

        if (computerTotalScore >= 100) {
            cout << "Computer wins!" << endl;
            break;
        }
    }

    return 0;
}

Output of the program code:

Human's turn
You rolled a 3
Turn total: 3
Roll again (r) or hold (h)? r
You rolled a 5
Turn total: 8
Roll again (r) or hold (h)? h
Human total score: 8
Computer's turn
Computer rolled a 2
Turn total: 2
Computer rolled a 4
Turn total: 6
Computer rolled a 6
Turn total: 12
Computer rolled a 3
Turn total: 15
Computer rolled a 1. Turn ends.
Computer total score: 15
Human's turn
You rolled a 1. Turn ends.
Human total score: 8
...

 

0 0

Discussions

Post the discussion to improve the above solution.