Created
March 25, 2023 09:11
-
-
Save 0x4248/3b5994b526fbb58a21be0a8bdc54d963 to your computer and use it in GitHub Desktop.
This is a simple lottery simulator that will simulate a lottery game.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Lottery Simulator | |
* This is a simple lottery simulator that will simulate a lottery game. | |
*/ | |
#include <iostream> | |
#include <cstdlib> | |
#include <ctime> | |
#include <vector> | |
#define DRAW_SIZE 5 | |
#define TICKET_PRICE 2 | |
#define MINOR_PRIZE 5 | |
#define MAJOR_PRIZE 100 | |
#define JACKPOT 10000 | |
#define MIN_NUMBER 1 | |
#define MAX_NUMBER 30 | |
int numbers_correct(std::vector <int> winningNumbers, std::vector <int> lotteryNumbers){ | |
int correct = 0; | |
for (int i = 0; i < winningNumbers.size(); i++){ | |
for (int j = 0; j < lotteryNumbers.size(); j++){ | |
if (winningNumbers[i] == lotteryNumbers[j]){ | |
correct++; | |
} | |
} | |
} | |
return correct; | |
} | |
int calculate_winnings_money(int correct){ | |
if (correct > DRAW_SIZE / 2){ | |
return MAJOR_PRIZE; | |
} else if (correct > 0){ | |
return MINOR_PRIZE; | |
} else { | |
return 0; | |
} | |
} | |
int calculate_winnings(std::vector <int> winningNumbers, std::vector <int> lotteryNumbers){ | |
int correct = numbers_correct(winningNumbers, lotteryNumbers); | |
return calculate_winnings_money(correct); | |
} | |
void print_win(std::vector <int> lotteryNumbers, int attempts, int money){ | |
std::cout << "You won! The winning numbers are: "; | |
for (int i = 0; i < lotteryNumbers.size(); i++){ | |
std::cout << "(" << lotteryNumbers[i] << ") "; | |
} | |
std::cout << std::endl; | |
std::cout << "It took " << attempts << " attempts to win." << std::endl; | |
std::cout << "You have £" << money << " " << std::endl; | |
} | |
int main(){ | |
std::srand(std::time(nullptr)); | |
std::vector<int> lotteryNumbers; | |
for (int i = 0; i < DRAW_SIZE; i++){ | |
lotteryNumbers.push_back(std::rand() % MAX_NUMBER + MIN_NUMBER); | |
} | |
int money = 2; | |
int attempts = 0; | |
while (true){ | |
money -= TICKET_PRICE; | |
std::vector<int> winningNumbers; | |
for (int i = 0; i < DRAW_SIZE; i++){ | |
winningNumbers.push_back(std::rand() % MAX_NUMBER + MIN_NUMBER); | |
} | |
if (lotteryNumbers == winningNumbers){ | |
money += JACKPOT; | |
print_win(winningNumbers, attempts, money); | |
break; | |
} else { | |
money += calculate_winnings(winningNumbers, lotteryNumbers); | |
} | |
attempts++; | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment