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 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217
|
bool GetQuote(int &userScore)
{
// declare and initialize variables
int myr = 0, myt = 0, answerQuit = 0, answerA = 0, answerB = 0, randFile = 0, randSize = 0;
bool correctA = 0, correctB = 0;
// Pseudo RNG seems to favor lower numbers. Creating two quote files, with quotes from near the bottom of the first file, placed near the top of the second file.
// This will increase the randomness of quotes, and lessen the chance of repeating the top few quotes every time the game is played.
// Seed RNG, get random number (for selection of quote file 1 or quote file 2)
srand(time(NULL));
randFile = rand() % 2 + 1; // (1 - 2)
// seed RNG, get random number (for selection of line number in quote file)
srand(time(NULL));
randSize = rand() % 25 + 1; // (1 - 25) (if randSize = 0, loop fails, function reports initialized values of "" (nothing)
// declare fstream variable and open file for reading
ifstream infile;
if (randFile == 1)
infile.open("quotes1.txt");
else
infile.open("quotes2.txt");
// declare and initialize parallel array for selecting line in quote file
// To save memory and cpu time, I was only initializing to randSize, instead of always initializing to max number of lines (25)
// However, that approach was creating random program crashes.
string quoteArr [25], correctArr [25], option1Arr [25], option2Arr [25], option3Arr [25];
for (int q = 0; q < 25; q++)
{
quoteArr[q] = "";
correctArr[q] = "";
option1Arr[q] = "";
option2Arr[q] = "";
option3Arr[q] = "";
}
// select a random line containing quote, correctA answerA, and three possible answers, from text file.
// semicolon is being used as deliminator
for (int r = 0; r < randSize; r++)
{
getline(infile, quoteArr[r], ';');
getline(infile, correctArr[r], ';');
getline(infile, option1Arr[r], ';');
getline(infile, option2Arr[r], ';');
getline(infile, option3Arr[r], ';');
myr = r;
}
cout << "Beginning Level 2. You will have to correctly answer a trivia question from" << endl;
cout << "The Godfather movies." << endl << endl;
// Prompt random question and input answer
cout << "WHO SAID: \n\n" << quoteArr[myr] << endl << endl;
cout << "1) " << option1Arr[myr] << endl;
cout << "2) " << option2Arr[myr] << endl;
cout << "3) " << option3Arr[myr] << endl << endl;
do
{
cout << "Select 1, 2, or 3: ";
cin >> answerA;
if (answerA < 1 || answerA > 3)
cout << "Input error. Please choose 1, 2, or 3." << endl << endl;
cout << endl;
}
while (answerA < 1 || answerA > 3);
// Check to see if answer is correct
if (answerA == 1)
if (option1Arr [myr] == correctArr [myr])
correctA = 1;
else
correctA = 0;
if (answerA == 2)
if (option2Arr [myr] == correctArr [myr])
correctA = 1;
else
correctA = 0;
if (answerA == 3)
if (option3Arr [myr] == correctArr [myr])
correctA = 1;
else
correctA = 0;
//Update score
if (correctA == 1)
{
cout << "CORRECT !! YOU'VE EARNED 100 BONUS POINTS." << endl << endl;
userScore += 100;
cout << "Your score is now " << userScore << endl << endl;
}
else
{
cout << "INCORRECT !! YOU'VE LOST 100 POINTS." << endl;
userScore -= 100;
cout << "Your score is now " << userScore << endl << endl;
}
// If player dies, end game.
if (userScore <= 0)
return 0;
//Problem starts somewhere after this point
// If player is still alive, offer a shot at double or nothing
cout << "You can be weak, like Johnny Fontane, and quit now; or be strong, like" << endl;
cout << "Lucca Brazzi, and play for double or nothing. What would you like to do?" << endl << endl;
do
{
cout << "1) Quit now. " << endl;
cout << "2) Play one more round for double or nothing. " << endl;
cin >> answerQuit;
cout << endl;
if (answerQuit < 1 || answerQuit > 2)
cout << "Input error. You must select 1 or 2." << endl << endl;
}
while (answerQuit < 1 || answerQuit > 2);
// if player quits:
if (answerQuit == 1)
{
cout << "QUITTER !! Your score is now " << userScore << "If you had continued, your score" << endl;
cout << "could have been " << userScore * 2 << "." << endl << endl;
return 1;
}
// if player plays double or nothing:
// Selecting a random line in quotes file could ask user the same question as last time. Select the next question in quotes file.
randSize += 1;
// if already on the last line in quotes file, ask quesiton in the first line.
if (randSize == 26)
randSize = 1;
// initialize array for double or nothing question
string quoteArr1 [25], correctArr1 [25], option1Arr1 [25], option2Arr1 [25], option3Arr1 [25];
for (int s = 0; s < 25; s++)
{
quoteArr1[s] = "";
correctArr1[s] = "";
option1Arr1[s] = "";
option2Arr1[s] = "";
option3Arr1[s] = "";
}
// remove cin endline from keyboard buffer
cin.ignore();
// get double or nothing question
for (int t = 0; t < randSize; t++)
{
getline(infile, quoteArr1[t], ';');
getline(infile, correctArr1[t], ';');
getline(infile, option1Arr1[t], ';');
getline(infile, option2Arr1[t], ';');
getline(infile, option3Arr1[t], ';');
myt = t;
}
//This is the part where the cout text appears but the variables (quoteArr1[myt], option1Arr1[myt], option2Arr1[myt], option3Arr1[myt]) sometimes do not appear).
// Prompt next question and input answer
cout << "Who said: \n\n" << quoteArr1[myt] << endl << endl;
cout << "1) " << option1Arr1[myt] << endl;
cout << "2) " << option2Arr1[myt] << endl;
cout << "3) " << option3Arr1[myt] << endl << endl;
do
{
cout << "Select 1, 2, or 3: ";
cin >> answerB;
if (answerB < 1 || answerB > 3)
cout << "Input error. Please choose 1, 2, or 3." << endl << endl;
cout << endl;
}
while (answerB < 1 || answerB > 3);
// Check to see if answer is correct
if (answerB == 1)
if (option1Arr1 [myt] == correctArr1 [myt])
correctB = 1;
else
correctB = 0;
if (answerB == 2)
if (option2Arr1 [myt] == correctArr1 [myt])
correctB = 1;
else
correctB = 0;
if (answerB == 3)
if (option3Arr1 [myt] == correctArr1 [myt])
correctB = 1;
else
correctB = 0;
// Update score
if (correctB == 1)
{
cout << "CORRECT !! YOU'VE DOUBLED YOUR POINTS !! " << endl << endl;
userScore *= 2;
cout << "Your score is now " << userScore << endl << endl;
return 1;
}
else
{
cout << "INCORRECT !! " << endl;
userScore = 0;
cout << "Your score is now 0" << endl << endl;
return 0;
}
// Close quotes file
infile.close();
}
|