In: Computer Science
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:
You will need to create 2 class files for this assignment.
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;
}
===========================================================