Project 1: Powerball! Recently, we experienced national hysteria over the Powerb
ID: 3677565 • Letter: P
Question
Project 1: Powerball!
Recently, we experienced national hysteria over the Powerball lottery. On January 13, 2016, after 20 consecutive unsuccessful drawings, the Powerball lottery had a jackpot of $1.5 billion. Even though the odds of winning the top prize (1 in 292.2 million) were worst than getting into a fatal car crash on the way to purchasing a ticket, the large jackpot brought out many players to try their luck. If you are unfamiliar with Powerball, please review the rules of the game.
The purpose this assignment is to remind you how classes and objects work. This will be a fundamental part of successfully completing CS 20A. Please use this assignment to jog your memory about how all this stuff works.
Your assignment is to produce a two classes that work together to simulate a Powerball lottery. In an effort to help you, the design of these two classes will be discussed here. In addition, some sample code has been provided to assist you with this task. Various UML diagrams are shown below to communicate the code you need to create. Please follow the steps outlined below and don't jump ahead until you have finished the earlier step.
First, you will create the class PowerballTicket. This class represents a player's ticket and stores the values of 5 balls plus the powerball. Please review the class diagram shown here:
As designed, the class PowerballTicket store 6 different ints and provides accessor methods for each one. Create a .h and .cpp for this class. Write some sample driver code and assertions to verify that your accessor methods are all working properly.
Next, acquire the RandomNumber class. This is code I am providing to you to complete this programming project. Click on the links for the .h and .cpp file for the RandomNumber class. Add it to your solution or project. This class provides you with a way to acquire a random int value within a specified range. As described in the rules of the game, balls 1-5 should each come a value between 1 to 69 and the powerball should come from a value between 1 to 26. Balls 1-5 must each be different but the powerball is allowed to match the value of the first five balls, if that should occur. Please review the class diagram shown here:
As described in the rules of the game, each ticket may potentially win a prize. You need to create an enumeration called WinningPossibility that will list the different possible winning outcomes. This enumeration will need to be a public part of the class PowerballLottery which I am about to explain next. Please review the enumeration shown here:
The second class you will create is called PowerballLottery. This class represents the winning combination for one particular Powerball game. Similar to a PowerballTicket, the PowerballLottery stores the values of 5 balls plus the powerball. Please review the class diagram shown here:
When the constructor is called with no arguments, use the RandomNumber class to generate all the ball values. When the constructor is called with 6 ints, use those values for the balls. The WinningPossibility enumeration needs to be apublic part of the PowerballLottery class. Create a .h and .cpp for this class. Write some sample driver code and assertions to verify that your accessor methods are all working properly.
Once you get to this point, there are three remaining class methods to implement. Each are described below.
1. PowerballTicket PowerballLottery::quickPick( );
This method should return a PowerballTicket correctly filled with random ball values for the driver code to inspect as diagrammed below.
2. PowerballLottery::WinningPossibility PowerballLottery::checkTicket( PowerballTicket ticket );
This method should check the values found in the ticket parameter against the lottery's values and return the WinningPossibility represented by this ticket. (Because of how hard it is to win, much of the time, your code will report NOTWINNING, unless you cheat..)
3. void PowerballLottery::printWhatHappened( PowerballTicket ticket );
This method should check the values found in the ticket parameter against the lottery's values and print to cout what happened so you can textually read whether you won or not. Yes, I am expecting this method to call the checkTicket method you created in Step 2. You should print one of the following messages, depending on the ticket passed:
You are free to create additional public and private methods and data members as you see fit.
Driver Code
PowerballTicket mBall1 : int -mBall2: int -mBall3: int -mBall4 int -mBall5: int -mPowerball int +PowerballTicket(ball1: int, ball2: int, ball3: int, ball4 : int, ball5: int, powerball: int) +getBall1) int +getBall2) int +getBall3) int +getBall4): int +getBall5) int +getPowerball): intExplanation / Answer
game.cpp
#include <iostream>
#include <string>
#include <cassert>
#include "PowerballLottery.h"
#include "PowerballTicket.h"
#include "RandomNumber.h"
using namespace std;
int main(){
// test code
PowerballTicket ticket( 1, 2, 3, 4, 5, 6 );
assert( ticket.getBall1() == 1);
assert( ticket.getBall2() == 2);
assert( ticket.getBall3() == 3);
assert( ticket.getBall4() == 4);
assert( ticket.getBall5() == 5);
assert( ticket.getPowerball() == 6);
PowerballLottery lottery( 1, 2, 3, 4, 5, 6 );
assert( lottery.getBall1() == 1);
assert( lottery.getBall2() == 2);
assert( lottery.getBall3() == 3);
assert( lottery.getBall4() == 4);
assert( lottery.getBall5() == 5);
assert( lottery.getPowerball() == 6);
assert( lottery.checkTicket(ticket) == PowerballLottery::WinningPossibility::FIVEPLUSPOWERBALL);
ticket = PowerballTicket( 1, 2, 3, 4, 5, 12 );
assert( lottery.checkTicket(ticket) == PowerballLottery::WinningPossibility::FIVE);
ticket = PowerballTicket( 1, 2, 3, 4, 15, 12 );
PowerballLottery lottery2(1, 4, 3, 25, 20, 19);
assert(lottery2.checkTicket(ticket) == PowerballLottery::WinningPossibility::THREE);
PowerballTicket quickPickTicket( 1, 2, 3, 4, 5, 6);
for (int i = 0; i < 20; i++)
{
quickPickTicket = lottery.quickPick();
// all the ball numbers need to be different...
assert(quickPickTicket.getBall1() != quickPickTicket.getBall2() &&
quickPickTicket.getBall1() != quickPickTicket.getBall3() &&
quickPickTicket.getBall1() != quickPickTicket.getBall4() &&
quickPickTicket.getBall1() != quickPickTicket.getBall5() &&
quickPickTicket.getBall2() != quickPickTicket.getBall3() &&
quickPickTicket.getBall2() != quickPickTicket.getBall4() &&
quickPickTicket.getBall2() != quickPickTicket.getBall5() &&
quickPickTicket.getBall3() != quickPickTicket.getBall4() &&
quickPickTicket.getBall3() != quickPickTicket.getBall5() &&
quickPickTicket.getBall4() != quickPickTicket.getBall5());
}
cout << "all tests passed!" << endl;
return 0;
}
PowerballLottery.cpp
#include <iostream>
#include "PowerballLottery.h"
#include "PowerballTicket.h"
#include "RandomNumber.h"
using namespace std;
//create random number for regular balls
RandomNumber a(1, 69, true, true);
//create random number for powerballs
RandomNumber b(1, 26, true, true);
//make sure no numbers repeat for the lottery numbers
PowerballLottery::PowerballLottery() {
mBall1 = a.random();
do {
mBall2 = a.random();
} while (mBall2 == mBall1);
do {
mBall3 = a.random();
} while (mBall3 == mBall2 || mBall3 == mBall1);
do {
mBall4 = a.random();
} while (mBall4 == mBall1 || mBall4 == mBall2 || mBall4 == mBall3);
do {
mBall5 = a.random();
} while (mBall5 == mBall1 || mBall5 == mBall2 || mBall5 == mBall3 || mBall5 == mBall4);
mPowerball = b.random();
}
//constructor for given lottery ticket numbers
PowerballLottery::PowerballLottery(int ball1, int ball2, int ball3, int ball4, int ball5, int powerball){
mBall1 = ball1;
mBall2 = ball2;
mBall3 = ball3;
mBall4 = ball4;
mBall5 = ball5;
mPowerball = powerball;
}
//functions to return values of lottery balls
int PowerballLottery::getBall1(){
return mBall1;
}
int PowerballLottery::getBall2(){
return mBall2;
}
int PowerballLottery::getBall3(){
return mBall3;
}
int PowerballLottery::getBall4(){
return mBall4;
}
int PowerballLottery::getBall5(){
return mBall5;
}
int PowerballLottery::getPowerball(){
return mPowerball;
}
//pick a make a lottery ticket with random numbers
PowerballTicket PowerballLottery::quickPick(){
int b1, b2, b3, b4, b5, pb;
b1 = a.random();
do {
b2 = a.random();
} while (b2 == b1);
do {
b3 = a.random();
} while (b3 == b2 || b3 == b1);
do {
b4 = a.random();
} while (b4 == b1 || b4 == b2 || b4 == b3);
do {
b5 = a.random();
} while (b5 == b1 || b5 == b2 || b5 == b3 || b5 == b4);
pb = b.random();
PowerballTicket ticket(b1, b2, b3, b4, b5, pb);
return ticket;
}
PowerballLottery::WinningPossibility PowerballLottery::checkTicket(PowerballTicket ticket){
int x = 0;
bool b = 0;;
int numbers[5] = { mBall1, mBall2, mBall3, mBall4, mBall5};
for (int i = 0; i < 5; i++) {
if (ticket.getBall1() == numbers[i] ) {
x++;
}
if (ticket.getBall2() == numbers[i]) {
x++;
}
if (ticket.getBall3() == numbers[i]) {
x++;
}
if (ticket.getBall4() == numbers[i]) {
x++;
}
if (ticket.getBall5() == numbers[i]) {
x++;
}
}
b = (ticket.getPowerball() == mPowerball);
if(x == 0){
if(b){
return POWERBALL;
}
return NOTWINNING;
}
if(x ==1){
if(b){
return ONEPLUSPOWERBALL;
}
return NOTWINNING;
}
if(x == 2){
if(b){
return TWOPLUSPOWERBALL;
}
return NOTWINNING;
}
if(b){
if (x == 3) {
return THREEPLUSPOWERBALL;
}
if(x == 4) {
return FOURPLUSPOWERBALL;
}
else{
return FIVEPLUSPOWERBALL;
}
}
if(!b){
if (x == 3) {
return THREE;
}
if(x == 4) {
return FOUR;
}
else{
return FIVE;
}
}
return NOTWINNING;
}
void PowerballLottery::printWhatHappened(PowerballTicket ticket){
if(checkTicket(ticket) == NOTWINNING){
cout << "You didn't win anything. Congratulations!" << endl;
}
if(checkTicket(ticket) == POWERBALL){
cout << "You mathced just the powerball. Congratulations!" << endl;
}
if(checkTicket(ticket) == ONEPLUSPOWERBALL){
cout << "You matched one ball plus the powerball. Congrattluations!" <<endl;
}
if(checkTicket(ticket) == TWOPLUSPOWERBALL){
cout << "You matched two balls plus the powerball. Congratulations!" << endl;
}
if(checkTicket(ticket) == THREEPLUSPOWERBALL){
cout << "You matched three balls plus the powerball. Congratulations!" << endl;
}
if(checkTicket(ticket) == THREE){
cout << "You matched three balls but not the powerball. Congratulations!" << endl;
}
if(checkTicket(ticket) == FOUR){
cout << "You matched four balls but not the powerball. Congratulations!" << endl;
}
if(checkTicket(ticket) == FOURPLUSPOWERBALL){
cout << "You matched four balls plus the powerball. Congratulations!" << endl;
}
if(checkTicket(ticket) == FIVE){
cout << "You matched five balls but not the powerball. Congratulations!" << endl;
}
if(checkTicket(ticket) == FIVEPLUSPOWERBALL){
cout << "You won the jackpot - all balls plus the powerball. Congratulations!" << endl;
}
}
PowerballLottery.h
#ifndef PowerballLottery_h
#define PowerballLottery_h
#include "PowerballTicket.h"
#include "RandomNumber.h"
class PowerballLottery{
public:
enum WinningPossibility{POWERBALL, ONEPLUSPOWERBALL, TWOPLUSPOWERBALL, THREEPLUSPOWERBALL, FOURPLUSPOWERBALL, FIVEPLUSPOWERBALL, THREE, FOUR, FIVE, NOTWINNING};
PowerballLottery();
PowerballLottery(int ball1, int ball2, int ball3, int ball4, int ball5, int powerball);
int getBall1();
int getBall2();
int getBall3();
int getBall4();
int getBall5();
int getPowerball();
PowerballTicket quickPick();
WinningPossibility checkTicket(PowerballTicket ticket);
void printWhatHappened(PowerballTicket ticket);
private:
int mBall1, mBall2, mBall3, mBall4, mBall5, mPowerball;
};
#endif /* PowerballLottery_h */
PowerballTicket.cpp
#include "PowerballTicket.h"
#include <iostream>
using namespace std;
//constructor that accepts given values for powerballticket
PowerballTicket::PowerballTicket(int ball1, int ball2, int ball3, int ball4, int ball5, int powerball){
mBall1 = ball1;
mBall2 = ball2;
mBall3 = ball3;
mBall4 = ball4;
mBall5 = ball5;
mPowerball = powerball;
}
//functions that return values for each ticket number
int PowerballTicket::getBall1(){
return mBall1;
}
int PowerballTicket::getBall2(){
return mBall2;
}
int PowerballTicket::getBall3(){
return mBall3;
}
int PowerballTicket::getBall4(){
return mBall4;
}
int PowerballTicket::getBall5(){
return mBall5;
}
int PowerballTicket::getPowerball(){
return mPowerball;
}
PowerballTicket.h
#ifndef PowerballTicket_h
#define PowerballTicket_h
#include <string>
using namespace std;
class PowerballTicket
{
//initialize constructor and methods
public:
PowerballTicket(int ball1, int ball2, int bal3, int ball4, int ball5, int powerball);
int getBall1();
int getBall2();
int getBall3();
int getBall4();
int getBall5();
int getPowerball();
//create values for each ticket number
private:
int mBall1;
int mBall2;
int mBall3;
int mBall4;
int mBall5;
int mPowerball;
};
#endif /* PowerballTicket_h */
RandomNumber.cpp
#include "RandomNumber.h"
#include <random>
#include <utility>
using namespace std;
RandomNumber::RandomNumber( int min, int max,
bool minInclusive, bool maxInclusive )
: mMinimum( min ), mMaximum( max )
{
if (mMinimum > mMaximum)
{
swap( mMinimum, mMaximum );
}
if (!minInclusive)
{
mMinimum++;
}
if (!maxInclusive)
{
mMaximum--;
}
}
int RandomNumber::random( )
{
static random_device rd;
static mt19937 generator(rd());
uniform_int_distribution<> distro( mMinimum, mMaximum );
return( distro( generator ) );
}
RandomNumber.h
#ifndef RANDOMNUMBER_H
#define RANDOMNUMBER_H
class RandomNumber
{
public:
RandomNumber( int min, int max, bool minInclusive=true, bool maxInclusive=true );
// supply a number between min and max inclusive
int random( );
private:
int mMinimum, mMaximum;
};
#endif
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.