Question

In: Computer Science

I want to make the Main function of this program to only drive. So, make more...

I want to make the Main function of this program to only drive. So, make more functions, and change main function to just drive.

This program Dice Game.

Based on this C++ program below:

#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;
}

Solutions

Expert Solution

//Modified C++ program

#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;
}

void getUserInput(int &user){
   cin>>user;
}

int decideWinner(int user,bool enableCheat){
int computer,sum,u_diff,c_diff;
   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)return 1;
   else return 0;
}

bool isValidInput(int user){
   if(user>=3 && user<=18)return true;
   return false;
}

int main()
{
int user,u_win=0,c_win=0;
int gameCount=0;
bool enableCheat=false;
int lastGameWon=0;
do
{
   cout<<"User guess is(1 - 18) :";
   getUserInput(user);
   if(isValidInput(user)){
       int val = decideWinner(user,enableCheat);
       gameCount++;
       if(val==1)
       {
           cout<<"user won\n";
           u_win++;
           if(lastGameWon+1==gameCount)enableCheat=true;
           lastGameWon=gameCount;
       }
       else if(val==0){
           cout<<"computer won\n";
           c_win++;
           enableCheat=false;
       }


   }
      
  
   else{
       cout<<"please enter between 1 to 18\n";
   }
}while(user!=-1);

cout<<"User won: "<<u_win<<" times.";
return 0;
}


Related Solutions

I want to make a python program that encodes/decodes inputted messages. There are only three options...
I want to make a python program that encodes/decodes inputted messages. There are only three options in the initial menu: 1. encode, 2. decode, 3. quit, and if 1, 2, or 3 are not the input the program says that it is not valid, try again. If encode is chosen, the user inputs their message, the message should only be alphabetical and spaces. Then, the user can pick a number in the range [-27,27] with space being the 27th character...
code in python I want to make a function that adds all the multipliers together. The...
code in python I want to make a function that adds all the multipliers together. The code is posted below and please look at wanted output section to see what I want the code to output. The last line of the code is the only addition I need. Thanks. Code: initial=int(input("Initial value : "))       #taking inputs multiplier=float(input("Multiplier : ")) compound=int(input("No of compounds : ")) print("Your values are:") mult=initial                         #initalizing to find answer for i in range(0,compound):         #multiplying by multiplier    ...
Note: Please rewrite the below Assignment because it is Plagiarized so i want to make it...
Note: Please rewrite the below Assignment because it is Plagiarized so i want to make it rewrite and remove the plagiarism SWOT Analysis SWOT analysis stands for identifying the strengths, weakness, opportunities and threats associated with a particular product, service, project, person or company. It is generally a study undertaken by companies. Strengths and weakness are generally internal in nature while opportunities and threats are external in nature. As a company plans its next move, it should consider all of...
Every C++ program starts with this function:____________________ 2. If A is > 100 I want cnt...
Every C++ program starts with this function:____________________ 2. If A is > 100 I want cnt to be equal to 32 but if A 100 or less I want cnt to be 2. Write a single line using a conditional operator to do this. 3. I want to store into B the remainder of dividing B by the total of A plus D. Write the statement: as a combined operator statement not using a combined operator 4. I want to...
I want to write java program to implement the indexing concept so as an example we...
I want to write java program to implement the indexing concept so as an example we have file named Students this file contains information like this 112233445 ahmed 222442211 saeed 112453345 john this program should search for the student name by its number so as an output example: enter the student number 112233445 name found : ahmed also i want to print the index number of where does the student name exists
So I need to make a java program that reverse or replace first or lastchar or...
So I need to make a java program that reverse or replace first or lastchar or remove char from user's string input. length, concat, charAt, substring, and equals (or equalsIgnoreCase) thses are the string method that only I can use for example if user put asdf asdf and chose reverse input should be fdsa fdsa if user put asdf asdf and chose replace first a with b input should be bsdf asdf if user put asdf asdf and chose remove...
Code in python Hello I want a program to read only the price of each line...
Code in python Hello I want a program to read only the price of each line in a txt file in python. The price is present right after the 2nd comma within the txt file. The output that I want is also printed at the bottom. Inside the txt file: 2019-08-01 00:08:39,BTC/USD,10128.08 2019-08-01 00:08:21,BTC/USD,10117.54 2019-08-01 00:08:20,BTC/USD,10120.51 2019-08-01 00:08:19,BTC/USD,10118.13 2019-08-01 00:08:18,BTC/USD,10120.74 Python file output: Price 1: 10128.08 Price 2: 10117.54 Price 3: 10120.51 Price 4: 10118.13 Price 5: 10120.74
I want to know how can I fix this program so it can work properly....can someone...
I want to know how can I fix this program so it can work properly....can someone run it and give me the fixed version but do not the entire program bear with this one please and thanks in advance!!!!! import java.util.Scanner; public class ifPractice { public static void main(String[] args) { Scanner KB = new Scanner(System.in); { double option; double option02; double stu_gradeTest01; double stu_gradeTest02; double stu_gradeTest03; double final_grade; { System.out.println("Enter #1 to see what test to grade first, #2...
I want to make picture compress program in matlab use eigenmatrix in RGB with PCA. This...
I want to make picture compress program in matlab use eigenmatrix in RGB with PCA. This code listed below. But this code is fail. I want to make the picture have a colour, but in my program the picture is still colorless. Can you fix my code? clc clear all picture = im2double(imread('picture1.jpg')); Red = picture(:,:,1); premean = mean(Red(:)); premax = max(Red(:)); premin = min(Red(:)); x = size(Red,1); y = size(Red,2); Z = ones(x,y)*premean; A = (Red - Z)*(1/premax -...
PLEASE USE THE ECLIPSE. I want to make a program that shows whether the correct number...
PLEASE USE THE ECLIPSE. I want to make a program that shows whether the correct number is correct or incorrect. Example: how much is 1+1? option 1: 2 option 2: 3 option 3: 4 option 4: 5 option 5: 6 The answer is 1. because the answer is 2 and the option number is 1. Write a Java application that simulates a test. The test contains at least five questions about first three lectures of this course. Each question should...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT