Question: Hi here is a C++ code, I have finished that, but can someone help me to simplify my code or maybe write in another way?

Hi here is a C++ code, I have finished that, but can someone help me to simplify my code or maybe write in another way? For the .h part I think I don't need any changes there. But for the PowerballLottery.cpp part I think that can be simplified. Also, for the game.cpp, can you help me to provide some more test cases there? Really appreciate!

so here are my code.

PowerballTicket.h

#ifndef POWERBALLTICKET_H #define POWERBALLTICKET_H

class PowerballTicket { public: PowerballTicket(int ball1, int ball2, int ball3, int ball4, int ball5, int powerball); int getBall1(); int getBall2(); int getBall3(); int getBall4(); int getBall5(); int getPowerball(); private: int mBall1; int mBall2; int mBall3; int mBall4; int mBall5; int mPowerball; };

#endif

PowerballTicket.cpp

#include "PowerballTicket.h"

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; }

int PowerballTicket::getBall1() //get method { 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; }

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

RandomNumber.cpp

#include "RandomNumber.h"

#include #include

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)); }

PowerballLottery.h

#ifndef POWERBALLLOTTERY_H #define POWERBALLLOTTERY_H #include"PowerballTicket.h"

class PowerballLottery { public: enum WinningPossibility {POWERBALL, ONEPLUSPOWERBALL,TWOPLUSPOWERBALL, THREE, THREEPLUSPOWERBALL,FOUR, FOURPLUSPOWERBALL, FIVE, FIVEPLUSPOWERBALL, 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; int mBall2; int mBall3; int mBall4; int mBall5; int mPowerball; }; #endif

PowerballLottery.cpp

#include"PowerballLottery.h" #include "RandomNumber.h" #include using namespace std;

PowerballLottery::PowerballLottery() { RandomNumber r(1, 69); mBall1 = r.random(); do { mBall2 = r.random(); } while (mBall2 == mBall1); do { mBall3 = r.random(); } while (mBall3 == mBall1 || mBall3 == mBall2); do { mBall4 = r.random(); } while (mBall4 == mBall1 || mBall4 == mBall2 || mBall4 == mBall3); do { mBall5 = r.random(); } while (mBall5 == mBall4 || mBall5 == mBall3 || mBall5 == mBall2 || mBall5 == mBall1); RandomNumber d(1, 26); mPowerball = d.random();

} 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; }

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; }

PowerballTicket PowerballLottery::quickPick() { RandomNumber r(1, 69); int first, second, third, forth, fifth, powerball; first = r.random(); do { second = r.random(); } while (second == first); do { third = r.random(); } while (third == first || third == second); do { forth = r.random(); } while (forth == first || forth == second || forth == third); do { fifth = r.random(); } while (fifth == forth || fifth == third || fifth == second || fifth == first); RandomNumber d(1, 26); powerball = d.random(); PowerballTicket t(first, second, third, forth, fifth, powerball); return t; }

PowerballLottery::WinningPossibility PowerballLottery::checkTicket (PowerballTicket ticket) { int win(0); bool p = false; int d = ticket.getBall1(); if (d == mBall1 || d == mBall2 || d == mBall3 || d == mBall4 || d == mBall5) { win++; } d = ticket.getBall2(); if (d == mBall1 || d == mBall2 || d == mBall3 || d == mBall4 || d == mBall5) { win++; } d = ticket.getBall3(); if (d == mBall1 || d == mBall2 || d == mBall3 || d == mBall4 || d == mBall5) { win++; } d = ticket.getBall4(); if (d == mBall1 || d == mBall2 || d == mBall3 || d == mBall4 || d == mBall5) { win++; } d = ticket.getBall5(); if (d == mBall1 || d == mBall2 || d == mBall3 || d == mBall4 || d == mBall5) { win++; } if (ticket.getPowerball() == mPowerball) { switch (win) { case 1: return ONEPLUSPOWERBALL; break; case 2: return TWOPLUSPOWERBALL; break; case 3: return THREEPLUSPOWERBALL; break; case 4: return FOURPLUSPOWERBALL; break; case 5: return FIVEPLUSPOWERBALL; break; default: return POWERBALL; break; } } else { switch (win) { case 3: return THREE; break; case 4: return FOUR; break; case 5: return FIVE; break; default: return NOTWINNING; break; } }

}

void PowerballLottery::printWhatHappened(PowerballTicket ticket) { switch (checkTicket(ticket)) { case POWERBALL: cout << "You matched just the powerball. Congratulations!" << endl; break; case ONEPLUSPOWERBALL: cout << "You matched one ball plus the powerball. Congratulations!" << endl; break; case TWOPLUSPOWERBALL: cout << "You matched two balls plus the powerball. Congratulations!" << endl; break; case THREE: cout << "You matched three balls but not the powerball. Congratulations!" << endl; break; case THREEPLUSPOWERBALL: cout << "You matched three balls plus the powerball. Congratulations!" << endl; break; case FOUR: cout << "You matched four balls but not the powerball. Congratulations!" << endl; break; case FOURPLUSPOWERBALL: cout << "You matched four balls plus the powerball. Congratulations!" << endl; break; case FIVE: cout << "You matched five balls but not the powerball. Congratulations!" << endl; break; case FIVEPLUSPOWERBALL: cout << "You won the jackpot - all balls plus the powerball. Congratulations!" << endl; break; default: cout << "You didn't win anything. Please buy another ticket!" << endl; break; } }

game.cpp

#include #include "PowerballTicket.h" #include"PowerballLottery.h"

#include #include

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);

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;

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!