1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166
|
#include <iostream>
#include <string>
using namespace std;
void swap(int xp, int yp)
{
int temp = xp;
xp = yp;
yp = temp;
}
void selectionSort(int *arr, int n)
{
int i, j, min_idx;
for (i = 0; i < n - 1; i++)
{
// Find the minimum element in unsorted array
min_idx = i;
for (j = i + 1; j < n; j++)
if ((arr + j) < (arr + min_idx))
min_idx = j;
// Swap the found minimum element with the first element
swap(&arr[min_idx], &arr[i]); //ERROR ON THIS LINEEE
}
}
int generateRandomNumber(int min, int max) {
return rand() % (max - min + 1) + min;
}
int *generateRandomArray(int min, int max) {
static int randomNumbers[5];
for (int i = 0; i < 5; i++) {
randomNumbers[i] = generateRandomNumber(min, max);
}
return randomNumbers;
}
bool isValidUserInput(int num, int start, int end) {
if (num >= start && num <= end) {
return true;
}
return false;
}
int * getWhiteBallInputFromUser() {
static int userInput[5];
int i = 0;
int input;
while (i < 5) {
std::cout << "Enter number " << i + 1 << " (between 1 and 69): ";
std::cin >> input;
if (isValidUserInput(input, 1, 69)) {
userInput[i] = input;
i++;
}
}
return userInput;
}
int * getWhiteBallInput(string gameMode, int inputStart, int inputEnd) {
static int *userInput;
if (gameMode.compare("Y") == 0 || gameMode.compare("y") == 0) {
userInput = getWhiteBallInputFromUser();
}
else {
userInput = generateRandomArray(inputStart, inputEnd);
}
selectionSort(userInput, 5);
return userInput;
}
int getRedBallInput(string gameMode) {
if (gameMode.compare("Y") == 0 || gameMode.compare("y") == 0) {
int i = 0;
int input;
while (true) {
std::cout << "Enter POWERBALL number " << i + 1 << " (between 1 and 26): ";
std::cin >> input;
if (isValidUserInput(input, 1, 26)) {
return input;
}
}
}
else {
return generateRandomNumber(1, 26);
}
}
/* here implement any logic required */
string getResult(int inputNumbers[], int winningNumbers[]) {
int result = 0;
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
if (inputNumbers[i] == winningNumbers[j]) {
result++;
break;
}
}
}
if (winningNumbers[5] == inputNumbers[5]) {
result += 5;
}
return std::to_string(result) + ".00";
}
int main() {
// you can change this according to you
int winningNumberSet[6] = { 2,10,12,15,30,22 };
string gamePlayChoice;
cout << "Number Guessing Game\n";
cout << "--------------------\n\n";
cout << "1. Select FIVE numbers from 1 to 69 for the white balls.\n";
cout << "2. Select ONE number from 1 to 26 for red powerball.\n";
cout << "3. Prize is determined by how of your numbers match the winning number.\n\n";
while (true) {
cout << "Do you want to self pick your white ball numbers(Y or N): ";
getline(cin, gamePlayChoice);
if (gamePlayChoice.compare("Y") == 0 || gamePlayChoice.compare("N") == 0 || gamePlayChoice.compare("n") == 0 || gamePlayChoice.compare("y") == 0) {
break;
}
}
int * whiteBallInput = getWhiteBallInput(gamePlayChoice, 1, 69);
while (true) {
cout << "Do you want to self pick your red ball numbers(Y or N): ";
cin >> gamePlayChoice;
if (gamePlayChoice.compare("Y") == 0 || gamePlayChoice.compare("N") == 0 || gamePlayChoice.compare("y") == 0 || gamePlayChoice.compare("n") == 0) {
break;
}
}
int userGamePlayNumbers[6];
for (int i = 0; i < 6; i++) {
userGamePlayNumbers[i] = *(whiteBallInput + i);
}
userGamePlayNumbers[5] = getRedBallInput(gamePlayChoice);
string result = getResult(userGamePlayNumbers, winningNumberSet);
cout << "******** Game Report *********\n\n";
cout << "You won " << result << " points for this Game.\n\n";
cout << "Here are your numbers: ";
for (int i = 0; i < 6; i++) {
cout << userGamePlayNumbers[i] << " ";
}
cout << "\n\n";
cout << "Here are winning numbers: ";
for (int i = 0; i < 6; i++) {
cout << winningNumberSet[i] << " ";
}
system("pause");
return 0;
}
| |