Você está na página 1de 1

// PowerballLottery.

h
// Project5
//
// Created by Howard Stahl on 1/11/16.
//

#ifndef POWERBALLLOTTERY_H
#define POWERBALLLOTTERY_H
#include "PowerballTicket.h"
class PowerballLottery
{
public:
PowerballLottery( );
PowerballLottery( int ball1, int ball2, int ball3, int ball4, int ball5, int powerb
all );
int
int
int
int
int

getBall1()
getBall2()
getBall3()
getBall4()
getBall5()

}
PowerballLottery::WinningPossibility PowerballLottery::checkTicket( Powerba
llTicket ticket )
{
WinningPossibility result = WinningPossibility::NOTWINNING;

const;
const;
const;
const;
const;

int count = countMatchingBalls( ticket );


if (ticket.getPowerball() == getPowerball())
{
switch( count )
{
case 0:
result = POWERBALL;
break;
case 1:
result = ONEPLUSPOWERBALL;
break;
case 2:
result = TWOPLUSPOWERBALL;
break;
case 3:
result = THREEPLUSPOWERBALL;
break;
case 4:
result = FOURPLUSPOWERBALL;
break;
case 5:
result = FIVEPLUSPOWERBALL;
break;
default:
break;
}
}
else
{
switch( count )
{
case 3:
result = THREE;
break;
case 4:
result = FOUR;
break;
case 5:
result = FIVE;
break;
default:
break;
}
}
return( result );

int getPowerball() const;


enum WinningPossibility { POWERBALL, ONEPLUSPOWERBALL,
TWOPLUSPOWERBALL, THREE,
THREEPLUSPOWERBALL, FOUR,
FOURPLUSPOWERBALL, FIVE,
FIVEPLUSPOWERBALL, NOTWINNING };
PowerballTicket quickPick( );
WinningPossibility checkTicket( PowerballTicket ticket );
void printWhatHappened( PowerballTicket ticket );
private:
// balls 1-5 are the possibilities 1-69 with no duplicates
// the powerball has the possibilities 1-26
int mBall1, mBall2, mBall3, mBall4, mBall5, mPowerball;
int countMatchingBalls( PowerballTicket ticket );
bool isMatched( int ball );
void generateFiveBallPlusPB( int& ball1, int& ball2, int& ball3, int&
ball4, int& ball5, int& powerball );
};

#endif

//
//
//
//
//
//
//

Powerball.cpp
Project5
Created by Howard Stahl on 1/11/16.
Copyright 2016 Howard Stahl. All rights reserved.

#include "PowerballLottery.h"
#include "RandomNumber.h"
#include <iostream>
PowerballLottery::PowerballLottery( )
{
generateFiveBallPlusPB( mBall1, mBall2, mBall3, mBall4, mBall5, mPower
ball );
}
PowerballLottery::PowerballLottery( int ball1, int ball2, int ball3, int ball4, int b
all5, int powerball )
: mBall1( ball1 ), mBall2( ball2 ), mBall3( ball3 ), mBall4( ball4 ), mBall5( ball5
), mPowerball( powerball )
{
}
int PowerballLottery::getBall1() const
{
return( mBall1 );
}
int PowerballLottery::getBall2() const
{
return( mBall2 );
}
int PowerballLottery::getBall3() const
{
return( mBall3 );
}

void PowerballLottery::generateFiveBallPlusPB( int& ball1, int& ball2, int&


ball3, int& ball4, int& ball5, int& powerball )
{
// balls 1-5 are the possibilities 1-69 with no duplicates
// the powerball has the possibilities 1-26
RandomNumber pb( 1, 26 );
RandomNumber balls( 1, 69 );
ball1 = balls.random();
ball2 = balls.random();
while (ball2 == ball1)
{
ball2 = balls.random();
}
ball3 = balls.random();
while (ball3 == ball2 || ball3 == ball1)
{
ball3 = balls.random();
}
ball4 = balls.random();
while (ball4 == ball3 || ball4 == ball2 || ball4 == ball1)
{
ball4 = balls.random();
}
ball5 = balls.random();
while (ball5 == ball4 || ball5 == ball3 || ball5 == ball2 || ball5 == ball1)
{
ball5 = balls.random();
}
powerball = pb.random();

int PowerballLottery::getBall4() const


{
return( mBall4 );
}
int PowerballLottery::getBall5() const
{
return( mBall5 );
}
int PowerballLottery::getPowerball() const
{
return( mPowerball );
}
PowerballTicket PowerballLottery::quickPick( )
{
int ball1, ball2, ball3, ball4, ball5, powerball;
generateFiveBallPlusPB( ball1, ball2, ball3, ball4, ball5, powerball );
return( PowerballTicket( ball1, ball2, ball3, ball4, ball5, powerball ) );
}
bool PowerballLottery::isMatched( int ball )
{
bool result = false;
if (ball == mBall1 || ball == mBall2 || ball == mBall3 || ball == mBall4 || ball
== mBall5)
{
result = true;
}
return( result );
}
int PowerballLottery::countMatchingBalls( PowerballTicket ticket )
{
int result = 0;
if (isMatched(ticket.getBall1()))
{
result++;
}
if (isMatched(ticket.getBall2()))
{
result++;
}
if (isMatched(ticket.getBall3()))

cout << endl;


break;
case PowerballLottery::FIVE:
case PowerballLottery::FIVEPLUSPOWERBALL:
cout << "You matched 5 balls";
if (possibility == PowerballLottery::FIVEPLUSPOWERBALL)
{
cout << " plus the powerball!";
}
cout << endl;
break;
case PowerballLottery::NOTWINNING:
cout << "You didn't win anything at all!" << endl;
break;
default:
break;

result++;
}
if (isMatched(ticket.getBall4()))
{
result++;
}
if (isMatched(ticket.getBall5()))
{
result++;
}
return( result );

}
void PowerballLottery::printWhatHappened( PowerballTicket ticket )
{
using namespace std;
WinningPossibility possibility = checkTicket( ticket );
switch( possibility )
{
case PowerballLottery::POWERBALL:
cout << "You matched the powerball!" << endl;
break;
case PowerballLottery::ONEPLUSPOWERBALL:
cout << "You matched 1 ball plus the powerball!" << endl;
break;
case PowerballLottery::TWOPLUSPOWERBALL:
cout << "You matched 2 balls plus the powerball!" << endl;
break;
case PowerballLottery::THREE:
case PowerballLottery::THREEPLUSPOWERBALL:
cout << "You matched 3 balls";
if (possibility == PowerballLottery::THREEPLUSPOWERBALL)
{
cout << " plus the powerball!";
}
cout << endl;
break;
case PowerballLottery::FOUR:
case PowerballLottery::FOURPLUSPOWERBALL:
cout << "You matched 4 balls";
if (possibility == PowerballLottery::FOURPLUSPOWERBALL)
{
cout << " plus the powerball!";
}

Você também pode gostar