For this assignment you will have to investigate the use of the C++ random generator function, rand(). Rand() produces a random integer in the range of 0 to RAND_MAX (a constant — often 32767 — defined in the stdlib, automatically included in all of your projects). You can find this in the text or using the online resources given in the lectures.
OPTION A (Basic) A Slot Machine Simulation
Understand the Application
What it Looks Like to the User
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
We create a new data type to use for this assignment: class TripleString. TripleString will consist of three private member strings as its basic data: (string1, string2, and string3). There will be few instance methods to support that data. The class will be very modest. Once defined, we will use it to 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 )
We will describe each method — and a few others — in the next section.
The Program Spec
Class TripleString Spec
The first step in writing this program is to create a simple, working class TripleString.
The Data
It will contain three private member strings as its main data: string1, string2, and string3. We will also add a public static member which is to be a const int MAX_LEN set to 20. This represents the maximum length that our class will allow any of its strings to be set to. We can use MAX_LEN in the TripleString method whose job it is to test for valid strings (see below).
In summary, three private instances strings and one public static MAX_LEN. That’s all the data for this class.
Default Constructor
TripleString() — a default constructor that initializes all members to “”. We do not need any parameter-taking constructors.
A Private Helper Method
bool validString( string str ) — a helper function that the mutators can use to determine whether a string is legal. This method returns true if its length <= MAX_LEN and false, otherwise.
Mutators/Accessor
set()s and get()s for these members.
Where it All Goes
There are now a variety of program elements, so let’s review the order in which things appear in your .cpp file:
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)
After writing this class, test it using a simple main() which instantiates an object, mutates the members, displays the object, etc. Don’t turn this test in. It’s part of your development cycle.
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
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”].
The way it determines and loads the three strings is by using another global scope helper method, described, next, randString(). So this method, pull() will call the next method randString() three times to get the three strings that will be stored into the TripleString object. Once that’s done, pull() just returns the TripleString object to the client and its job is done.
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 40% the time, which numbers would you want to trigger a “Bar”? Since a “cherries” should happen 30% 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 pullString() (thePull) as a parameter, and inspects it to determine what its pay multiplier should be: 5? 15? 100? 0? It does this by looking at the three strings inside the passed-in TripleString object and using if statements to determine and return the right value. For example, if all three of the strings are “cherries”, which is easily checked using an if statement, then this method returns a pay multiplier of 30. You can use logic like this to create a sequence of if or else if statements that will give you the desired multiplier. However you do it, the method will return one of the values; 0, 5, 15, 30, 50 or 100.
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.”
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 pullString 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’s the method that deals with such errors. Don’t worry about non-numbers. Assume that a number was entered. But do test for range and only return to main after you have a valid range. getBet() may not decide about ending the program. That’s up to main().
Test Run Requirements:
Submit one run that lasts a minimum of 40 pulls, but possibly more (continue reading). At least once enter an illegal amount to make sure that your program handles it correctly. Also, make sure your one run contains both a win of cherries cherries cherries and a win of BAR BAR BAR This may take many runs, but should be accomplished in less than two minutes if your program is written correctly.
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.
Between your source and your run, report which pull groups that resulted in all cherries and which pulls resulted in all BARs (e.g., “pull groups #3 and #5 resulted in all cherries and pull group #62 resulted in all BARs.” You can type this out in your text file manually after you have pasted the source and run in their correct places.
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.
Try it now!
How it works?
Follow these simple steps to get your paper done
Place your order
Fill in the order form and provide all details of your assignment.
Proceed with the payment
Choose the payment system that suits you most.
Receive the final file
Once your paper is ready, we will email it to you.
Our Services
Custom Writings Help is a Quality-Oriented Company in Online Writing as a result of hiring exceptional professionals to execute clients' tasks.
Essays
At Custom Writings Help,We understand the struggle of research paper writing, and that is why at Custom WritingS Help, we are all out to help you. We pride ourselves on having a team of clinical writers. The stringent and rigorous vetting process ensures that only the 'BEST' Writers are chosen for the job. We have highly qualified Ph.D. and MA writers working with us; we equally offer these experienced writers specific bonuses and incentives to make them deliver highly original, unique, and informative content at reasonably low prices.
Admissions
Thesis Writing Service
Worlwide, Many Masters Students are struggling with Thesis Completion. A thesis is likely to be the longest and most challenging piece of work a student has ever completed. However, unlike essays and other assignments, a student can pick a particular interest topic and work on their initiative. Fortunately, we are there for you. At Custom Writings Help, you are assured of an authentic, imaginative, informative, linguistically great, and advantageous thesis that adheres to all your needs. So, why continue considering different writers when you have discovered the best in the field?
Editing
Custom Papers
Not a single student can avoid writing custom papers. However, a total lack of experience, skills, and time makes it very hard to produce a superb writing piece. Therefore, if you are seeking professional help, turn to us. Our specialized and experienced writers compose a variety of model papers, including custom essays, college term papers, research papers, book reports, MBA essays, executive summaries, dissertations, Ph.D. theses, admission essays, and research proposals for college and university students at any level.
Coursework
Essay Writing
Most of the students disregard the critical principles of essay writing and compose papers below sensible guidelines. Therefore, with Custom Writings Help, one should not worry about his/her essay. Our Writers compose informative and engaging content on all complexities and topics. We write meaningful and smart essays while prioritizing all aspects that bring about a good grade, such as impeccable grammar, proper structure, zero-plagiarism, and conformance to guidelines.
Coursework
Coourse Work Writing
Don't let the seemingly never-ending onslaught of writing assignments get you down. If you are looking where to get course work assistance online, the writers at Custom Writings Help are here to assist you with all of your writing needs. We undertake to unique delivery of papers that meet the professor's requirements. The content is proofread, edited, and checked plagiarism before submission to customers. No matter how big or small your work is, we will deliver on time. Try US Now! !
Coursework
Dissertation Writing Service
High-Quality Dissertation Writing Services are rare. They require Ph.D. academicians – not easily found. However, are an exception. The years, time, and resources we have invested in the dissertation world has given us a competitive advantage over others. Choose to come to Custom Writings Help; You will find perfect Ph.D. consultants who have written hundreds of dissertations theses ready to help you. Let our dissertation-writing services help you craft your dissertation, for you are assured we will give you the results.