#include <cstdlib>
#include <iostream>
#include <string>
#include <cmath>
#include <iomanip>
int spin(int,int, int);
void playGame();
float insertMoney(float);
int spin(int, int, int);
int showSpin(int, int, int);
int showSymbol(int);
int getMatches(int, int, int);
float calcWinnings(int, float);
char wantToPlayAgain(char);
using namespace std;
const int CHERRIES = 1;
const int ORANGES = 2;
const int PLUMS = 3;
const int BELLS = 4;
const int MELONS = 5;
const int BARS = 6;
int main()
{
// The again variable is used to determine whether the
// user wants to continue playing the game.
char again;
float money;
// Start playing
do{
// Pressing a key spins the machine.
cout << "Press Enter to spin
" << endl;
cin.get();
// Simulate the spin.
playGame();
// Does the user want to play again?
wantToPlayAgain(again);
}while (wantToPlayAgain(again) == 'Y');
system("PAUSE");
return EXIT_SUCCESS;
}
void playGame()
{
int symbol1, symbol2, symbol3, matches;
float money, winnings;
money = 0;
money = insertMoney(money);
spin(symbol1,symbol2,symbol3);
showSpin(symbol1,symbol2,symbol3);
matches = getMatches(symbol1,symbol2,symbol3);
winnings = calcWinnings(matches, money);
cout << "You Won $" << setprecision(2) << fixed << winnings << endl;
}
float insertMoney(float money)
{
cout << "How much money do you want to insert?";
cin >> money;
return money;
}
int spin(int symbol1, int symbol2, int symbol3)
{
symbol1 = rand() % BARS + CHERRIES;
symbol2 = rand() % BARS + CHERRIES;
symbol3 = rand() % BARS + CHERRIES;
}
// The showSymbol module displays a slot symbol as a word.
int showSpin(int symbol1, int symbol2, int symbol3)
{
showSymbol(symbol1);
showSymbol(symbol2);
showSymbol(symbol3);
}
// The showSymbol module displays a slot symbol as a word.
int showSymbol(int symbol)
{
switch (symbol)
{
case 1:
cout << "Cherries\n";
break;
case 2:
cout << "Oranges\n";
break;
case 3:
cout << "Plums\n";
break;
case 4:
cout << "Bells\n";
break;
case 5:
cout << "Melons\n";
break;
case 6:
cout << "Bars\n";
break;
}
}
// The getMatches function accepts arguments for the three
// slot symbols and returns the number of them that match.
int getMatches(int symbol1, int symbol2, int symbol3)
{
int matches;
// Do all three symbols match?
if (symbol1 == symbol2 && symbol1 == symbol3)
{
matches = 3;
}
// Else, do two of the symbols match?
else if (symbol1 == symbol2 || symbol1 == symbol3 || symbol2 == symbol3)
matches = 2;
// Otherwise, there are no matches.
else {
matches = 0;
}
// Return the number of matches.
return matches;
}
// The calcWinnings function accepts the number of matches and
// the amount of money inserted as arguments, and returns the
// amount of money won for this spin.
float calcWinnings(int matches, float money)
{
return matches * money;
}
// The wantToPlayAgain module asks the user if he or she wants to
// play again and stores the input as "Y" or "N" in the reference
// parameter.
char wantToPlayAgain(char again)
{
// Ask the user
cout << "Do you want to play again?\n";
cin >> again;
// Validate the input.
while (again != 'Y' && again != 'N')
{
cout << "Enter either Y or N.\n";
cin >> again;
}
}