Pig is a dice game that uses a single die. Two players take turns rolling the di
ID: 3771422 • Letter: P
Question
Pig is a dice game that uses a single die. Two players take turns rolling the die and the game ends when one player scores 100 or higher. During each turn, the player can roll for as long as she wants or until she rolls a 1. The score for a player's turn is the sum of all rolls in that turn, or zero if a 1 was rolled. At the end of a player's turn, her score for the turn is added to her score for the game. Have some fun and play the game on the above website!
Because the game of Pig is complicated we are going to break it down into smaller steps and work on those steps one at a time. This is a typical way of approaching a programming problem. Today we will be working on the first step: Asking the player to enter his or her name, rolling the die once, and telling the player what was rolled.
Here is some code to get you started: RollDice.cpp. Download this starter program, create a project in Visual C++ using this starter program, and complete the program. Your output should look similar to the following figure. Turn in your completed program.
RollDice.cpp
output
Explanation / Answer
#include
// We need this library so we can use string functions
#include
// We need these libraries for the random number generation function
#include<iostream.h>
#include<stdlib.h>
using namespace std;
// This is the prototype for the getRandomNum function that was
// written by the instructor.
int getRandomNum(int lowRange, int highRange);
int main()
{
int number; // The random number
string name; // The player's name
// Seed the random number generator
// We do this at the start of our main function whenever we
// use random numbers in our program.
// We will discuss how to use this function at a later time.
srand( static_cast (time(NULL)) );
/////////////////////////////////////////////////////////////////////
// Start of your code
// Ask the player to enter his or her name
// Make sure your program can handle multiple words as input
cout<<"Enter your name: ";
cin>>name;
// Tell the player to press enter to roll the dice
// use the ignore input function to continue the program
// after the player presses enter
cout<<"Press Enter to roll the dice.....";
getch();
// Call the getRandomNum function to simulate the single
// die roll between 1 and 6. The parameters to the function
// are the lowest number we want to retrieve (1), and the
// highest number we want to retrieve (6).
number = getRandomNum(1,6);
// Print the player's name and the number they rolled
// See the lab web page for sample output
cout<<name<<" rolled a "<<number;
// End of your code
/////////////////////////////////////////////////////////////////////
system("pause");
return 0;
}
//
// Returns a random number in the range of the lowRange and
// highRange parameters (inclusive)
//
int getRandomNum(int lowRange, int highRange)
{
int randNum;
randNum = ( rand() % (highRange - lowRange + 1) ) + lowRange;
return randNum;
}