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
|
#include <iostream>
#include <stdlib.h>
#include <ctime>
using namespace std;
void welcome();
const int arraySize = 4;
void guessArray(char guess[][arraySize], int row);
void answerArray(char answer[][arraySize], int row);
void playerTurn(char guess[][arraySize], char answer[][arraySize], int row);
void completedWord(char guess[][arraySize], char answer[][arraySize], int row);
void printWord(char myWord[][arraySize], int size, int row);
int countAsterisks(char guess[][arraySize], int row);
int main()
{
int rowNumber{0};
bool tryAgain{true};
char again{'Y'};
const int minVal = 0;
const int maxVal = 1;
unsigned seed = time(0);
srand (seed);
int row = (rand () % (maxVal - minVal + 1) + minVal);
char guessWord[5][arraySize] = {
{'C','*','R','*'},
{'*','R','*','*'},
{'*','O','*','T'},
{'*','R','*','E'},
{'*','*','N','*'}};
char answerWord[5][arraySize] = {
{'C','A','R','S'},
{'A','R','M','S'},
{'B','O','A','T'},
{'T','R','E','E'},
{'L','I','N','K'}};
welcome(); //Call welcome to output program description and rules
while (tryAgain)
{
guessArray(guessWord, rowNumber);
playerTurn(guessWord, answerWord, rowNumber);
answerArray(answerWord, rowNumber);
cout << "Try Again?(Y/N)";
cin >> again;
if (again == 'Y')
tryAgain = true;
else if (again == 'N')
tryAgain = false;
else
{
cout << "Your input was not Y/N.\n";
cout << "I will assume it is: N\n";
tryAgain = false;
}
return 0;
}
void welcome()
{
/* Output a description of the program and the
developers name */
cout << " ************Welcome to The Guessing Game************\n";
cout << " You will be prompted to enter a guess one letter at a time\n";
cout << " for the incomplete word you are given.\n";
cout << " You will only have 8 Guesses to Win or Lose the Game\n";
cout << " Please enter only capital letters.\n";
cout << " \n";
cout << " Program Developed by: Stephanie Juan.\n";
cout << " ***************************************************\n";
}
void printWord(char myWord[][arraySize], int size, int row)
{
for (int kount = 0; kount < size; kount++)
cout << myWord[row][kount];
cout << endl;
}
void guessArray(char word[][arraySize], int row)
{
for (int cols = 0; cols < 4; cols++)
{
cout << word[row][cols];
}
cout << endl;
}
void answerArray(char word[][arraySize], int row)
{
cout << "The answer was : ";
for (int cols = 0; cols < 4; cols++)
{
cout << word[row][cols];
}
cout << std::endl;
}
void playerTurn(char guess[][arraySize], char answer[][arraySize], int row)
{
int numberOfGuesses{8};
int countAsterisk = countAsterisks(guess, row);
char letter{' '};
while (countAsterisk !=0 && numberOfGuesses !=0)
{
cout << "Enter a letter to guess the word.\n";
cin >> letter;
for (int cols = 0; cols < 4; cols++)
{
if (answer[row][cols] == letter && guess[row][cols] == '*')
{
cout << "Letter was found.\n";
guess[row][cols] = letter;
countAsterisk--;
if (numberOfGuesses < 9)
numberOfGuesses++;
}
}
if (countAsterisk == 0)
completedWord(guess, answer, row);
else
{
numberOfGuesses--;
cout << "You have " << numberOfGuesses << " tries left.\n";
cout << endl;
}
}}
void completedWord(char guess[][arraySize], char answer[][arraySize], int row)
{
bool completeWord{ false };
for (int cols = 0; cols < 4; cols++)
{
if (guess[row][cols] == '*')
completeWord = false;
else if (guess[row][cols] != '*')
completeWord = true;
}
if (completeWord == false)
{
cout << "You did not complete the word.\n";
answerArray(answer, row);
}
else if (completeWord == true)
{
cout << "You got it correct.";
answerArray(answer, row);
cout << endl;
}
}
int countAsterisks(char guess[][arraySize], int row)
{
int countAsterisk{0};
for (int cols = 0; cols < 4; cols++)
{
if (guess[row][cols] == '*')
countAsterisk++;
}
return countAsterisk;
}
|