Question

In: Computer Science

The program will loop, asking the user for a bet amount from 0 to 50 (assume...

The program will loop, asking the user for a bet amount from 0 to 50 (assume dollars, you can use ints or longs). If the user types a 0 that means she wants to quit. Otherwise, accept the amount as their bet and simulate a slot machine pull. Your program will print out a line that looks like a slot machine result containing three strings. Some examples are:  BAR 7 BAR, 7 7 cherries, cherries BAR space, space BAR BAR, or cherries cherries BAR.

  • Each of the three positions in the string could be one of the following: "BAR", "7", "cherries" or "space".
  • Each of the three output positions must be generated by your program randomly with probabilities (defined, as always, by const statics)::
    • BAR   (38%)
    • cherries    (40%)
    • space           (7%)
    • 7                     (15%)
    • Therefore, cherries   should be the most frequent symbol seen and space or 7 the least frequent.
  • The following combinations should pay the bet as shown (note ORDER MATTERS):
    • cherries [not cherries] [any] pays 5 × bet (5 times the bet)
    • cherries cherries [not cherries] pays 15 × bet
    • cherries cherries cherries pays 30 × bet
    • BAR BAR BARpays 50 × bet
    • 7 7 7 pays 100 × bet
  • After the pull, display the three strings regardless of the outcome. If the user did not win, tell him/her "Sorry, you lose." If he won, pay him by displaying his winnings (his original bet times the winning factor from the above table). Then, repeat the whole process by requesting another bet amount.

Position counts! If you read the above bullet that contains the warning "ORDER MATTERS", you will see that cherries bar cherries pays 5× while cherries cherries bar pays 15× and bar cherries cherries pays nothing.

A Helper Class: TripleString

For this assignment we use our previous class TripleString. We will instantiate TripleString objects that can be used in our main() method and/or the global-scope methods that main() invokes to simulate this casino project.

The Global-Scope Client Methods

Each global-scope method that you have to write to simulate this casino app plays a special role. For example, there will be one method that gets the bet from the user and returns it to main():

   int getBet()

Another method will simulate a random pull of the slot machine -- it generates three random strings and returns them as a TripleString object to main():

   TripleString pull()

An output method will be used at the end of each loop-pass when the user needs to see the results of her pull, and receive the news about how much she won (or not):

   void display (TripleString thePull, int winnings )

Al description of each method -- and a few others -- is provided in the next section.

The Program Specification

The Global Scope Method Specs

int getBet()

This prompts the user for input and returns the bet amount as a functional return. It should vet the amount before it returns and insist on a legal bet (0 < bet < 50) until it gets one from the user. This method loops. If any other method is used to test for an illegal value or output an error message based on an illegal value, there will be a 4 point penalty.  getBet() must return the legal value to the client and not take any other action besides getting the legal amount.

getBet() has to not only acquire the bet from the user, but also do the output that asks for the bet.

This method must not end the program, but return the valid input result, 0, to the client if/when the user provides it. The client, main() will do what it must when it sees a return value of 0 from this method.

TripleString pull()

This method instantiates and returns a TripleString object to the client.   The data of the TripleString object has to be filled with three randomly chosen strings according to the probabilities described in the "Understand the Application" section above. For example, it might return a TripleString object that contains the three strings ["cherries", "BAR" , "space"].

string randString()

This helper method does a little work -- yet is still quite short. It produces and returns a single random string based on the required probabilities. It does this by calling the C++  rand() function and using the return result of that function as a means of deciding which of the four possible strings to return. Take this in stages.  rand() returns an int between 0 and RAND_MAX. One idea (but not the only one) is to turn that into an int between 1 and 1000 using techniques from five weeks ago. Then, decide which of those numbers should trigger a "7", which should trigger a "cherries", etc. based on the desired probabilities. Since a "Bar" should happen 38% the time, which numbers would you want to trigger a "Bar"? Since a "cherries" should happen 40% of the time, which numbers would trigger a "cherries"? So you see, this is a very simple -- and even short -- function, even though it has to be designed carefully. Common sense will go a long way here.

int getPayMultiplier (TripleString thePull)

After main() gets a TripleString object from pull() (which I will call thePull), it needs to know what the payout will be. That's the job of this function, getPayMultiplier(), which takes the TripleString result from pull() (thePull) as a parameter, and inspects it to determine what its pay multiplier should be: 5? 15? 100? 0?  

void display (TripleString thePull, int winnings )

This method takes the winnings (a dollar amount) and thePull as parameters and displays the three strings inside thePull along with " sorry - you lost " or "congrats, you won $X."

Where it All Goes

There are now a variety of program elements, so let's review the order in which things appear in your solution files:

  1. includes and namespace
  2. class prototype(s)
  3. global-scope method prototype(s)
  4. main() definition
  5. global-scope method definition( )
  6. class method definition(s)

main()'s Workflow

You can debug each of the above methods individually using a test main() that consists of a statement or two. That way you will make sure each component works before trying to write the final main() client.

main() will be a loop controlled by value returned from getBet(). As long as that value is non-zero, we keep playing.

Each time through the loop, we have to call pull() to get the thePull as a return value. Then we need to pass that to getPayMultiplier() to find the multiplier. We then compute the winnings based on the previous information, and finally we display it all using display(). That's all that each loop pass does. So main() is quite neat and clean.

Input Errors

The only place the user can make an input error is in getBet(), so that is the method that deals with such errors. Do not worry about non-numbers. Assume that an integer was entered. But do test for range and only return to main after you have a number in the valid range. getBet() may not decide about ending the program. That is up to main().

Test Run Requirements:

Submit one run that lasts a minimum of 10 pulls, but possibly more .   At least once enter an illegal amount to make sure that your program handles it correctly. Hopefully you are luck and will have one run that contains both a win of cherries cherries cherries  and a win of  BAR BAR BAR  This may take many runs, but may occur if it is your lucky day!

General Requirements

Communicate all values as parameters or return values, not through globals. The meaning of these terms and examples are contained in the module reading.

Again, getBet() must not only acquire the bet from the user, but also do the output that asks for the bet.

Also, I will emphasize that in keeping with the separation of I/O and computation, we would not have any method other than display() output results to the screen, and display() is called from main(), not from any other method. Similarly, getBet() is the only method that does input. The other methods do no input, no output and do not call any methods that do input or output. Let's keep that idea fresh.

What to Turn In

Hand in 3 files: No zip files.

  • casino.h : interface file
  • casino.cpp : implementation file
  • a4.cpp : application file

Solutions

Expert Solution

Working code implemented in C++ and appropriate comments provided for better understanding.

Here I am attaching code for these files:

  • a4.cpp
  • casino.cpp
  • casino.h

a4.cpp:

#include <iostream>
#include <string>
#include "casino.h"
using namespace std;

int main() {
   srand(time(NULL));
   int amountbet, winnings;
   TripleString thePull;

   while (true) {
       amountbet = getBet();
       if (amountbet == 0)
           break;
       thePull = pull();
       winnings = amountbet * getPayMultiplier(thePull);
       display(thePull, winnings);
   }
   return 0;
}

casino.cpp:

#include "casino.h"
#include <iostream>
#include <string>
using namespace std;

const int TripleString::MIN_LEN = 1;
const int TripleString::MAX_LEN = 50;
const string TripleString::DEFAULT_STRING = "(undefined)";

const int MIN_BET = 0;
const int MAX_BET = 50;
const double BAR_LIMIT = RAND_MAX * .38;
const double CHERRIES_LIMIT = RAND_MAX * .78;
const double SPACE_LIMIT = RAND_MAX * .85;
const string BAR = "BAR";
const string CHERRIES = "cherries";
const string SPACE = "(space)";
const string SEVEN = "7";
const int PAYOUT0 = 0;
const int PAYOUT1 = 5;
const int PAYOUT2 = 15;
const int PAYOUT3 = 30;
const int PAYOUT4 = 50;
const int PAYOUT5 = 100;

//Constructors

TripleString::TripleString() {
   setString1(DEFAULT_STRING);
   setString2(DEFAULT_STRING);
   setString3(DEFAULT_STRING);
}

TripleString::TripleString(string str1, string str2, string str3) {
   if (!setString1(str1))
       string1 = DEFAULT_STRING;
   if (!setString2(str2))
       string2 = DEFAULT_STRING;
   if (!setString3(str3))
       string3 = DEFAULT_STRING;
}

//Accessors
string TripleString::getString1() {
   return string1;
}

string TripleString::getString2() {
   return string2;
}

string TripleString::getString3() {
   return string3;
}

string TripleString::toString() {
   return getString1() + ", " + getString2() +
       ", " + getString3();

}

bool TripleString::validString(string str) {
   int num;
   num = str.length();
   if (num >= MIN_LEN && num <= MAX_LEN)
       return true;
   else {
       return false;
   }
}

//Mutators
bool TripleString::setString1(string str1) {
   if (validString(str1)) {
       string1 = str1;
       return true;
   } else {
       return false;
   }
}

bool TripleString::setString2(string str2) {
   if (validString(str2)) {
       string2 = str2;
       return true;
   } else {
       return false;
   }
}

bool TripleString::setString3(string str3) {
   if (validString(str3)) {
       string3 = str3;
       return true;
   } else {
       return false;
   }
}

//Global (Casino Functions)
int getBet() {
   int AmountBet;
   do {
       cout << "How much would you like to bet ($1 - 50) "
               "or 0 to quit? ";
       cin >> AmountBet;
   } while (AmountBet < MIN_BET || AmountBet > MAX_BET);
   return AmountBet;
}

//declare here for pull();
string randString();

TripleString pull() {
   TripleString output(randString(), randString(),
               randString());
   return output;
}

string randString() {
   int random = rand();

   if (random <= BAR_LIMIT) {
       return BAR;
   } else if (random <= CHERRIES_LIMIT) {
       return CHERRIES;
   } else if (random <= SPACE_LIMIT) {
       return SPACE;
   } else {
       return SEVEN;
   }
}

int getPayMultiplier(TripleString thePull) {
   string string1 = thePull.getString1();
   string string2 = thePull.getString2();
   string string3 = thePull.getString3();

   //Set multiplier to 0 so that program only has to pick out
   //when it shouldn't be 0

   int payMultiplier = PAYOUT0;

   if (string1 == CHERRIES) {
       if (string2 == CHERRIES) {
           if (string3 == CHERRIES) {
               payMultiplier = PAYOUT3;
           } else {
               payMultiplier = PAYOUT2;
           }
       } else {
           payMultiplier = PAYOUT1;
       }
   } else if (string1 == BAR && string2 == BAR &&
           string3 == BAR) {
       payMultiplier = PAYOUT4;
   } else if (string1 == SEVEN && string2 == SEVEN &&
           string3 == SEVEN) {
       payMultiplier = PAYOUT5;
   }
   return payMultiplier;
}

void display(TripleString thePull, int winnings) {
   cout << "whirrrrrr .... and your pull is ...\n";
   cout << thePull.toString() << "\n";
   if (winnings > 0) {
       cout << "congratulations, you win: " << winnings << "\n\n";
   } else
       cout << "sorry, you lose.\n\n";
}

casino.h:

#include <iostream>
#include <string>
using namespace std;

#ifndef CASINO_H_
#define CASINO_H_

//triplestring class from previous lab
class TripleString {
private:
   string string1;
   string string2;
   string string3;

   static bool validString(string str);

public:

//Static Constants
   static const int MIN_LEN;
   static const int MAX_LEN;
   static const string DEFAULT_STRING;

//Constructors
   TripleString(string string1, string string2,
           string string3);
   TripleString();

//Accessors
   string getString1();
   string getString2();
   string getString3();
   string toString();

//Mutators
   bool setString1(string str1);
   bool setString2(string str2);
   bool setString3(string str3);
};

//Casino Functions
int getBet();
TripleString pull();
string randString();
int getPayMultiplier(TripleString thePull);
void display(TripleString thePull, int winnings);

#endif /* CASINO_H_ */

Sample Output Screenshots:


Related Solutions

First, write a program to loop asking for a number from the user until the user...
First, write a program to loop asking for a number from the user until the user inputs a zero or until the user has input 50 numbers. Store each value in an array except the values that are divisible by 5 and display all stored values. Determine how many of the values stored in the array were divisible by 10 and display result. Next, write a function getMinMax that accepts an array of floats and the size of the array,...
What it Looks Like to the User The program will loop, asking the user for a...
What it Looks Like to the User The program will loop, asking the user for a bet amount from 0 to 100 (assume dollars, you can use ints or longs). If the user types a 0 that means she wants to quit. Otherwise, accept the amount as their bet and simulate a slot machine pull. Your program will print out a line that looks like a slot machine result containing three strings. Some examples are:  BAR 7 BAR, 7 7 cherries,...
Write a program using c++. Write a program that uses a loop to keep asking the...
Write a program using c++. Write a program that uses a loop to keep asking the user for a sentence, and for each sentence tells the user if it is a palindrome or not. The program should keep looping until the user types in END. After that, the program should display a count of how many sentences were typed in and how many palindromes were found. It should then quit. Your program must have (and use) at least four VALUE...
Write a C# program to ask the user for an undetermined amount ofnumbers (until 0...
Write a C# program to ask the user for an undetermined amount of numbers (until 0 is entered) and display their sum.
def main():     # keeping asking user for a word or name until user enters 0    ...
def main():     # keeping asking user for a word or name until user enters 0     while True:         word = input('Enter a word/name to convert (enter 0 to quit): ').upper()       if word == '0': break         print(convert_to_soundex(word)) Whenever i try to run the program it tells me unintend doesn't match any outer identation level !!!!
Python 10 - (10 pts) - Implement a program that starts by asking the user to...
Python 10 - (10 pts) - Implement a program that starts by asking the user to enter a userID (i.e., a string). The program then checks whether the id entered by the user is in the list of valid users. The current user list is: ['joe', 'sue', jamal, 'sophie'] Depending on the outcome, an appropriate message should be printed. Regardless of the outcome, your function should print 'Done.' before terminating. Here is an example of a successful login: >>> Login:...
Step by step in python Write a program that will keep asking for a user input...
Step by step in python Write a program that will keep asking for a user input (until a blank line is entered) and will inform me whether what I entered was a valid number or not (without crashing). The program should use at least one try/except loop The program should include at least two custom written functions (a main() function can count as one of the two)
create a program using IDLE where you will gather input from the user using a loop....
create a program using IDLE where you will gather input from the user using a loop. The user needs to provide you with a list of test scores for two categories (two different classrooms). Test scores must be integer values between 0 and 10. You need to process each score so that you can output the following: Number of test scores entered for classroom A. Number of test scores entered for classroom B. Average of test scores entered for classroom...
create a program using IDLE where you will gather input from the user using a loop....
create a program using IDLE where you will gather input from the user using a loop. The user needs to provide you with a list of test scores for two categories (two different classrooms). Test scores must be integer values between 0 and 10. You need to process each score so that you can output the following: Number of test scores entered for classroom A. Number of test scores entered for classroom B. Average of test scores entered for classroom...
Create a matlab program that calculates equivalent cpacitance and charge by asking the user whether the...
Create a matlab program that calculates equivalent cpacitance and charge by asking the user whether the circuit is in paraller , series or combination and then asks for the values of capacitances and voltage. After that it calculates the equivalent capacitance.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT