Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Craps Program
Index -> Programming, C++ -> C++ Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
simonsayz




PostPosted: Thu Oct 23, 2008 11:38 pm   Post subject: Craps Program

Hi all. I have a craps assignment due soon and I have finished my code and wanted someone else besides myself to overlook it and see how it is. Any feedback would be great . thanks

c++:

// General program shell for Assignment III
// This is a program that simulates the game
// of casino craps.

// #include preprocessor directives
#include <iostream>
#include <cstdlib>            // for using rand() function
#include <ctime>                // for using time() function

// namespace directives
using namespace std ;

void CrapsIntro() ;               // brief description of Craps game
int  CrapsRoll() ;                  // outcome of two six-sided dice rolls
void myTimeDelay(int) ;  // time delay between rolls
void CrapsPassLine();       // Function for if chooses passline
void CrapsDontPassLine();   // Function for if chooses dont passline
char goAgain();         // option if user wants to play again

const int DELAY = INT_MAX / 5// manual delay between rolls


int main ()
{
    int mainBet;

        // Display game instructions
        CrapsIntro();

        // Ask user to play Pass Line OR Don't Pass Line
        cout << "Would you like to play the Passline or Don't Passline?" << endl;
        cout << "Enter '1' for Passline or '2' for Don't Passline" << endl;
        cin >> mainBet;

        while (mainBet){    // Used so that user can play again if they like
                if (mainBet == 1){
                                CrapsPassLine();   // Calls the PassLine function
                                if(goAgain() == 'N') {
                                        // User can choose to play pass or dont pass again
                                        myTimeDelay(DELAY);
                                        return 1;
                                }
                  }
                else if (mainBet == 2){
                                CrapsDontPassLine(); // Calls the DontPassLine
                                if(goAgain() == 'N') {
                                        // User can choose to play pass or dont pass again
                                        myTimeDelay(DELAY);
                                        return 1;
                                }
                }
                else{
                                cout << "Sorry, invalid entry" << endl;   
                }
                cin >> mainBet;
        }
        return 0;
}

   void CrapsPassLine()
   {
           int myRoll;       
           int pointRoll;   // If user hits a "point", it will be stored here
           int anotherRoll;
           myRoll = CrapsRoll()// Uses function CrapsRoll() to genereate a random number
          
           if (myRoll == 7 || myRoll == 11){
                   cout << "You win." << endl;
               return;
           }
           else if (myRoll == 2 || myRoll == 3 || myRoll == 12){
                   cout << "Sorry you lose." << endl;
               return;
           }
           else if (myRoll == 4 || myRoll == 5 || myRoll == 6 ||
                   myRoll == 8 || myRoll == 9 || myRoll == 10) {
                                cout << "The number you rolled is a point number." << endl;
                        pointRoll = myRoll;
           }
           cout << "Are you ready to roll again? Press '1' when ready" << endl;
           cin >> anotherRoll;   // Roll again after point is hit
          
           if (anotherRoll == 1){  // If it is true (User entered 1)
                  
             while (anotherRoll == 1)         // If it doesn't hit point or 7,
             {                                // loop is used until it does.
                         myRoll = CrapsRoll()// dice roll
                         if (myRoll == pointRoll){
                           cout << "Congradulations, you Win." << endl;
                           return;
                         }
                         else if (myRoll == 7){
                           cout << "Sorry, 7 came up. You crapped out." << endl;
                           return;
                         }
                         else {
                           cout << "You're still in the game. 7 has not yet rolled." << endl;
                           cout << "Are you ready to roll again? Press '1' when ready" << endl;
                         }
                          cin >> anotherRoll;  //go back to the loop to roll again
                 }
               
           } else {
                    cout << "Invalid entry" << endl;  // User did not enter 1.
           }   
          
    }
   void CrapsDontPassLine()
   {
           int myRoll;       
           int pointRoll;   // If user hits a "point", it will be stored here
           int anotherRoll;
           myRoll = CrapsRoll()// Uses function CrapsRoll() to genereate a random number
          
           if (myRoll == 7 || myRoll == 11){
                   cout << "Sorry. You lose." << endl;
               return;
           }
           else if (myRoll == 2 || myRoll == 3){
                   cout << "Congradulations, you Win." << endl;
               return;
           }
           else if (myRoll == 12){
                   cout << "Game is a draw. You do not lose." << endl;
           }
           else if (myRoll == 4 || myRoll == 5 || myRoll == 6 ||
                   myRoll == 8 || myRoll == 9 || myRoll == 10) {
                                cout << "The number you rolled is a point number." << endl;
                        pointRoll = myRoll;
           }
           cout << "Are you ready to roll again? Press '1' when ready" << endl;
           cin >> anotherRoll;   // Roll again after point is hit
          
           if (anotherRoll == 1){  // If it is true (User entered 1)
                  
             while (anotherRoll == 1)         // If it doesn't hit point or 7,
             {                                // loop is used until it does.
                         myRoll = CrapsRoll()// dice roll
                         if (myRoll == pointRoll){
                           cout << "Sorry. You lose." << endl;
                           return;
                         }
                         else if (myRoll == 7){
                           cout << "Congradulations, you win." << endl;
                           return;
                         }
                         else {
                           cout << "You're still in the game. pointRoll has not yet hit." << endl;
                           cout << "Are you ready to roll again? Press '1' when ready" << endl;
                         }
                          cin >> anotherRoll;  //go back to the loop to roll again
                 }
               
           } else {
                    cout << "Invalid entry" << endl;  // User did not enter 1.
           }
   }

