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 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234
|
// Start off by iterating through array and placing question marks in every position .. this will not print the array to the console yet!
int initializeArray(char guessArray[ROWS][COLUMNS])
{
int row, column;
guessesLeft = 5; // Need to make sure guesses reset to 5 when array is initialized. If they aren't set to 5 here, user can close out of game and upon starting again will have less than 5 lives.
// Iterate through array and put a ! in every position
for (row = 0; row < ROWS; row++)
{
for (column = 0; column < COLUMNS; column++)
{
guessArray[row][column] = '!';
}
}
return 0;
}
int displayArray(char guessArray[ROWS][COLUMNS])
{
int row, column;
cout << endl;
cout << " "; // Adding 3 spaces to the top row
for (column = 0; column < COLUMNS; column++)
{
cout << setw(3) << column + 1; // Since arrays start at 0, we add 1
}
cout << endl;
cout << " " << "--------------------------" << endl;
for (row = 0; row < ROWS; row++)
{
cout << " " << row + 1 << "|"; // Since arrays start at 0, we add 1
for (column = 0; column < COLUMNS; column++)
{
cout << setw(3) << guessArray[row][column]; // Helps display the array with spacing to be more readable
}
cout << endl;
}
cout << " " << "--------------------------" << endl;
return 0;
}
// Explain Rules, simple cout to display rules to user
int coutGameRules()
{
cout << "**" << " ********************* " << "**" << endl;
cout << "**" << " " << "**" << endl;
cout << "**" << " Find Gold! " << "**" << endl;
cout << "**" << " " << "**" << endl;
cout << "**" << " You have 5 guesses! " << "**" << endl;
cout << "**" << " " << "**" << endl;
cout << "**" << " Earn an extra guess " << "**" << endl;
cout << "**" << " when you find gold! " << "**" << endl;
cout << "**" << " " << "**" << endl;
cout << "**" << " There are... " << "**" << endl;
cout << "**" << " 5 pieces of gold, " << "**" << endl;
cout << "**" << " and 1 bomb. " << "**" << endl;
cout << "**" << " Good Luck! " << "**" << endl;
cout << "**" << " " << "**" << endl;
cout << "**" << " ********************* " << "**" << endl;
cout << endl;
cout << endl;
return 0;
}
// Secretly place gold and bombs in array , but don't show it to "hide" the bombs and gold.
int populateArray(char guessArray[ROWS][COLUMNS])
{
int bombInputCounter = 0;
int x, y;
int goldInputCounter = 0;
srand(time_t(NULL)); // Seed so everything is random
// Adds a piece of gold at a random (X,Y) coordinate until 5 pieces are placed
do {
x = rand() % ROWS; // Get a random Row #
y = rand() % COLUMNS; // Get a random Column #
if (guessArray[x][y] != 'G')
{
guessArray[x][y] = 'G'; // Put 'G' for Gold at the random (X,Y) Coordinate
goldInputCounter++; // Hacky way of making sure 5 pieces of gold are input ... see the Update
}
} while (goldInputCounter < 5);// guessArray contains <= 5 'G' characters
// Adds a bomb at a random (X,Y) coordinate
do
{
x = rand() % ROWS; // Get a random Row #
y = rand() % COLUMNS; // Get a random Column #
if (guessArray[x][y] != 'G') // If random row and column are equal to 'G', go back to beginning of loop and get another random row and column. This prevents a bomb covering up a piece of gold!
{
guessArray[x][y] = 'B'; // Put 'B' for Bomb at the random (X,Y) coordinate
bombInputCounter++;
}
} while (bombInputCounter < 1);
return 0;
}
int gameLogic(char guessArray[ROWS][COLUMNS])
{
int points = 0;
int gRow, gColumn; // gRow = guessRow; gColumn = guessColumn
do
{
// Ask for coordinate entry from user
cout << "Enter an X coordinate between 1 and 8: ";
cin >> gRow;
//While the input entered is not an integer, prompt the user to enter an integer.
while (!cin || gRow > 8 || gRow < 1)
{
cout << "Please enter an integer that is between 1 and 8: ";
cin.clear();
cin.ignore();
cin >> gRow;
}
gRow--;
cout << endl;
cout << "Enter a Y coordinate between 1 and 8: ";
cin >> gColumn;
//While the input entered is not an integer, prompt the user to enter an integer.
while (!cin || gColumn > 8 || gColumn < 1)
{
cout << "Please enter an integer that is between 1 and 8: ";
cin.clear();
cin.ignore();
cin >> gColumn;
}
gColumn--;
cout << endl;
if (guessArray[gRow][gColumn] == 'G')
{
cout << "You Found Gold!! You've gained an extra guess and now have " << guessesLeft << " guesses left!";
guessArray[gRow][gColumn] = 'F'; // This makes it so user knows where they found gold at the end of the game
points += 1; // Give user a point if they find gold!
cout << endl;
cout << endl;
continue; // Go back to beginning of loop
}
// If they find bomb, break out of loop and end game.
else if (guessArray[gRow][gColumn] == 'B')
{
cout << endl;
cout << "BOMB! Oh no! You have lost the game." << endl << endl;
break; // Lose Game
}
// If they don't find anything, say TOO BAD and subtract a guess
else
{
cout << "Too bad, no gold." << endl;
guessesLeft--; // Decrement guessesLeft by 1
cout << "You have " << guessesLeft << " guesses left!" << endl << endl;
}
} while (guessesLeft > 0); // Do this loop until user has 0 guessesLeft
cout << "You've earned " << points << " points!" << endl;
cout << "Better luck next time!" << endl;
// Display array at the end where all of the gold and bombs were placed at the end
cout << "Here's your board:" << endl << endl;
// Take out question marks from array
for (gColumn = 0; gColumn < COLUMNS; gColumn++)
{
for (gRow = 0; gRow < ROWS; gRow++)
{
if (guessArray[gRow][gColumn] == '?')
{
guessArray[gRow][gColumn] = ' ';
}
}
}
// The logic below displays the guessArray to the user but adds the hidden bombs and hidden gold. Also shows where the user found gold!
cout << " ";
for (gColumn = 0; gColumn < COLUMNS; gColumn++)
{
cout << setw(3) << gColumn + 1; // Since arrays start at 0, we add 1. Setw helps formatting --- spaces columns out
}
cout << endl;
cout << " " << "--------------------------" << endl;
for (gRow = 0; gRow < ROWS; gRow++)
{
cout << " " << gRow + 1 << "|"; // Since arrays start at 0, we add 1
for (gColumn = 0; gColumn < COLUMNS; gColumn++)
{
cout << setw(3) << guessArray[gRow][gColumn]; // Helps display the array with spacing to be more readable
}
cout << endl;
}
cout << " " << "--------------------------" << endl;
return 0;}
| |