CPS 6 Fall 2000: Assignment #4

Due: Tuesday, Oct. 24 by 11pm

15 points

Setup

Assignments are to be your own work. You may consult with UTAs, TAS, professors and other CPS 6 students if you get stuck, but the final writeup is to be yours. Please see the CPS 6 web page for the definition of consult.

See the CPS 6 web page about the late policy for programming assignments.

In your cps006 directory, you should create an assign4 directory to work in. You'll then copy files into this directory to help you get started. That is, type the following unix commands from your account after you login (don't forget the trailing dot on the last line).

cd cps006 mkdir assign4 cd assign4 cp ../assign3/slot.cpp . cp ../assign3/Makefile .

If you did these commands correctly, typing pwd should show that you are in your assign4 directory, and typing ls should show the files

Note that 20% of your program grade is based on the readability of your program. This includes things like style, comments, and the naming of variables and functions. Make sure you include a comment at the top of each program with your name, course and purpose of the program specified.

// Name: YOUR NAME HERE // // Course: CPS 6 // // Lecture Section No: // // Lab Section No: // // Purpose: purpose of program here //

Problem : (15 pts) Gambling Fool Again

For this assignment, you will rewrite the slot machine program from assignment 3 using a class. For example, you could have a Game class. You will need to decide what data should be in the private section and which functions should be in the class.

For 5 points extra credit, you should have two classes. Possibilities include a Game class and a Machine class.

To make it easier to compile, you should put the class definition and the functions for the class all in the same file with the main function, in slot.cpp. If you create multiple files, it will not compile with the given Makefile, plus we will require all the code in slot.cpp to make it easier for grading.

Start with slot.cpp and in the same file near the top create the class definition, that is:

class Game
{
  public:
    Game();
    void Spin();
    void PrintSpin();

  private:
};

Note the semicolon after the }.

These are just examples of possible functions. You might want to use these and maybe add arguements to them, maybe not. Or create your own functions. You'll probably need more functions than this. How does one manage the money? You should have a lot of member functions in the class. Which ones should be accessor functions and which ones should be mutator functions? If you have private data that the user needs to see, consider including an accessor function to return that info to the user.

Give the function prototypes for each member function you want to write for your class. Should they be public or private functions? Public functions are ones the user will call. Private functions are extra functions to make your class more modular, and you don't want the user to call them. Instead they will be called by functions in the class.

Then below the class definition in slot.cpp, put the actual function with an emtpy body and try to compile. Note that in the code for the actual functions you will include the class name and ::, in this case Game::.

Game::Game()
{
}

void Game::Spin()
{
}

void Game::PrintSpin()
{
}

Once this compiles, then add the code for the body of these functions. Compile again.

The main function and any other functions that use the class should be at the bottom of the file slot.cpp. Once the class compiles fine, then try using it.

Remember, whenever you add a function to the class, you will need to put its function signature (or prototype) in the class definition (either the public or private section), and then put its body below the class definition and add classname:: to the name (only for the actual function, this is not added to the function signature).

Whenever you add a new data type to the private section (all data should be in the private section) you should initialize it in the constructor.


Here are the details from assignment 3.

Write a program slot.cpp to play the slot machine. The file slot.cpp has been provided for you to help you get started. Your program should do the following.

  1. Ask the gambler how much money he/she wants to spend.
  2. Ask the gambler which dollar machine he/she wants to play (one, five or ten). The gambler will type in the word not the integer (ten, not the number 10). Money is exchanged for the corresponding coins: either 10 dollar coins, 5 dollar coins, or 1 dollar coins.
  3. Spend all the money possible in the slot machine. If the gambler wanted to spend 17 dollars and play the 5 dollar machine, then he/she would spend 15 dollars. Inform the gambler that the extra 2 dollars is not spent.
  4. The slot machine has three wheels that spin. The left and right wheels have the numbers 1-10 on them. The middle wheel has the numbers 1-9 and a special symbol "*" which matches with anything. For each coin entered in the machine, the handle is pulled and the wheels spin, eventually stopping.
  5. The gambler never spends his winnings.
  6. Each time the wheel is spun, show the three numbers (or "*") and print a message if the gambler won.
  7. At the end, print a message indicating how much money the gambler won, and a message indicating if the gambler made money, lost money, or broke even. Money not spent (for example, the 2 dollars in the example above) is not used in the comparison.

Below are two sample runs. Your output does not have to look exactly like this, but should follow the specifications above.

> slot
How many dollars do you want to spend:? 7
Which dollar machine do you wish to play (one, five, or ten)? one
Spinning... Numbers are: 2 9 1
Spinning... Numbers are: 5 * 9
*** Congrats, won $2
Spinning... Numbers are: 8 * 8
*** Congrats, won $10
Spinning... Numbers are: 9 2 2
*** Congrats, won $2
Spinning... Numbers are: 8 7 5
Spinning... Numbers are: 4 * 5
*** Congrats, won $2
Spinning... Numbers are: 9 9 2
*** Congrats, won $2
Total winnings are $18
You won money!
>> slot
How many dollars do you want to spend:? 88
Which dollar machine do you wish to play (one, five, or ten)? ten
You can spend all but 8 dollars
Spinning... Numbers are: 7 2 4
Spinning... Numbers are: 8 7 10
Spinning... Numbers are: 5 7 4
Spinning... Numbers are: 7 4 6
Spinning... Numbers are: 10 * 3
*** Congrats, won $20
Spinning... Numbers are: 2 9 3
Spinning... Numbers are: 4 5 7
Spinning... Numbers are: 3 * 5
*** Congrats, won $20
Total winnings are $40
You lost money!
>

Submit

When all your programs compile and produce the correct output, create a file named README (please use all capital letters in naming the file). In the file include your name, section number, the date, and an estimate of how long you worked on the assignment . You must also include a list of names of all those people with whom you consulted on the assignment. See the CPS 6 web page for the meaning of consult.

To submit your programs electronically you must be in your assign4 directory and type (REPLACE instructor with rodger or ramm depending on who your professor is, for example Prof. Rodger's students would type assign4rodger):

submit_cps006 assign4instructor *.cpp README

You should receive a message telling you that the programs were submitted correctly. If the submit_cps006 command does not work, then try

  ~rodger/bin/submit6 assign4instructor *.cpp README

To see that the programs were submitted correctly, type (REPLACE instructor with rodger or ramm depending on who your professor is, for example Prof. Rodger's students would type assign4rodger):

    submit_cps006 assign4instructor

with no arguments and a list of the files submitted will be shown. PLEASE RUN THIS COMMAND to make sure your programs were submitted correctly.

The submit command with *.cpp submits all of your .cpp programs in your current directory at the same time. You can resubmit again if you realize you made a mistake and want to change something. If you submit more than once, we grade the last submission. If the first submission is on time and the last one is late, then the programs are late. All programs must be turned in together. You cannot submit one program early and the rest late.