void CrapsIntro()
     /* This function is only for the user to
         understand how the game works. It's only
         use is to output multiple strings.
         */


         // Pre-Condition: None
         // Post-Condition: None
{
        cout << "       The Pass Line bet" << endl;
        cout << "If the come-out roll is a 7 or 11, the player wins." << endl;
        cout << "If the come-out roll is 2, 3 or 12, the player loses (crapping out)." << endl;
        cout << "If the come-out roll is any other value (4, 5, 6, 8, 9, or 10)," << endl;
        cout << "it establishes a point." << endl;
        cout << "The player continues to roll, until:" << endl;
        cout << "If the established point is rolled again before a seven, the player wins." << endl;
        cout << "If, with an established point, a 7 is rolled before the established point" << endl;
        cout << "is re-rolled the player loses (seven out)." << endl;
        cout << endl;
        cout << "   The Don't Pass Line bet" << endl;
        cout << "If the come-out roll is 7 or 11, the player looses." << endl;
        cout << "If the come-out roll is 2 or 3, the player wins." << endl;
        cout << "If the come-out roll is 12, the game is a draw and the player does not lose." << endl;
        cout << "If the come-out roll is any other value (4, 5, 6, 8, 9, or 10)," << endl;
        cout << "it establishes a point." << endl;
        cout << "The player continues to roll, until:" << endl;
        cout << "If the established point is rolled again before a seven, the player loses." << endl;
        cout << "If, with an established point, a 7 is rolled before," << endl;
        cout << "the established point is re-rolled the player wins." << endl ;
        cout << endl;
}


void myTimeDelay(int delay)
   // This function creates a time delay so
   // that the computer doesn't generate the
   // same outcome number for the dice.
{
        int count = 0 ;
        while (delay > count)
        {
                // do nothing delay period
                count++;
        }
}


int CrapsRoll()
    /* The purpose of this function is to create
    a random roll for two die and return to the
    caller. It has another function inside for
    time delay.
    */


        // Pre-Condition: None
        // Post-Condition: Send back to the caller
        // the sum of two die's being randomly rolled
{
        int randomNumber ;                  // a random number
        int dieOne ;                // a six-sided die roll value
        int dieTwo ;                // a six-sided die roll value
       
        cout << "Please Wait..." << endl;

        myTimeDelay(DELAY);

        cout << "Rolling my two dice ..." << endl ;
       
        // die one
        srand(int(time(0))) ;            // seeding random number generator
        randomNumber = rand() ;   // generate random number
        dieOne = (randomNumber % 6) + 1 ;       // a number between 1 and 6
       
        myTimeDelay(DELAY) ;

        // die two
        srand(int(time(0))) ;            // seeding random number generator
        randomNumber = rand() ;   // generate random number
        dieTwo = (randomNumber % 6) + 1 ;       // a number between 1 and 6

        cout << "You rolled a " << dieOne + dieTwo << endl ;
       
        return dieOne + dieTwo ;
}

char goAgain()
        /* This function prompts the user if they would
        like to play again. They can either press
        'Y' for yes or 'N' for no. They also have a
        choice in changing to the passline OR the dont passline
        */


        // Pre-Condition: None
        // Post-Condition: If 'Y' || 'N' is entered,
        // it sends back to the caller and enters a loop.
{
       cout << "Would you like to play again?" << endl;
           cout << "Enter 'Y' for Yes or 'N' for No." << endl;
           char playAgain;
           cin >> playAgain;

           while (playAgain) {
                   if (playAgain != 'Y' && playAgain != 'N'){
                cout << "Invalid entry. Please select 'Y' or 'N'. (Capitals)" << endl;
           }
           if (playAgain == 'N'){
                   cout << "Thank you for playing. Good Bye" << endl;
               return playAgain;
           }
        else if (playAgain == 'Y'){
                   cout << "Would you like to play the Passline or Don't Passline?" << endl;
               cout << "Enter '1' for Passline or '2' for Don't Passline" << endl;
                   return playAgain;
        }
         cin >> playAgain;
        }
}


[edit by md] Fixed formatting and code block. Next time use [code] or [syntax]!
Sponsor
Sponsor
Sponsor
sponsor
Rigby5




PostPosted: Fri Oct 24, 2008 2:36 pm   Post subject: RE:Craps Program

Not bad.
Normally I would have put the function into a .h or .hpp file, but I don't think it is necessary in this case, since there are no classes anyway.

If I were to suggest anything, it might have been to see if you could have made a smaller amount of code that was more flexible.
For example, would it have been possible to have the same game code work with both variations of the passline thing?
Could you have simply used different number sets for what was win, lose, draw, or set?

You may also want to use a system call for the delay wait, like sleep().
A busywait loop wastes computer time.

If you were doing a commercial program, you would also have put the intro text in a separate file, so that it could be changed without touching the program that just read in and displayed the file contents.
md




PostPosted: Sat Oct 25, 2008 12:47 am   Post subject: RE:Craps Program

Functions do not belong in header files. Function prototypes, class prototypes, and templates belong in header files. Functions belong in source files.

The only thing I can think of in the OP besides usnig sleep is that using namespace std is poor practice IMHO. Namespaces exist for a reason and mixing them can occasionally lead to problems.
Rigby5




PostPosted: Wed Oct 29, 2008 12:03 pm   Post subject: Re: RE:Craps Program

md @ Sat Oct 25, 2008 12:47 am wrote:
Functions do not belong in header files. Function prototypes, class prototypes, and templates belong in header files. Functions belong in source files.

The only thing I can think of in the OP besides usnig sleep is that using namespace std is poor practice IMHO. Namespaces exist for a reason and mixing them can occasionally lead to problems.



Agreed. I meant prototypes, but did not type that.
You can see the list of function prototypes at the beginning of the code.
If I am not putting prototypes in a .h file, I usually just put the function before they are called, and skip the prototypes entirely.
Display posts from previous:   
   Index -> Programming, C++ -> C++ Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 4 Posts ]
Jump to:   


Style:  
Search: