Introduction
,,main
Requirement
Programming Assignment #3
o Goal: To get familiar with the following
Functions , parameters passing , by-value vs by-referece
control structures : while loop / For loop
selection statements : if-else , switch-case statements
random number generator function rand(), set seed
function :rand(unsigned)
simple C++ class
o For this homework, you have to implement a “simplified “ craps Game in C++.
About : Simplified Craps
In this game , two dice are being rolled. A player simply wins if
he/she throws 2, 3, 4, 10, 11 or 12 and
loses if he/she rolls 5, 6, 7, 8 or 9.
The players disadvantage in the number of winning combinations is
repaid in the payoffs which are:
Total Payoff
2 3 to 1
3 or 4 1 to 1 (evens)
10 or 11 2 to 1
12 5 to 1
Requirement :
Use the given Dice class (), to implement the simplified Craps
Game.
What to submit: only the C++ source codes in plain text format .
Make sure that your code is well-documented. up to 10 %
deduction if your code is not documented or ill-documented.
Make sure your code is compiling error free.
#ifndef DICE_H
#define DICE_H
#include
#include
#include
using namespace std;
class Dice {
private :
int face ; // # of face (side) of the dice
int value ;
public:
int getFace() { return face ; }
int getValue() { return value ; }
Dice() ; // default c’tor
Dice(int face) ; // c’tor for Dice of given face
Dice (Dice other) ; // copy c’tor
Dice perator= (Dice rhs) ; // overload assignment operator
int operator+(Dice rhs) ; // overload ++ operator
int roll() ; // roll the dice and return the resulting value
};
Dice::Dice() {
face = 6 ;
srand((unsigned) time(NULL)) ;
value = roll() ;
}
Dice::Dice(int face) {
this->face = face ;
srand((unsigned) time(NULL)) ;
value = roll() ;
}
Dice::Dice(Dice other) {
face = other.getFace() ;
value = other.getValue() ;
}
Dice Dice::operator= (Dice rhs) {
if (this != rhs ) {
face = rhs.getFace() ;
value = rhs.getValue() ;
}
return *this ;
}
int Dice::operator+(Dice rhs) {
int sum = value + rhs.getValue() ;
return sum;
}
int Dice:: roll() {
value = (rand() % face) + 1 ;
return value;
}
#endif
Sample Run
Welcome to Simplified Craps Game
Please enter your age : 21
Here is the rule of Simplified Craps Game
A player puts up wager against the banker and rolls two dice.
Player simply wins if he throws either 2, 3, 4, 10, 11 or 12
and loses if he rolls either 5, 6, 7, 8 or 9.
Player’s payoff is given as follows:
Total Payoff
2 3 to 1
3 or 4 1 to 1 (evens)
10 or 11 2 to 1
12 5 to 1
Are you ready to play? (y/n) y
Please enter your stake : 100
please enter your bet : 20
you have got (2,5) score 7 , you lose!
Do you want to play another round? (y/n) y
Your current balance is $ 80.00
please enter your bet : 20
you have got (4,3) score 7 , you lose!
Do you want to play another round? (y/n) y
Your current balance is $ 60.00
please enter your bet : 20
you have got (2,1) score 3 , you are break-even!
Do you want to play another round? (y/n) y
Your current balance is $ 60.00
please enter your bet : 20
you have got (2,1) score 3 , you are break-even!
Do you want to play another round? (y/n) y
Your current balance is $ 60.00
please enter your bet : 20
you have got (6,6) score 12 , you win!
the payoff is $ 100.00
Do you want to play another round? (y/n) y
Your current balance is $ 140.00
please enter your bet : 20
you have got (5,6) score 11 , you win!
the payoff is $ 40.00
Do you want to play another round? (y/n) y
Your current balance is $ 160.00
please enter your bet : 20
you have got (1,1) score 2 , you win!
the payoff is $ 60.00
Do you want to play another round? (y/n) n
Thank you for playing Simplified Craps Game!
Here is the game summary:
Total number of rounds played : 7
Your starting balance :$100.00
Your final balance :$200.00
Congratulation!! Your have won a total of $100.00 !
Don’t forget to report your winning as earning to IRS!!