Skip to content

Instantly share code, notes, and snippets.

@keir-nellyer
Created January 19, 2016 11:53
Show Gist options
  • Save keir-nellyer/605122c26642f2724f75 to your computer and use it in GitHub Desktop.
Save keir-nellyer/605122c26642f2724f75 to your computer and use it in GitHub Desktop.
#include <iostream>
#include <vector>
using namespace std;
#define NUMBER_AMOUNT 6
#define INPUT_LOWER_BOUND 1
#define INPUT_UPPER_BOUND 59
string* rewards = new string[NUMBER_AMOUNT + 1] { "Nothing.", "One Free Lucky Dip", "£25", "~£100", "~£1,000", "~£50,000", "Jackpot" };
vector<int> getUserGuesses(int amount, int lowerBound, int upperBound);
int getInteger(int lowerBound, int upperBound);
void sort(vector<int> &numbers);
void swap(int &a, int &b);
string intArrayToString(vector<int> array, string deliminator);
vector<int> generateRandomNumbers(int amount, int lowerBound, int upperBound);
int generateRandomNumber(int lowerBound, int upperBound);
vector<int> getMatches(vector<int> winningNumbers, vector<int> userNumbers);
void showMatches(vector<int> matches);
int main() {
srand((unsigned int) time(NULL)); // set random seed
// get and display user input
cout << "Please enter " << NUMBER_AMOUNT << " numbers." << endl;
vector<int> inputs = getUserGuesses(NUMBER_AMOUNT, INPUT_LOWER_BOUND, INPUT_UPPER_BOUND);
sort(inputs);
cout << "Your numbers: " << intArrayToString(inputs, ", ") << endl;
// generate and display winning numbers
vector<int> winningNumbers = generateRandomNumbers(NUMBER_AMOUNT, INPUT_LOWER_BOUND, INPUT_UPPER_BOUND);
sort(winningNumbers);
cout << "Winning numbers: " << intArrayToString(winningNumbers, ", ") << endl;
// get and show matching numbers
vector<int> matchingNumbers = getMatches(winningNumbers, inputs);
sort(matchingNumbers);
showMatches(matchingNumbers);
}
vector<int> getUserGuesses(int amount, int lowerBound, int upperBound) {
vector<int> inputs((size_t) amount);
for (int i = 0; i < amount; i++) {
inputs[i] = getInteger(lowerBound, upperBound);
}
return inputs;
}
int getInteger(int lowerBound, int upperBound) {
int input;
cin >> input;
if (cin.fail()) {
cout << "Not an integer." << endl;
input = getInteger(lowerBound, upperBound);
}
if (input < lowerBound || input > upperBound) {
cout << "Please enter a number in the range " << lowerBound << "-" << upperBound << endl;
input = getInteger(lowerBound, upperBound);
}
return input;
}
void sort(vector<int> &numbers) {
long size = numbers.size();
for (int count1 = 0; count1 < size; count1++) {
for (int count2 = 0; count2 < size; count2++) {
int nextIndex = count2 + 1;
if (nextIndex < size && numbers[count2] > numbers[nextIndex]) {
swap(numbers[count2], numbers[nextIndex]);
}
}
}
}
void swap(int &a, int &b) {
int aCopy = a;
a = b;
b = aCopy;
}
string intArrayToString(vector<int> array, string deliminator) {
string output = "";
for (int i = 0; i < array.size(); i++) {
output += to_string(array[i]);
if (i != array.size() - 1) {
output += deliminator;
}
}
return output;
}
vector<int> generateRandomNumbers(int amount, int lowerBound, int upperBound) {
vector<int> numbers((size_t) amount);
for (int i = 0; i < amount; i++) {
numbers[i] = generateRandomNumber(lowerBound, upperBound);
}
return numbers;
}
int generateRandomNumber(int lowerBound, int upperBound) {
return lowerBound + (rand() % (upperBound - lowerBound + 1));
}
vector<int> getMatches(vector<int> winningNumbers, vector<int> userNumbers) {
vector<int> matches(winningNumbers.size()); // this can only ever be as big as winningNumbers.size()
int matchesIndex = 0;
for (int i = 0; i < winningNumbers.size(); i++) {
int winningNumber = winningNumbers[i];
for (int j = 0; j < userNumbers.size(); j++) {
int userNumber = userNumbers[j];
if (userNumber == winningNumber) {
matches[matchesIndex++] = winningNumber;
}
}
}
// downsize if we didn't use the full capacity of the vector
matches.resize((size_t) matchesIndex);
return matches;
}
void showMatches(vector<int> matches) {
int matchAmount = (int) matches.size();
if (matchAmount > 0) {
cout << matches.size() << " matches (" << rewards[matchAmount] << ")" << endl;
cout << "Matching Numbers: " << intArrayToString(matches, ", ") << endl;
} else {
cout << "No matches this time, sorry!" << endl;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment