CoinTossSimulator.h
TossingCoinsForADollar.cpp
TossingCoinsForADollarDm.cpp
CoinTossSimulator.h
/* CoinTossSimulator.h - Specification file for the Coin class. */
#ifndef COIN_TOSS_SIMULATOR_H
#define COIN_TOSS_SIMULATOR_H
#include <string>
class Coin
{
private:
std::string sideUp; // The side of the coin facing up
int numWins; // Holds the accrued number of wins
int numLosses; // Holds the accrued number of losses
int balance; // Holds the balance
public:
Coin::Coin() : balance(0), numWins(0), numLosses(0) // Constructor
{
initRNG();
}
void initRNG(); // Initializes the random number generator
void toss(); // Performs a coin-toss
void setBalance(int); // Adds coin values to the balance member
void setScore(); // Sets the current score
std::string getSideUp() const // Returns the side of the coin facing up
{ return sideUp; }
int getNumWins() const // Returns the number of wins
{ return numWins; }
int getNumLosses() const // Returns the number of losses
{ return numLosses; }
int getBalance() const // Returns the current balance
{ return balance; }
};
#endif
TossingCoinsForADollar.cpp
/* CoinTossSimulator.cpp - Implementation file for the Coin class. */
#include <cstdlib>
#include <ctime>
#include "CointTossSimulator.h"
/* **********************************************************
Coin::initRNG()
This function initializes the RNG.
********************************************************** */
void Coin::initRNG()
{
srand((unsigned int)time(0));
}
/* **********************************************************
Coin::toss()
This function determines the coin side facing up after
each toss. The side facing up is assigned to the sideUp
member variable.
********************************************************** */
void Coin::toss()
{
const int HEADS = 1;
const int TAILS = 2;
unsigned int sides = 0;
sides = HEADS + rand() % TAILS;
if (sides == HEADS)
{
sideUp = "Heads";
}
else
{
sideUp = "Tails";
}
}
/* **********************************************************
Coin::setBalance() accepts an integer value
This function adds the coin values passed to it to the
balance member.
********************************************************** */
void Coin::setBalance(int coinValue)
{
balance += coinValue;
}
/* **********************************************************
Coin::setScore()
The ternary determines the current balance. If it is 100,
numWins increases by 1, else numLosses increases, and the
balance is reset to 0.
********************************************************** */
void Coin::setScore()
{
getBalance() == 100 ? numWins += 1 : numLosses += 1;
balance = 0;
}
TossingCoinsForADollarDm.cpp
/* TossingCoinsForADollarDm.cpp - This program demonstrates the Coin class. */
#include <iomanip>
#include <iostream>
#include <string>
#include "CointTossSimulator.h"
#include "ClearScreen.h"
using std::cout;
using std::cin;
enum CoinValues { NICKEL = 5, DIME = 10, QUARTER = 25 };
enum MenuItems { INTRO = 'I', DISPLAY_SCORE = 'D', PLAY = 'P', QUIT = 'Q' };
void intro();
char options();
void menu(Coin &, Coin &, Coin &, Coin &);
void game(Coin &, Coin &, Coin &, Coin &);
void quit(const Coin);
int main()
{
Coin coinToss;
Coin nickel;
Coin dime;
Coin quarter;
menu(coinToss, nickel, dime, quarter);
cin.get();
cin.ignore();
return 0;
}
/* **********************************************************
intro()
This function welcomes the player and introduces him or
her to the game.
********************************************************** */
void intro()
{
cout << "\t\tTOSSING COINS FOR A DOLLAR GAME\n\n"
<< "Hello, friend! *Psst!* Yes, you, come closer! Closer! How about a little game?\n"
<< "Win a dollar each round, the odds are most fair! A nickel, a dime and a quarter\n"
<< "we toss, their faces we add, the tails we'll forget. The sum of a dollar, the goal\n"
<< "is set, play a couple of rounds, and your fortune is made!\n\n";
}
/* **********************************************************
options()
This function contains the menu options. The player is
asked to make a choice, which is validated, and - if valid
is returned.
********************************************************** */
char options()
{
char choice = ' ';
cout << "\t\tTOSSING COINS FOR A DOLLAR GAME\n\n"
<< "\t[I]ntro\n"
<< "\t[P]lay\n"
<< "\t[D]isplay Score\n"
<< "\t[Q]uit\n\n"
<< "\tSelect: ";
cin >> choice;
while (toupper(choice) < INTRO && toupper(choice) > QUIT)
{
cout << "\nSelect: ";
cin >> choice;
}
return (toupper(choice));
}
/* **********************************************************
menu() accepts four Coin objects passed to it by reference
This function acts as menu.
********************************************************** */
void menu(Coin &coinToss, Coin &nickel, Coin &dime, Coin &quarter)
{
char choice = ' ';
do
{
choice = options();
cout << "\n";
switch (choice)
{
case INTRO:
{
clearScreen();
intro();
} break;
case PLAY:
{
game(coinToss, nickel, dime, quarter);
cout << "Press Enter to Continue ...";
cin.get();
cin.ignore();
clearScreen();
} break;
case DISPLAY_SCORE:
{
cout << "Won: $" << std::setw(4) << coinToss.getNumWins() << "\n";
cout << "Lost: $" << std::setw(4) << coinToss.getNumLosses() << "\n";
cout << "Press Enter to Continue ...";
cin.get();
cin.ignore();
clearScreen();
} break;
case QUIT:
{
quit(coinToss);
}
}
} while (toupper(choice) != 'Q');
}
/* **********************************************************
game() accepts four Coin objects passed to it by reference
This function tosses the three coins as long as the score
is lower than or equal to 100. After each toss, the sides
currently facing up are output to screen, and the balance
as well as the score is set.
********************************************************** */
void game(Coin &coinToss, Coin &nickel, Coin &dime, Coin &quarter)
{
const int GOAL_SCORE = 100;
do
{
nickel.toss();
nickel.getSideUp() == "Heads" ? coinToss.setBalance(NICKEL) : 0;
dime.toss();
dime.getSideUp() == "Heads" ? coinToss.setBalance(DIME) : 0;
quarter.toss();
quarter.getSideUp() == "Heads" ? coinToss.setBalance(QUARTER) : 0;
// Display the side facing up after each toss
cout << "Nickel: " << std::setw(11) << nickel.getSideUp() << "\n";
cout << "Dime: " << std::setw(13) << dime.getSideUp() << "\n";
cout << "Quarter: " << std::setw(10) << quarter.getSideUp() << "\n";
cout << std::fixed << std::showpoint << std::setprecision(2);
cout << "Balance: " << std::setw(4) << coinToss.getBalance() << " cents\n\n";
if (coinToss.getBalance() == GOAL_SCORE)
{
cout << "$1.00 on the nickel and the dime? Incredible, "
<< "but next round will be mine.\n";
break;
}
else if (coinToss.getBalance() > GOAL_SCORE)
{
cout << "$" << (static_cast<double>(coinToss.getBalance()) / 100) << " cents "
<< "this round is mine! Better luck next dime!\n";
}
} while (coinToss.getBalance() <= GOAL_SCORE);
coinToss.setScore();
}
/* **********************************************************
quit() accepts a Coin object as argument
If the player decides to quit the game, his or her final
score is output to screen.
********************************************************** */
void quit(const Coin coinToss)
{
cout << "QUITTERS, CHEATERS! But fine ... go ahead, go ... ";
coinToss.getNumWins() > coinToss.getNumLosses() ?
cout << "you are the winner, $"
<< (coinToss.getNumWins() - coinToss.getNumLosses())
<< " are now thine!" :
cout << "\nbut before you leave, pay the $"
<< (coinToss.getNumLosses() - coinToss.getNumWins())
<< " you still owe!";
}
No comments:
Post a Comment