CMPSC 121 Programming Project 2 solved


Category: You will receive a download link of the .ZIP file upon Payment


5/5 - (1 vote)

In this project, you will build a program to play a simplified version of Roulette. The
user will enter their name, and at each turn they can decide to continue playing by placing
a bet, or quit. When they choose to quit, or run out of money, the program will exit.
Initially, the player starts out with 100 virtual dollars. Each time they place a bet, it is
automatically $5. They may either bet on odd, even, or zero. The wheel has 37
positions, numbered 0 through 36. If they bet on even or odd and are correct, they win
the amount of their bet (and lose nothing). If they bet on zero and win, they win $50.
Any incorrect wager loses the $5 amount of the bet.
Note that you will have to check for an even win carefully. Review Slide 21 of our
Chapter 6 slides for how to use modulus to determine an even or odd number. Using
modulus shows that zero is an even number, but for purposes of this game it is NOT. If a
user bets on even, and a zero is spun, they lose their bet.
As with the Math Tutor program, you must seed the random number generator so that
different random numbers are generated each time the program is run. However, in this
program, the random numbers generated must be in the range 0 through 36, inclusive.
Since 0 is included, you will NOT add one to the result of rand(), but you will have to
determine which value to use on the right of the % sign. Be sure to create a named
constant for it!
Most of your code will be inside of a loop. The loop will continue as long as the user has
money remaining, and has chosen something other than quitting. Each time they win or
lose, you should keep a count of that. When they are done playing (either by choice or
losses), display their total number of wins and losses.
When the user enters their choice (even, odd, zero, quit), you must accept their input as
TEXT (either char or string), NOT a number. You must also accept either upper- or
lower-case responses.
Also, when the user is done playing, your program will write out a report to a file on disk.
This file will be named after the user. For instance, if their name is Joe, you would
produce a disk file named Joe-Roulette.txt. In this file, you will put the following
information: total number of bets, and either the total amount won or the total amount
lost. This amount must be labeled, and must always be either zero (they broke even) or
positive (for instance, if the user lost 15 dollars, you would NOT report that they won -15
dollars). You can determine if they won or lost based upon how their ending cash
compares to their starting cash.
Assume you have the user’s name stored in the string variable name. Further assume
you’re trying to create the file name (as described above) in the variable fileName.
You can create the proper fileName by using string concatenation (the + operator).
For instance:
string name, fileName;
// Code to read the name from the user
fileName = name + “-Roulette.txt”;
The problem comes when you try to open the file. Assume that the output stream object
is outputFile. When you want to open the file, if you write the following code: fileName ); // ERROR
You will get a compiler error. The problem is that the open() member function only
accepts a C-string, not a string object. Fortunately, the string class has a member
function that can help, named c_str(), which converts the string into a C-string.
Here is an example that works, using fileName as the above example did: fileName.c_str() );
Following this, you should check to make certain that the output file was opened
properly. If not, do not try to write any information to the file. How could opening the
output file fail? The disk could be full or the filename could be invalid. If the user enters
their name properly, this should not be a problem. But what if they put a backslash, or
asterisk in their name? These are invalid characters in filenames. You can use the member function to check whether the file was opened
properly. See the end of the Chapter 5 slides for an example. Also, see the “Inventory
with File Input and Output” example in our Class Practice folder for a full program using
an output file.
Tiered grading:
50% Prompt the user and read their name. Give them starting instructions
outside of the loop. Create a loop which will prompt the user inside the
loop, and read their choice. The loop should end if they choose to quit.
60% Handle even and odd bets. Add winnings and subtract losses. The loop
should end if the player’s money runs out. The user’s menu choice MUST
be text, not int or double, and you should be able to handle both
upper- and lower-case entries. You can use either char or string for
the menu choice.
70% Ensure that an even bet does not win on a spin of 0.
80% Display the ending information (wins and losses) on screen, and create the
data file named after the user, containing the total number of spins and
either their total earnings or total losses. Do not write any information if
the file cannot be opened. Close the file if it was opened.
Up to 20% will be awarded for good programming practices, broken down as follows:
4% Use of named constants; meaningful constant and variable names.
5% Proper formatting, indenting, and use of blank lines.
10% Meaningful comments. Add a block comment at the start of the file, and
comment each section of code. Your comments should be in proper
English. Your comments should be understandable and (mostly)
grammatically correct. Do NOT use IM-speak, such as “These variables r
4 the duration and cost”.
1% No compiler warnings. This should not be an issue on this project, but if
you have trouble with any, let me know and I will help you clean them up.
For this first project, be sure to look at the Coding Guidelines file for examples of good
comments, indenting, etc. The textbook also provides good examples of indenting.
Make sure that your program compiles under Visual Studio (Pro or Express). If you have
been programming on a Mac, test your program with Visual Studio before handing it in.
With the tiered grading system, most of you should have a good idea of approximately
how many points you are going to receive.
When you are done, place your source code file in the Project 2 Dropbox.
A few notes to help you:
Most of the information maintained by this program can be stored in integer variables:
the current cash, the number of wins and losses, and the randomly-generated roulette
number. The user’s name (and the generated file name) should be stored as strings, while
the menu choice should be stored as a character or a string.
Below is a sample session of using the program and getting correct output (the input
typed by the user is shown in blue). Note that this is only an example! Your output does
not have to look exactly like this, but it should include the same information. Output a
blank line between bets to make it more readable.
What is your name? Joe
Welcome to roulette, Joe! You may bet on Even, Odd, or Zero
(use E, O, or Z). Each bet is $5.
If you bet on zero and win, you win $50!
You can choose Q to quit the game.
The game ends automatically if you run out of money.
Bet on E)ven, O)dd, Z)ero or Q)uit > e
The wheel comes up 25
Sorry, you lose. You now have $95
Bet on E)ven, O)dd, Z)ero or Q)uit > o
The wheel comes up 22
Sorry, you lose. You now have $90
Bet on E)ven, O)dd, Z)ero or Q)uit > o
The wheel comes up 3
You win! You now have $95
Bet on E)ven, O)dd, Z)ero or Q)uit > z
The wheel comes up 5
Sorry, you lose. You now have $90
Bet on E)ven, O)dd, Z)ero or Q)uit > a
Please enter E for Even, O for Odd, Z for Zero, or Q to quit.
Bet on E)ven, O)dd, Z)ero or Q)uit > E
The wheel comes up 15
Sorry, you lose. You now have $85
Bet on E)ven, O)dd, Z)ero or Q)uit > q
Your remaining money is $85
Number of wins: 1
Number of losses: 4
After this, but before the program ends, create a file named Joe-Roulette.txt, and
in it you would place information similar to the following:
Total number of bets: 5
User lost $15