More Classes and Assignment 4

More Class Information

The concept of classes is extremely important.  It is what makes C++ a great language for collborative projects.  That is, it allows one group of people to work on a set of functions that can be plugged into a program that another group of people are working on.  This is done by one group working on a class that can be incoroporated into the main project using #include statements.

Some of you expressed some confusion over what the #include statements actually do.  It is really quite simple.  They include another file in your program.  This gives you access to the functions that someone else has already written.  We did this right from the start using cout and cin.  These functions are not actually built into C++.  We only had access to them when we included the iostreams header file.  The difference between the header file (filename.h) and the source file (filename.cpp) is that the header file contains a list of the functions and variable that are included, while the source file contains the functions themselves.  The header file, therefore, is useful for understanding what functions you will have access to when you include that file.

Assignment 4

Assignment 4 is to create a computer version of the game of craps.   Craps is really quite a simple game.  Here are the rules as written in A Computer Science Tapestry

A player rolls two dice.  If the sum of the two is 7 or 11, the roller wins at once;  if the sum is a 2, 3 or 12, the roller loses at once.  If the sum is 4, 5, 6, 8, 9 or 10, the roller rolls again.  By repeating the initial number, the roller "makes his or her piont" and wins.  By rolling a 7 the roller "craps out" and loses.  Otherwise, the roller keeps on rolling again until he or she wins or loses. 

Your assignment is to make this happen on the computer.  You can start by simulating one game, but then you should work betting into your program, and a recursive menu so a person can keep betting as long as they have money.  This is shown in the demo version of the program that you can find here.

Click Here to Download a DOS Version of the Program comptr9.gif (546 bytes)

We have not yet learned how to draw random numbers, but you will not need to.  I have included a header file called rando.h and a source file called rando.cpp that do this for you.  All you need to do is know how to use these files.  As with the school class you will need to download these two files and add them to your project using the following steps.   These steps must be followed in order.

  1. Go to Visual C++ and create a new project.  Give it the name yourlastname4 (where yourlastname is your actual last name).

  2. When you create a project, a new folder is created with the name of the project.  Make sure you know where that folder is.

  3. Right click on the two files above (rando.h and rando.cpp) and choose "Save link as..." Then select the folder where you created your project.

  4. Switch back to Visual C++ and go to the project menu.   Select Add to Project and then Files.   Find rando.cpp and double-click on it.

  5. Create a new source file by going to the File menu and selecting New.  Then select C++ Source File, and give it a name.

  6. You are now ready to begin your part of the program.

Using the rando header file is quite easy.  You only need to know three things about it. 

  1. You must #include "rando.h" on the first line of your program.

  2. You need to have a variable of type RandGen in order to generate random numbers.  This is done, for example with a line like RandGen myrandomnumber.  It may be a good idea to make this variable global.

  3. You can generate a random number from a to b (where a and b are integers) using the line variableoftypeRandGen.RandInt(a,b), where variableoftypeRandGen is an actual variable of type RandGen.   Continuing with the previous example, we could use the line myrandomnumber(1,6).

A sample main program that generate one random roll of one die is shown here:

#include "rando.h"
#include <iostream.h>
RandGen myrandomnumbers;  //defines myrandomnumbers as type RandGen
int main(void)
    int dieroll;
    myrandomnumbers.RandInt(1,6); //gets 1st bad random # out of the way
    dieroll=myrandomnumbers.RandInt(1,6);  //generates a random number from 1 to 6
                                                                          //and puts it in the variable dieroll
    cout<<"You rolled a "<<dieroll<<endl;
    return 0;

You should note that when you are writing this program, that you cannot simply generate a random number from 2 to 12 to simulate the roll of two dice.   You must generate two random numbers from 1 to 6 and add them.  The distribution of results is much different this way.  You may not use the switch statement or the Dice class in this program (if you do not know what this means, then you are ok).

If you feel that you need more of a challenge than this program, you can write a Blackjack program instead.  The rules of blackjack are contained here.

This program should have comments on almost every line explaining what the program is doing, especially where calculations are made.  If you have no, or very few comments you will automatically lose 5 points on your assignment.

This program will be graded on the following factors:

  1. How well it works.
  2. How well you incorporate the RandGen class.
  3. How easy your program is to follow, including your use of functions.   Dividing your program up into easy to follow functions is mandatory. This will also include things like comments and variables that have meaningful names.
  4. How well you understand your program based on your comments.
  5. How creative your were in your approach to solving the problem.

This assignment will be due Monday, March 9. You must deposit the program in the class folder on the network (I:\COMP\C++ Class Klopfer\Assignment 4) by this date.  Projects that are not deposited there will not be graded.

Back to Home Page House3.wmf (25540 bytes)