Beginning c++ through game programming code review
Jul 24, 2015 at 6:18pm UTC
hey all so I'm on chapter 6 of this great book and one of the exercises says to redo the mad lib game using references, which I did but found it to be too easy. I decided to go back through and remake the hangman game using vectors, references, and functions. Some parts seem hack-ish to me so let me know what you think I could improve on!
thanks.
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
#include "stdafx.h"
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <ctime>
#include <cctype>
using namespace std;
//Maybe not needed?
char askLetter();
string WORDS();
void instructions();
void winLose(const int & max_turns);
void displayBoard(const int & turns);
void makeGuess(char & guess, string& THE_WORD, int & turns);
int main()
{
instructions();
string word = WORDS();
cout << word << endl;
char guess = askLetter();
int turns = 0;
makeGuess(guess, word, turns);
return 0;
}
void makeGuess(char & guess, string& THE_WORD, int & turns)
{
string soFar(THE_WORD.size(), '-' );
string used = "" ;
while ((turns != 7) && (soFar != THE_WORD))
{
while (used.find(guess) != string::npos)
{
cout << "already guessed that\n" ;
guess = askLetter();
}
used += guess;
if (THE_WORD.find(guess) != string::npos)
{
cout << "That is in the word!" << endl;
for (unsigned int i = 0; i < THE_WORD.length(); i++)
{
if (THE_WORD[i] == guess)
{
soFar[i] = guess;
}
}
}
else
{
cout << "Sorry not in the word" << endl;
turns++;
}
cout << "These are the letters you have used so far: " << used << endl;
displayBoard(turns);
cout << "So far the word is \n\t " << soFar << endl;
//Hackish maybe? the while loop wasnt exiting when i got here and asked for another
//letter even if i had the word or i used all my turns so i added this
if (soFar == THE_WORD){
break ;
}
else if (turns == 7){
break ;
}
else {
guess = askLetter();
}
//to create more space and not have each 'board' drawing so close
cout << "\n\n\n\n\n\n\n\n\n\n\n\n\n" ;
}
//display the final board and see if you won or not
displayBoard(turns);
winLose(turns);
}
string WORDS()
{
vector<string> words;
words.push_back("GUESS" );
words.push_back("WATER" );
words.push_back("GUITAR" );
words.push_back("FISHING" );
words.push_back("BOOKS" );
srand(static_cast <unsigned int > (time(0)));
random_shuffle(words.begin(), words.end());
const string THE_WORD = words[0];
string soFar(THE_WORD.size(), '-' );
string used = "" ;
return THE_WORD;
return soFar;
}
char askLetter()
{
char Letter;
cout << "Please give me a letter: " ;
cin >> Letter;
Letter = toupper(Letter);
return Letter;
}
void instructions()
{
cout << "Welcome to HangMan.\n\n" ;
cout << "Try and guess the word before you get hanged.\n" ;
}
void winLose(const int & max_turns)
{
if (max_turns == 7)
{
cout << "You were hanged and are now dead sorry!" << endl;
}
else
cout << " You escaped with your life this time" << endl;
}
//ASCII drawing of my gameboard
void displayBoard(const int & turns)
{
switch (turns)
{
case 0:
cout << " ______ " << endl;
cout << " | | " << endl;
cout << " | | " << endl;
cout << " | " << endl;
cout << " | " << endl;
cout << " | " << endl;
cout << " | " << endl;
cout << " |_______" << endl;
break ;
case 1:
cout << " ______ " << endl;
cout << " | | " << endl;
cout << " | | " << endl;
cout << " | O " << endl;
cout << " | " << endl;
cout << " | " << endl;
cout << " | " << endl;
cout << " |_______" << endl;
break ;
case 2:
cout << " ______ " << endl;
cout << " | | " << endl;
cout << " | | " << endl;
cout << " | O " << endl;
cout << " | 8 " << endl;
cout << " | " << endl;
cout << " | " << endl;
cout << " |_______" << endl;
break ;
case 3:
cout << " ______ " << endl;
cout << " | | " << endl;
cout << " | | " << endl;
cout << " | O " << endl;
cout << " | 8 " << endl;
cout << " | 8 " << endl;
cout << " | " << endl;
cout << " |_______" << endl;
break ;
case 4:
cout << " ______ " << endl;
cout << " | | " << endl;
cout << " | | " << endl;
cout << " | O " << endl;
cout << " | 8\\ " << endl;
cout << " | 8 " << endl;
cout << " | " << endl;
cout << " |_______" << endl;
break ;
case 5:
cout << " ______ " << endl;
cout << " | | " << endl;
cout << " | | " << endl;
cout << " | O " << endl;
cout << " | /8\\ " << endl;
cout << " | 8 " << endl;
cout << " | " << endl;
cout << " |_______" << endl;
break ;
case 6:
cout << " ______ " << endl;
cout << " | | " << endl;
cout << " | | " << endl;
cout << " | O " << endl;
cout << " | /8\\ " << endl;
cout << " | 8 " << endl;
cout << " | / " << endl;
cout << " |_______" << endl;
break ;
case 7:
cout << " ______ " << endl;
cout << " | | " << endl;
cout << " | | " << endl;
cout << " | O " << endl;
cout << " | /8\\ " << endl;
cout << " | 8 " << endl;
cout << " | / \\" << endl;
cout << " |_______" << endl;
break ;
}
}
Jul 24, 2015 at 6:21pm UTC
P.S. I have the word printing out in the very beginning for debugging purposes and forgot to take it back out.
Jul 24, 2015 at 7:06pm UTC
Line 95: srand() should only ever be called once during a program. Calling it more than once in the same second will cause the RNG to be reset back to the beginning of the sequence. The best place to put a call to srand() is in the beginning of main.
Line 100-101: You have two return statements. The second one will never be reached.
line 98: soFar is never returned.
Line 99: used is never referenced.
Lines 69-77: The elses are not needed.
1 2 3 4 5
if (soFar == THE_WORD)
break ;
if (turns == 7)
break ;
guess = askLetter();
As to hackish, I'd change the while loop to a do while loop.
1 2 3 4 5
do
{ guess = ask_letter();
...
}
while (! (turns == 7 || soFar == THE_WORD));
Topic archived. No new replies allowed.