Welcome to Dream.In.Code
Getting Help is Easy!

Join 109,297 Programmers for FREE! Ask your question and get quick answers from experts. There are 1,217 online right now! We've got more than 500 tutorials and 2,000 snippets. Join and find out why Dream.In.Code is the #1 programming help community on the internet! Registration is fast and FREE... Join Now!



Simple craps game

 
Reply to this topicStart new topic

Simple craps game

tortillaboy05
post 2 Jul, 2008 - 05:17 PM
Post #1


New D.I.C Head

*
Joined: 2 Jul, 2008
Posts: 20


My Contributions


This is a simple game of craps that I wrote. It has a betting system and keeps track of how many times you won. I'm looking for some comments, opinions, criticisms anything is appreciated.

main.cpp:

CODE

#include <iostream>
#include <math.h>
#include <time.h>
#include <iomanip>

#include "dice.h"

using namespace std;

int main()
{
    float bank = 100, bet;
    int roll, ctr = 0, conf;
      
    cout << setprecision(2) << fixed;
    
    do
    {
        
      system("cls");
      conf = menu(bank);
      
    }while(conf > 2);
        
    srand(time(NULL));
    
    while(conf == 1)
    {          
               bet = wager(bank);    
               die();
               roll = dice();
              
               if(roll == 7 || roll == 11)
               {
                    firstWin(ctr, roll, bank, bet);
                    
               }
               else if(roll == 2 || roll == 3 || roll == 12)
               {
                    firstLose(roll, bank, bet);
               }
               else
               {                  
                  reRoll(ctr, roll, bet, bank);
               }
              
               if(bank == 0)
               {
                   cout << "\n\nYou are out of money. ";    
                   break;
               }
                                                  
               do
               {
                  system("pause");
                  system("cls");
                  conf = menu(bank);
      
               }while(conf > 2);
    }
    
    summary(ctr, bank);
    
    system("PAUSE>nul");
    return EXIT_SUCCESS;
}


dice.h:
CODE

#ifndef _DICE_H_
#define _DICE_H_
#include <iostream>
#include <math.h>
#include <time.h>
#include <iomanip>


using namespace std;

int die(void);
int dice(void);
void firstWin(int &, int, float &, float);
void firstLose(int, float &, float);
void reRoll(int &, int, float, float &);
int menu(float);
float wager(float);
void summary(int , float);

int die()
{
    return(rand() % 6 + 1);
}

int dice()
{
    return(die() + die());
}

void firstWin(int &ctr, int roll, float &bank, float bet)
{
     cout << "\n\nYou rolled a " << roll << "!!!\n\nYOU WIN!!!\n\n\n";
     bank = bank + bet;
     ++ctr;
}

void firstLose(int roll, float &bank, float bet)
{
     cout << "\n\nYou rolled a " << roll << "!!!\n\nYOU LOSE!!!\n\n\n";
     bank = bank - bet;
}

void reRoll(int &ctr, int roll, float bet, float &bank)
{
     int points = roll;
     int conf;
                  
     cout << "\n\nYou rolled a " << roll << "!!!";
                  
     cout << "\n\n\nEnter 1 to roll...";
     cin >> conf;
     cout << "\n___________________________________";

      while(conf == 1)
      {
           roll = dice();
                        
           if(roll == points)
           {
               cout << "\n\nYou rolled a " << roll
                    << "!!!\n\n\nYOU WIN!!!";
              
               bank = bank + bet;
                          
               ++ctr;
                
               break;    
           }
           else if(roll == 7)
           {
                cout << "\n\nYou rolled a " << roll
                     << "!!!\n\n\nYOU LOSE!!!";
                
                bank = bank - bet;
                    
                break;    
           }
           else
           {
               cout << "\n\nYou rolled a " << roll <<"!!!";
              
           }
                        
           cout << "\n\nEnter 1 to roll...";
           cin >> conf;
           cout << "__________________________________";
      }
}

int menu(float bank)
{
    int x;
        
      cout << "\n\n_______________CRAPS________________\n\n"
           << "\n1. Roll"
           << "\n2. Quit"
           << "\n3. Rules of Craps"
           << "\n4. Check your balance"
           << "\n\nEnter your choice: ";
      cin >> x;
          
      switch(x)
      {              
              case 1: return(x);
              case 2: return(x);
              case 3: cout << "\n\n\nOn Your first roll:\n\nIf you roll a 7 or"
                           << " 11 you win.\nIf you roll a 2, 3 or 12 you lose.\n"
                           << "If you roll anything else that becomes your points"
                           << " and you roll again.\n\n"
                           << "On all rolls after the first:\n\nIf you roll a 7 "
                           << "you lose.\nIf you roll your points again you win."
                           << "\nIf you roll anything else you roll again."
                           << "\nHave fun and good luck!\n\n";
                      return(x);
              case 4: cout << "\n\nYour balance is " <<bank <<".\n\n";
                      return(x);
                      break;
              default: cout << "Invalid input, try again\n\n";
     }
    
}

float wager(float bank)
{
      float bet;
      
      cout << "___________________________________"
           << "\n\nYour balance is " <<bank << ".\n\n";
      
      do
      {
        cout << "How much would you like to bet? ";
        cin >> bet;
        
        if(bet > bank)
        {
               cout << "\n\nYou don't have enough money.\n\n";
               continue;
        }
        else
        {
           return(bet);
        }
      }while(bet > bank);
            
}

void summary(int ctr, float bank)
{
     if(ctr < 5)
         cout << "\n\nNice try. You won " << ctr << " times and ended the game "
              << "with " <<bank << " dollars.";
     else if(ctr < 10)
          cout << "\n\nGood job! You won " <<ctr << " times and "
               << "ended the game with " <<bank << " dollars.";
     else
         cout << "\n\nYou have really good luck! \nYou won " <<ctr << " times and "
              << "ended the game with " <<bank << " dollars.";
}
#endif


Let me know what you guys think smile.gif. Thanks.

User is offlineProfile CardPM

Go to the top of the page


manhaeve5
post 3 Jul, 2008 - 12:54 AM
Post #2


D.I.C Regular

Group Icon
Joined: 28 Dec, 2006
Posts: 491



Dream Kudos: 225
My Contributions


Nice one there smile.gif

It's obvious that u thought about the different things very well. I was quite surpised by the message that says how many times u won when u quit, I wouldn't have tought of that.

Keep on the good work! icon_up.gif

Just a little thing, in C++, use <cmath> instead of <math.h> and <ctime> instead of <time.h>.

This post has been edited by manhaeve5: 3 Jul, 2008 - 12:55 AM
User is offlineProfile CardPM

Go to the top of the page

baavgai
post 3 Jul, 2008 - 04:54 AM
Post #3


Dreaming Coder

Group Icon
Joined: 16 Oct, 2007
Posts: 1,572



Thanked 44 times

Dream Kudos: 325

Expert In: C, C++, Java, C#, ASP.NET, PHP, Perl, Python, Oracle, SQL Server, MySql, HTML, JavaScript, Lua

My Contributions


Overall, well done. However, you can still clean stuff up. Personally, I'd use a struct to hold the state information, but that's mostly cosmetic.

The big one is that reRoll seems to almost duplicate the functionality of your main loop, which is kind of confusing. Also, you print out the roll, win, and loss info to the user in many places. Is if you can do it in as few times as needs. Here's some code to play with.

cpp

void showWin(float &bank, float bet, int &ctr) {
cout << "YOU WIN!!!\n\n\n";
bank = bank + bet;
++ctr;
}

void showLoose(float &bank, float bet) {
cout << "YOU LOSE!!!\n\n\n";
bank = bank - bet;
}

int points = 0;
int conf = 1;
while(conf == 1) {
roll = dice();
cout << "You rolled a " << roll << "!!!" << endl << endl;

if (points==0) {
if(roll == 7 || roll == 11) {
showWin(bank, bet, ctr);
} else if(roll == 2 || roll == 3 || roll == 12) {
showLoose(bank, bet);
} else {
points = roll;
cout << "You now need to match " << roll << "." << endl << endl;
}
} else {
if(roll == points) {
showWin(bank, bet, ctr);
points = 0;
} else if(roll == 7) {
showLoose(bank, bet);
points = 0l;
} else {
cout << "No match, no bust, keep trying.";
}
}
cout << "\n\nEnter 1 to roll...";
cin >> conf;
cout << "__________________________________";
}


Hope this helps.
User is offlineProfile CardPM

Go to the top of the page

gabehabe
post 3 Jul, 2008 - 06:01 AM
Post #4


T3H R0XX0R!

Group Icon
Joined: 6 Feb, 2008
Posts: 2,591



Thanked 51 times

Dream Kudos: 1450

Expert In: (X)HTML, CSS, Batch Scripting, C, C++

My Contributions


Just a little thing, more to do with personal opinion.

bank = bank + bet; looks kinda messy, you could instead be using bank += bet;

You could use this for other things as well, the common operators are:
*= /= += -= %=

Good work though icon_up.gif

This post has been edited by gabehabe: 3 Jul, 2008 - 06:03 AM
User is online!Profile CardPM

Go to the top of the page

tortillaboy05
post 3 Jul, 2008 - 08:45 AM
Post #5


New D.I.C Head

*
Joined: 2 Jul, 2008
Posts: 20


My Contributions


I wrote this about 2 month ago, I had been away from any kind of programming for awhile, and I just got back into school. Actually I wrote this before we learned about structures. Also, the libraries I used were ones specified by my professor, so i'll definitly have to update that.

I see what your saying baavgi, what you wrote makes sense to me now. I guess when I was writing this, thats what it looked like in my head and thats why I wrote it that way haha.

Thanks for the input, I'm glad I found this community, everyone is very helpful. smile.gif
User is offlineProfile CardPM

Go to the top of the page

Fast ReplyReply to this topicStart new topic
Time is now: 9/6/08 09:41AM

Live Help!

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month