Code:
#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
#include <cstdlib>
#include <conio.h>
#include <ctime>
#include "Powerball.h"
#include <array>
#include <algorithm>
#include <functional>
using namespace std;
char username;
const short LOTTO_SIZE = 6; // Size of arrays for tracking picks
// Output shown when the player wins
const char* const winTxt[] = {
"Winner! Jackpot! - $23,000,000",
"Winner! Five white balls - $1,000,000",
"Winner! Four white balls & powerball - $10,000!",
"Winner! Four white balls - $100!",
"Winner! Three white balls & powerball - $100!",
"Winner! Three white balls - $7!",
"Winner! Two white balls & powerball - $7!",
"Winner! One white ball & powerball - $4!",
"Winner! Powerball only - $4!"};
void header()
{
cout << "C++\n"
<< "Powerball: Program 1\n\n";
cout << "You will be asked to pick a total of six numbers.\n"
<< "The first five numbers must be a value between 1-55.\n"
<< "The last number, the powerball, must be\n"
<< "between 1-42, and may repeat a previously picked value.\n"
<< "All picks must be whole number values.\n\n"
<< "=============================================================\n"
<< "There are nine possible winning combinations:\n"
<< "Five matches + powerball $23,000,000 Odds 1 in 175,223,510\n"
<< "Five matches $1,000,000 Odds 1 in 153,632.65\n"
<< "Four matches + powerball $10,000 Odds 1 in 648,975.96\n"
<< "Four matches $100 Odds 1 in 19,078.53\n"
<< "Three matches + powerball $100 Odds 1 in 12,244.83\n"
<< "Three matches $7 Odds 1 in 360.14\n"
<< "Two matches + powerball $7 Odds 1 in 706.43\n"
<< "One match + powerball $4 Odds 1 in 110.81\n"
<< "Powerball only $4 Odds 1 in 55.41\n"
<< "=============================================================\n\n";
}
struct pbSet
{
float alone;
float match1;
float match2;
float match3;
float match4;
float match5;
};
// Used to keep tally of wins if playing until jackpot
struct winCount
{
pbSet powerball;
float match3;
float match4;
float match5;
} winnings = {0, 0, 0, 0, 0, 0}; // Counter to track the winnings
int menu()
{
short temp = 0;
string username;
cout << "Enter your name: ";
cin >> username;
cout << endl
<< "1. Play\n";
cout << username + ", pick an menu option: ";
cin >> temp;
cout << endl;
if (!cin)
{
cin.clear();
cin.ignore();
}
else
return temp;
}
void game(const bool& type)
{
short *playerPicks, // numbers chosen by the player
*randPicks; // random numbers (winning picks)
float counter = 1.0f; // counter of number of tries
bool win = false; // tracks win condition
playerPicks = makePicks(LOTTO_SIZE);
cout << endl
<< "You've chosen: ";
for (short i = 0; i < LOTTO_SIZE; i++)
cout << playerPicks[i] << " ";
_getch();
{
while (!win)
{
randPicks = makePicksRand(LOTTO_SIZE);
cout << "\nTry " << counter << ": ";
for (short i = 0; i < LOTTO_SIZE; i++)
cout << randPicks[i] << " ";
cout << endl;
win = checkWin(playerPicks, randPicks, type);
counter++;
delete[] randPicks;
}
}
}
short* makePicks(const short& size)
{
short *temp = new short[size];
bool repeat = false;
cout << "Pick your first five numbers.\n"
<< "Choices must be from 1-55, and may not repeat\n";
for (short i = 0; i < LOTTO_SIZE;)
{
if ((i == 5) && (!repeat))
{
cout << "Now, pick your powerball number.\n"
<< "Choice must be from 1-42, and may\n"
<< "repeat any previous pick.\n";
repeat = true;
}
cout << "Pick " << (i + 1) << ": ";
cin >> temp[i];
if (!cin)
{
cin.clear();
cin.ignore();
cout << "Invalid input.\n";
}
else
{
if (validate(i, temp))
i++;
else
cout << "Pick " << (i + 1) << " conflicts with a previous \n"
<< "choice or is invalid.\n";
}
}
return temp;
}
short* makePicksRand(const short& size)
{
short *temp = new short[size];
for (short i = 0; i < LOTTO_SIZE;)
{
if (i == 5)
temp[i] = (rand() % 42) + 1;
else
temp[i] = (rand() % 55) + 1;
if (validate(i, temp))
i++;
}
return temp;
}
bool checkArray(int array[5], int size, int number)
{
bool has = false;
for (int i = 0; i< size; i++)
{
if (array[i] == number)
{
has = true;
}
}
return has;
}
bool validate(const short& num, const short* picks)
{
if (num == 5) // when checking the last number (powerball)
{
if ((picks[num] < 1) || (picks[num] > 42))
return false;
else
return true;
}
else // checks all other numbers
{
if ((picks[num] > 55) || (picks[num] < 1))
return false;
else if (num > 0)
for (short i = 0; i <= num; i++)
if (picks[i] == picks[i + 1])
return false;
return true;
}
}
bool checkWin(const short* player, const short* random, const bool& type)
{
bool pbMatch = false;
short matches = 0;
for (short i = 0; i < LOTTO_SIZE; i++)
{
if (player[i] == random[i])
{
if (i == 5)
pbMatch = true;
else
matches++;
}
}
if (pbMatch)
switch (matches)
{
case 0: // $4
cout << winTxt[8] << endl;
if (type)
{
winnings.powerball.alone++;
return false;
}
else
return true;
break;
case 1: // $4
cout << winTxt[7] << endl;
if (type)
{
winnings.powerball.match1++;
return false;
}
else
return true;
break;
case 2: // $7
cout << winTxt[6] << endl;
if (type)
{
winnings.powerball.match2++;
return false;
}
else
return true;
break;
case 3: // $100
cout << winTxt[4] << endl;
if (type)
{
winnings.powerball.match3++;
return false;
}
else
return true;
break;
case 4: // $100,000
cout << winTxt[2] << endl;
if (type)
{
winnings.powerball.match4++;
return false;
}
else
return true;
break;
case 5: // jackpot
cout << winTxt[0] << endl;
return true;
}
else
switch (matches)
{
case 3: // $7
cout << winTxt[5] << endl;
if (type)
{
winnings.match3++;
return false;
}
else
return true;
break;
case 4: // $100
cout << winTxt[3] << endl;
if (type)
{
winnings.match4++;
return false;
}
else
return true;
break;
case 5: // $1,000,000
cout << winTxt[1] << endl;
if (type)
{
winnings.match5++;
return false;
}
else
return true;
break;
}
return false;
}