Question

Here are Dice Game which programed in C++ below. For this program, add a "cheater" function,...

Here are Dice Game which programed in C++ below. For this program, add a "cheater" function, which will prevent user from winning three times in the row (two times is OK, too). Also, add comments.

#include

#include

using namespace std;

int main()

{

int user,computer,sum,u_diff,c_diff,u_win=0,c_win=0;

do

{

cout<<"User guess is(1 - 18) :";

cin>>user;

if(user>=3&&user<=18) //the sum of three dice should be between 3 to 18

{

cout<<"Computer guess is :";

computer=rand()%16+3;

cout<

cout<<"Sum of the three rolls is :";

sum=rand()%16+3; //rand()%16 will generate the random number between 0 to 15.

cout<

u_diff=user-sum;

c_diff=computer-sum;

if(u_diff<0){

u_diff=u_diff* -1; //u_diff,c_diff is the diffence and it should always be positive.

}

if(c_diff<0){

c_diff*=c_diff* -1; //if u_diff or c_diff is negetive than we make it positive.

}

if(u_diff

cout<<"user won\n";

u_win++;

} else{

cout<<"computer won\n";

c_win++;

}

cout<<"User won: "<

}else if(user >= 19 || user <= 0){

cout<<"please enter between 1 to 18\n";

}

}while(user!=-1);

return 0;

}

Homework Answers

Answer #1

Thanks for the question.


Below is the code you will be needing Let me know if you have any doubts or if you need anything to change.


Thank You !!

Notes: Please comment this line  cout<<"Using cheat"<<endl; inside the cheater function. I kept it for testing the logic.

Everytime user wins two successive games, the cheater function will be called and it will allow computer to win. See the screeenshot for the output.

Let me know for any questions or doubts.

thank you !!


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

#include <iostream>
#include <cstdlib>

using namespace std;

int cheater(int computer, int user){
   cout<<"Using cheat"<<endl;
   if(computer>user)return computer+rand()%2;
   if(computer==3 || computer==18)return computer;
   if(user>computer)return computer -1;
}

int main()
{
int user, computer,sum,u_diff,c_diff,u_win=0,c_win=0;
int gameCount=0;
bool enableCheat=false;
int lastGameWon=0;
do
{
cout<<"User guess is(1 - 18) :";
cin>>user;
if(user>=3&&user<=18) //the sum of three dice should be between 3 to 18
{
   gameCount+=1;
cout<<"Computer guess is :";
computer=rand()%16+3;
cout<<computer<<endl<<"User guess is : "<<user<<endl;
cout<<"Sum of the three rolls is :";
sum=rand()%16+3; //rand()%16 will generate the random number between 0 to 15.
if(enableCheat)sum=cheater(computer,user);
cout<<sum<<endl;
u_diff=user-sum;
c_diff=computer-sum;
if(u_diff<0){
u_diff=u_diff* -1; //u_diff,c_diff is the diffence and it should always be positive.
}
if(c_diff<0){
c_diff*=c_diff* -1; //if u_diff or c_diff is negetive than we make it positive.
}
if(u_diff<c_diff){

cout<<"user won\n";
u_win++;
if(lastGameWon+1==gameCount)enableCheat=true;
lastGameWon=gameCount;
} else{
cout<<"computer won\n";
c_win++;
enableCheat=false;
}
cout<<"User won: "<<u_win<<" times.";
}else if(user >= 19 || user <= 0){
cout<<"please enter between 1 to 18\n";
}
}while(user!=-1);
return 0;
}

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Please write variables and program plan (pseudocode) of the C++ programming below: #include <iostream> #include <cmath>...
Please write variables and program plan (pseudocode) of the C++ programming below: #include <iostream> #include <cmath> using namespace std; void divisors(int num); int main () {    char repeat;    int num;       while (repeat !='n')    {        cout << "Enter a number: ";        cin >> num;        divisors(num);        cout << "Continue? (y or n): ";        cin >> repeat;    }    return 0; } void divisors(int num) {   ...
Below is my C++ program. There is a function to add data, display it and search...
Below is my C++ program. There is a function to add data, display it and search the data. Just add two more functions to this porgram. One function to update the student data and second function to delete the data. Also, change the display function little bit. Right now, it shows all data entered. Make it so you have to put the uin number and it only displays the data of that specific student. #include <iostream> #include <string> using namespace...
C++ PROGRAM SPECIFICATION For the assignment, we will use the previous assignment’s program that determines whether...
C++ PROGRAM SPECIFICATION For the assignment, we will use the previous assignment’s program that determines whether a college class room is in violation of fire law regulations regarding the maximum room capacity and add more logic to that program. We will need to make the following enhancements… The program should now determine the number of classes, and should do so by generating a unique, random number. Replace taking user input for the number of rooms with a computer generated number...
Please write variables and program plan (pseudocode) of the C++ programming code below: #include <iostream> #include...
Please write variables and program plan (pseudocode) of the C++ programming code below: #include <iostream> #include <cmath> using namespace std; int getWeight(); float deliveryCharge(int weight); void displayCharge(int weight); int main () {    displayCharge(getWeight());    return 0; }   int getWeight() {    int weight;    cout << "Enter package weight (oz): ";    cin >> weight;    return weight; } float deliveryCharge(int weight) {    if (weight > 16)    {        return (((weight - 16) / 4) *...
1) What do you think the following program will print if you type from the keyboard...
1) What do you think the following program will print if you type from the keyboard the numbers 3 for a and 8 for b ? #include < iostream> using namespace std; int main() { int a, b; cin >> a >> b; if (a > b && b > 7) { if(a < 7) cout << “ a less than 7: “ << a << ‘\n’; else cout << “Else branch executed\n”; return 0; }
Use CPP This question is about providing game logic for the game of craps we developed...
Use CPP This question is about providing game logic for the game of craps we developed its shell in class. THe cpp of the class is attached to this project. At the end of the game, you should ask user if wants to play another game, if so, make it happen. Otherwise quit. craps.cpp /*** This is an implementation of the famous 'Game of Chance' called 'craps'. It is a dice game. A player rolls 2 dice. Each die has...
Im using visual studio to write a c++ program has a function that will output the...
Im using visual studio to write a c++ program has a function that will output the number if ways a person can climb stairs using only 1 or two steps and and function that when given a number outputs the corresponding fibonacci number. I want to call the functions in main. #include <iostream> using namespace std; int FiboNum(int fiboNum); int Stairs(int nStairs); int main() {    int fiboNum;    int nStairs;    cout << "Enter a number : " <<...
Utilize the code from last week Add a default, full, and copy constructor. Also add a...
Utilize the code from last week Add a default, full, and copy constructor. Also add a constructor that allows you to specify only the name of the video game with no high score or times played specified. Adjust your code to demonstrate use of all 4 constructors and output of the resulting objects. #include <iostream> #include <string> #include <iomanip> using namespace std; class VideoGame { private:     string name;     int highScore;     int numOfPlays; public:     VideoGame() {        ...
1. Create a new project. Type in the following program. Add a comment at the top...
1. Create a new project. Type in the following program. Add a comment at the top to include your name and the date. 2. Compile and Run with different values. What data values should you choose? Think about what we discussed in class. 3. Add code to compute the modulus. Hint: you will also have to declare a new variable. //this program demonstrates the use of various operators #include <iostream > using namespace std; int main() { int num1; int...
c++ program can you please explain how it works and the process? Question: You will design...
c++ program can you please explain how it works and the process? Question: You will design a program in C++ that plays hangman using classes (polymorphism and inheritance).... Hangman Game CODE: #include <iostream> #include <cstdlib> #include<ctime> #include <string> using namespace std; int NUM_TRY=8; //A classic Hangman game has 8 tries. You can change if you want. int checkGuess (char, string, string&); //function to check the guessed letter void main_menu(); string message = "Play!"; //it will always display int main(int argc,...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT