In: Computer Science
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.
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:
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.
Working code implemented in C++ and appropriate comments provided for better understanding.
Here I am attaching code for these files:
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: