Important alert: (current site time 7/15/2013 6:11:07 PM EDT)
 

VB icon

Dice Gambler

Email
Submitted on: 2/27/2008 6:00:51 PM
By: Michael A. Delfino  
Level: Beginner
User Rating: By 2 Users
Compatibility: C++ (general), Microsoft Visual C++
Views: 6558
(About the author)
 
     Simple Dice Game

 

INCLUDE files:

Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
//**************************************
//INCLUDE files for :Dice Gambler
//**************************************
#include <iostream>
#include <cstdlib>
#include <ctime>
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
 
Terms of Agreement:   
By using this code, you agree to the following terms...   
  1. You may use this code in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.
  2. You MAY NOT redistribute this code (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.   
  3. You may link to this code from another website, but ONLY if it is not wrapped in a frame. 
  4. You will abide by any additional copyright restrictions which the author may have placed in the code or code's description.
				
//**************************************
// Name: Dice Gambler
// Description:Simple Dice Game
// By: Michael A. Delfino
//
//This code is copyrighted and has// limited warranties.Please see http://www.Planet-Source-Code.com/vb/scripts/ShowCode.asp?txtCodeId=12004&lngWId=3//for details.//**************************************

/*
Michael Delfino
CS 115 Section 007
megabeano@gmail.com
This program is a simple dice rolling game.
The player rolls a die and that initiates his/her
score. The player may continue to roll until they
match their intial roll and each roll will add to their
total score. If they do match, they lose all of their
points. They may choose to stop rolling before that.
If so, they win their total score up until that point.
The program will ask if they want to play again after
each game until they decline.
I will use the libraries iosteam (use cout and cin to
get input from keyboard and display output in the
console) and ctime (to use time to seed the random
number generator). I will also use cstdlib to err on the
side of caution and compatability.
*/
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
//The getrand function returns a random number between 1 and 6.
int getrand();
//drawdice will call a draw function based on what number the player rolls.
void drawdice(int die);
//Each of these functions will draw the face of a die based on the number.
void draw1();
void draw2();
void draw3();
void draw4();
void draw5();
void draw6();
int main() {
 //num will be the initial roll and newnum will be subsequent rolls
 int num, newnum;
 //These chars will act as sentinels for the main game loops
 char reroll, playagain = 'y';
 //This bool will let us know if a roll matches the initial roll
 bool match;
 //Seed the random number generator using time
 srand((unsigned)time(NULL));
 //Main game loop
 while ( playagain == 'y' || playagain == 'Y' ) {
 //initialize our flag
 match = false;
 //get initial roll
 num = getrand();
 //initialize score to first roll
 int score = num;
 //inform player of the rules and their first roll
 cout << "\n\nDon't Match!!!!!\n\n";
 cout << "First roll is a " << num << endl;
 //draw intial roll
 drawdice(num);
 //elaborate on rules and ask player if they want to roll agiain
 cout << "You can roll as long as you don't roll another "
<< num << endl << endl;
 cout << "Do you want to roll again? (y/n) ";
 //primer for while loop
 cin >> reroll;
 //game loop, will run if player wants to roll again and they don't match
 while ( reroll == 'y' || reroll == 'Y' ) {
 //get next roll
 newnum = getrand();
 //draw die and tell player what they rolled
 drawdice(newnum);
 cout << "You rolled a " << newnum << endl;
 //If roll matches initial roll
 if (newnum == num) {
 //set score to zero
 score = 0;
 //don't allow the loop to run again
 reroll = 'n';
 //activate flag (roll matched initial roll)
 match = true;
 }
 //If roll doesn't match intial roll
 else {
 //add roll to score
 score = score + newnum;
 //display score and ask if player wants to roll again
 cout << "Your total is now " << score << "\n\n";
 cout << "Do you want to roll again? (y/n) ";
 //get value for reroll to see if the loop should run again
 cin >> reroll;
 }
 }
 //If player declined offer to roll again before they matched
 if (match == false)
 //Tell them how much they won
 cout << "You won $" << score << "!!\n\n";
 //If they rolled until they matched
 else
 //inform them that they matched and therefore they lost
 cout << "You matched!! You lose!\nToo bad!\n\n";
 //Ask player if they want to play again
 cout << "Do you want to play another game? (y/n) ";
 cin >> playagain;
 }
 return 0;
}
//The getrand function returns a random number between 1 and 6.
int getrand() {
 return ( rand() % 6 + 1);
}
//drawdice will call a draw function based on what number the player rolls.
void drawdice(int die) {
 if (die == 1)
 draw1();
 if (die == 2)
 draw2();
 if (die == 3)
 draw3();
 if (die == 4)
 draw4();
 if (die == 5)
 draw5();
 if (die == 6)
 draw6();
}
//Will draw a die showing face 1
void draw1() {
 cout << "*********" << endl
<< "**" << endl
<< "**" << endl
<< "***" << endl
<< "**" << endl
<< "**" << endl
<< "*********" << endl;
}
//Will draw a die showing face 2
void draw2(){
 cout << "*********" << endl
<< "* * *" << endl
<< "**" << endl
<< "**" << endl
<< "**" << endl
<< "* * *" << endl
<< "*********" << endl;
}
//Will draw a die showing face 3
void draw3(){
 cout << "*********" << endl
<< "* * *" << endl
<< "**" << endl
<< "***" << endl
<< "**" << endl
<< "* * *" << endl
<< "*********" << endl;
}
//Will draw a die showing face 4
void draw4(){
 cout << "*********" << endl
<< "* ** *" << endl
<< "**" << endl
<< "**" << endl
<< "**" << endl
<< "* ** *" << endl
<< "*********" << endl;
}
//Will draw a die showing face 5
void draw5(){
 cout << "*********" << endl
<< "* ** *" << endl
<< "**" << endl
<< "***" << endl
		<< "**" << endl
<< "* ** *" << endl
<< "*********" << endl;
}
//Will draw a die showing face 6
void draw6(){
 cout << "*********" << endl
<< "* ** *" << endl
<< "**" << endl
<< "* ** *" << endl
<< "**" << endl
<< "* ** *" << endl
<< "*********" << endl;
}


Other 4 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Beginner category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments

3/1/2008 5:05:52 AMThe ShadowMan

Hey, pretty simple but funny! I'm thinking if it wouldn't be cooler, instead of having all those if's to choose the right function to draw a die, you would have a function array and that you'd call like DrawDie[die] - I think it's simple and cool! What do you think?
Cool job anyway!
(If this comment was disrespectful, please report it.)

 
3/5/2008 4:33:56 PMMichael A. Delfino

Yeah, I agree there are more efficient ways I could've handled the dice drawing functions. This was for my beginning programming class last semester so there were guidelines we had to follow. Thanks for the feedback!
(If this comment was disrespectful, please report it.)

 
9/23/2010 11:00:50 AMRizkvn

i will try before
(If this comment was disrespectful, please report it.)

 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.