Question: My code for trifecta and trifectabox doesn't follow the cheat results #include #include #include #include #include #include #include using namespace std; class Race { private:

My code for trifecta and trifectabox doesn't follow the cheat results #include
#include
#include
#include
#include
#include
#include
using namespace std;
class Race {
private:
int horses[4]={1,2,3,4};
public:
Race(){
srand(time(NULL)); // Set the random seed
}
void readySetGo(){
int n = sizeof(horses)/ sizeof(horses[0]);
for (int i =0; i < n; i++){
int randomValue = i +(rand()%(n - i));
int randomElement = horses[randomValue];
horses[randomValue]= horses[i];
horses[i]= randomElement;
}
};
int first(){
return horses[0];
}
int second(){
return horses[1];
}
int third(){
return horses[2];
}
int fourth(){
return horses[3];
}
};
bool exacta(array picks, array winner){
return (picks[0]== winner[0] && picks[1]== winner[1]);
}
bool exactaBox(array picks, array winner){
bool a =(picks[0]== winner[0] && picks[1]== winner[1]);
bool b =(picks[0]== winner[1] && picks[1]== winner[0]);
return a || b;
}
bool trifecta(array picks, array winner){
return (picks[0]== winner[0] && picks[1]== winner[1] && picks[2]== winner[2]);
}
bool trifectaBox(array picks, array winner){
sort(picks.begin(), picks.end());
sort(winner.begin(), winner.end());
return (picks[0]== winner[0] && picks[1]== winner[1] && picks[2]== winner[2]);
}
int main(){
int balance =200;
Race race;
cout << "Welcome to the horse race betting program!
";
while (true){
race.readySetGo();
cout << "The Cheat: "<< race.first()<<""<< race.second()<<""
<< race.third()<<""<< race.fourth()<< endl;
cout << "Enter your bet (e.g., 'exacta 12', 'trifectabox 231', 'balance', 'exit'): ";
string input;
getline(cin, input);
vector words;
stringstream ss(input);
string word;
while (ss >> word){
words.push_back(word);
}
if (words.empty()){
cout << "Invalid input. Please try again.
";
continue;
}
string command = words[0];
if (command == "exit"){
cout << "Exiting program.
";
break;
}
else if (command == "balance"){
cout << "Your current balance: $"<< balance << endl;
continue;
}
else if (command != "exacta" && command != "exactabox" && command != "trifecta" && command != "trifectabox"){
cout << "Invalid command. Please enter a valid bet command.
";
continue;
}
array picks2;
array picks3;
for (size_t i =1; i < words.size(); ++i){
if (i <=2){
picks2[i -1]= stoi(words[i]);
}
else {
picks3[i -2]= stoi(words[i]);
}
}
int betCost;
if (command == "exacta" || command == "exactabox"){
betCost =15;
}
else {
betCost =20;
}
if (balance < betCost){
cout << "Insufficient balance to place the bet.
";
continue;
}
balance -= betCost;
bool win;
if (command == "exacta"){
win = exacta(picks2,{ race.first(), race.second()});
}
else if (command == "exactabox"){
win = exactaBox(picks2,{ race.first(), race.second()});
}
else if (command == "trifecta"){
win = trifecta(picks3,{ race.first(), race.second(), race.third()});
}
else if (command == "trifectabox"){
win = trifectaBox(picks3,{ race.first(), race.second(), race.third()});
}
if (win){
int winnings;
if (command == "exacta" || command == "exactabox"){
winnings =150;
}
else {
winnings =250;
}
balance += winnings;
cout << "Congratulations! You won $"<< winnings <<"!
";
}
else {
cout << "Sorry, you lost the bet.
";
}
cout << "Your current balance: $"<< balance << endl;
}
return 0;
}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!