Question

In: Computer Science

C++ Please For this assignment, you will write a program that lets the user play against...

C++ Please

For this assignment, you will write a program that lets the user play against the computer in a variation of the popular blackjack car game. In this variation of the game, two-six sided dice are used instead of cards. The dice are rolled, and the player tries to beat the computer's hidden total without going over 21.

Here are some suggestions for the game's design:

  • Each round of the game is performed as an iteration of a loop that repeats as long as the payer agrees to roll the dice, and the player's total does not exceed 21.
  • At the beginning of each round, the program will ask the users whether they want to roll the dice to accumulate points.
  • During each round, the program simulates the rolling of two six-sided dice. It rolls the dice first for the computer, then it asks the user if he or she wants to roll.
  • The loop keeps a running total of both the computer and the user's points.
  • The computer's total should remain hidden until the loop has finished.
  • After the loop has finished, the computer's total is revealed, and the player with the most points without going over 21 wins.

You will need to create 2 class files for this assignment.

  1. One class will hold the dice information. (Variable to store the value and number of sides, constructor that creates the dice with 6 sides, accessor and mutators, method to roll the dice, and toString).
  2. One class will hold the user information. (Variable to store total score, constructor that creates the user with a default of 0 points to start, accessor and mutators, method to set reset their score, and toString).

Solutions

Expert Solution

Code to copy along with screenshots of code and output are provided.
Please refer to screenshots to understand the indentation of code.
If you have any doubts or issues. Feel free to ask in comments
Please give this answer a like, or upvote. This will be very helpful for me.
================================================================

Screenshots of Code :

Screenshots of Output :

Code to copy:

#include <iostream>
#include <stdlib.h>
#include <time.h>

using namespace std;

// dice class
class dice
{

// declaring variables to hold data
int value;
int numberOfSides;

// public functions
public:
// constructor which set value to 0 and sides to 6
dice()
{
value = 0;
numberOfSides = 6;
}
// getValue function used to return value of dice
int getValue()
{
return value;
}
// roll function used to roll the dice and generate a new value
void roll()
{
value = rand() % numberOfSides + 1;
}
// setter or mutator
void setValue()
{
int n;
cout<<"/nPlease enter the value for dice: ";
cin>>n;

while(value<=1 || value>=6)
{
cout<<"Invalid Value.\n Please try again : ";
cin>>n;
}

value = n;
}

// tostring function to print string indicating value
void toString()
{
cout<<"Die: "<<value<<endl;
}
};

// player class to store user's data
class Player
{
// declaring variable to hold points
int points;

public:
// constructor for class player
Player()
{
points = 0;
}
// function to return points value
int getPoints()
{
return points;

}
// function to add value to the points
void addPoints(int n)
{
points = points + n;
}
// function to reset the score
void resetScore()
{
points = 0;
}
// function to print the points
void toString()
{
cout<<"Points: "<<points<<endl;
}

};
int main()
{
// seeding the random number generator
srand(time(0));

// declaring required variables
int sum;
char answer ;

// greeting message
cout<<"<<<============ (Welcome to Game) ================>>>"<<endl;

// creating instance of two dice class
dice dice1, dice2;

// creating 2 instances of Player class
Player computer, user;

// loop for game
while(true)
{


// rolling the dice for computer
dice1.roll();
dice2.roll();
// calculating the sum
sum = dice1.getValue() + dice2.getValue();
// adding to computer's points
computer.addPoints(sum);

// printing string which says that computer have rolled it
cout<<"\n\nComputer rolled the dice."<<endl;

cout<<endl<<endl;

// checking if points exceeds 21
if(computer.getPoints()>21)
{
// printing final scores
cout<<"\n\n============================"<<endl;
cout<<"Computer's Points: "<<computer.getPoints()<<endl;
cout<<"User's Points: "<<user.getPoints()<<endl;
cout<<"==============================="<<endl;
// declaring winner
cout<<" \n\ncomputer exceeds 21 points. You win!!!"<<endl;
// breaking the loop
break;
}

cout<<"Your points: "<<user.getPoints()<<endl;
// Asking user if he wants to toll the dice
cout<<"Do you want to roll the dice? (Enter 'y' or 'n'): ";
cin>>answer;

// if yes then
if(answer == 'y' || answer == 'Y')
{
// rolling the dice
dice1.roll();
dice2.roll();
// calculating sum
sum = dice1.getValue() + dice2.getValue();
// showing the dice values rolled
cout<<"You rolled "<<dice1.getValue()<<" and "<<dice2.getValue()<<endl;
// adding points to score
user.addPoints(sum);
// showing points after rolling
cout<<"Your points: "<<user.getPoints()<<endl;
// if user exceeds 21 points
if(user.getPoints() > 21)
{
cout<< "\n\nYour points exceeded 21. You LOSE!!!"<<endl;
break;
}

}
else
{
// showing final score
cout<<"\n\n=============================="<<endl;
cout<<"Computer's Points: "<<computer.getPoints()<<endl;
cout<<"User's Points: "<<user.getPoints()<<endl;
cout<<"=============================="<<endl;

// deciding winner using if else and printing it
if(user.getPoints() > computer.getPoints())
{
cout<<"\n\n **** You WON!! ****"<<endl;

}
else if(computer.getPoints() > user.getPoints())
{
cout<<"\n\n**** Computer WON!!!! ****"<<endl;
}
else if (computer.getPoints() == user.getPoints())
{
cout<<"\n\n**** It's a DRAW!! ****"<<endl;
}
break;
}

}

return 0;
}

===========================================================


Related Solutions

Write a Java program that lets the user play a game where they must guess a...
Write a Java program that lets the user play a game where they must guess a number between zero and one hundred (0-100). The program should generate a random number between 0 and 100 and then the user will try to guess the number. The program should help the user guess the number (see details below). The program must allow the user five attempts to guess the number. Further Instruction: (a) Declare a variable diff and assign to it the...
How to write a C++ program that lets the user enter a string and checks if...
How to write a C++ program that lets the user enter a string and checks if it is an accepted polynomial. Accepted polynomials need to have one term per degree, no parentheses, spaces ignored.
C++ Write a program that lets the user enter a two letters which are f and...
C++ Write a program that lets the user enter a two letters which are f and s with a length of 5. And outputs how many times it was occurred and lists the 2 most repeating pattern with 5lengths of f and s. The output display should always start at three-f(s) .Include an option where user can retry the program. Example: Input: sssfsfsfssssfffsfsssssfffsffffsfsfssffffsfsfsfssssfffffsffffffffffffssssssssfffsffffsssfsfsfsfssssfffsssfsfsffffffssssssffffsssfsfsfsss Output: The most repeating 5lengths of pattern is: fffsf and occurred 6times. Output2: The second most repeating...
Write a C++ program that lets the user enter the total rainfall for each of 12...
Write a C++ program that lets the user enter the total rainfall for each of 12 months (starting with January) into an array of doubles. The program should calculate and display (in this order): the total rainfall for the year,     the average monthly rainfall,     and the months with the highest and lowest amounts. Months should be expressed as English names for months in the Gregorian calendar, i.e.: January, February, March, April, May, June, July, August, September, October, November,...
IN C This assignment is to write a program that will prompt the user to enter...
IN C This assignment is to write a program that will prompt the user to enter a character, e.g., a percent sign (%), and then the number of percent signs (%) they want on a line. Your program should first read a character from the keyboard, excluding whitespaces; and then print a message indicating that the number must be in the range 1 to 79 (including both ends) if the user enters a number outside of that range. Your program...
Rock, Paper, Scissors Game Write a Python program rps.py that lets the user play the game...
Rock, Paper, Scissors Game Write a Python program rps.py that lets the user play the game of Rock, Paper, Scissors against the computer. The program should work as follows: You can set these constant global variables at the top outside of your main function definition: COMPUTER_WINS = 1 PLAYER_WINS = 2 TIE = 0 INVALID = 3 ROCK = 1 PAPER = 2 SCISSORS = 3 For this program 1 represents rock, 2 represents paper, and 3 represents scissors. In...
Please Write C++ PROGRAM : That will write a program that initially prompts the user for...
Please Write C++ PROGRAM : That will write a program that initially prompts the user for a file name. If the file is not found, an error message is output, and the program terminates. Otherwise, the program prints each token in the file, and the number of times it appeared, in a well formatted manner. To accomplish all this, do the following: - Open the file - the user must be prompted and a file name input. DO NOT hardcode...
Write a program in C that lets the user enter a message using SMS Language(e.g. lol,...
Write a program in C that lets the user enter a message using SMS Language(e.g. lol, omg, omw etc.), then translates it into English (e.g. laughing out loud, oh my god, on my way etc.). Also provide a mechanism to translate text written in English into SMS Language. needs to be able to translate at least 10 sms words
PYTHON BEGINNER Problem Create a program that lets the user play a simplified game of Blackjack,...
PYTHON BEGINNER Problem Create a program that lets the user play a simplified game of Blackjack, which is played between the user and an automated dealer as follows. The dealer shuffles a standard deck of 52 cards, draws two cards, and gives them to the user. The user can then choose to request another card from the dealer, adding it to their hand. The user can continue to request cards or choose to stop at any time. After each time...
*Please write code in C++* Write a program to verify the validity of the user entered...
*Please write code in C++* Write a program to verify the validity of the user entered email address.   if email is valid : output the stating that given email is valid. ex: "The email [email protected] is valid" else : output the statement that the email is invalid and list all the violations ex:  "The email sarahwinchester.com is invalid" * @ symbol * Missing Domain name The program should keep validating emails until user enter 'q' Upload your source code. ex: main.cpp
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT