I feel it is so close, but I am not able to get the game to work right. It prints out both winning and losing lines whether or not the player guesses a correct letter. I tried this after watching a tutorial, but I'm not sure where I went wrong or if I should just start over. Perhaps I was too ambitious. To be clear, this is for school but I am allowed outside help, just not stealing others work, or letting someone else code for me. I want to understand my mistakes, not just gloss over them. anything would be appreciated. TOTAL NOOB
// This was added so it would run on my compiler. I am
// not sure you need it.... Jim Shain
//#include "stdafx.h"
int playerLives = 7; // Our hangman has 7 stages, so the player starts with 7 lives
char knownLetters[50]; // The word could be of any length
string word = "";
//string word = wordList[wordID];
string GenerateWord();
void PrintLetters();
void PrintMan();
void PlayerLose();
void PlayerWin();
// I put PrintMan and the others here. Jim
void PrintMan() {
switch(playerLives) {
case 7:
cout << "\n\n\n\n\n";
break;
case 6:
cout << "\n\n\n\n______";
break;
case 5:
cout << "\n |\n |\n |\n |\n____|_";
break;
case 4:
cout << " ___\n | \\|\n |\n |\n |\n____|_";
break;
case 3:
cout << " ___\n | \\|\n O |\n |\n |\n____|_";
break;
case 2:
cout << " ___\n | \\|\n O |\n | |\n |\n____|_";
break;
case 1:
cout << " ___\n | \\|\n O |\n/|\\ |\n |\n____|_";
break;
case 0:
cout << " ___\n | \\|\n O |\n/|\\ |\n/ \\ |\n____|_";
break;
}
// Jim
string GenerateWord() {
srand(time(NULL)); // Use the system time as a seed
int wordID = rand() % 11; // Replace 11 with the amount of words you have + 1
void PlayerWin() {
cout << "yay, you guessed the word correctly! winter is coming";
cout << "Press ENTER to quit\n";
cin.get();
running = 0;
return;
}
void PlayerLose() {
cout << "You lose! may the seven gods forgive you. The word was: " << word << "\n";
cout << "Press ENTER to quit\n";
cin.get();
running = 0;
return;
}
int main() {
word = GenerateWord(); // Pick a random word
char userGuess;
while(running) {
cout << "Guess a letter, hint the word is a house in game of thrones: ";
cin >> userGuess;
bool correct = 0;
//original
//for(int pos = 0; pos < word.length; pos++) {
// The length of a string is a function so you need parens after it. - Jim
for(int pos = 0; pos < word.length(); pos++) {
if(userGuess == word[pos]) {
knownLetters[pos] = userGuess; // If the letter is in the word, mark it as guessed
correct = 1;
}
}
// Jim
int lives;
if(correct == 0) lives--; // If the letter was wrong, subtract a life
correct = 0;
PrintMan();
PrintLetters();
if(lives <= 0) PlayerLose();
// Jim
string knowLetters;
if(knowLetters == word) PlayerWin();
}
cout << "\n";
// Jim main must return a value
//original
//return;
return(0);
}
#include<iostream>
#include<string>
#include<stdlib.h>
#include<time.h>
usingnamespace std;
bool running = 1;
int playerLives = 7; // Our hangman has 7 stages, so the player starts with 7 lives
char knownLetters[50]; // The word could be of any length
string word = "";
//string word = wordList[wordID];
string GenerateWord();
void PrintLetters();
void PrintMan();
void PlayerLose();
void PlayerWin();
// I put PrintMan and the others here. Jim
void PrintMan() {
switch(playerLives) {
case 7:
cout << "\n\n\n\n\n";
break;
case 6:
cout << "\n\n\n\n______";
break;
case 5:
cout << "\n |\n |\n |\n |\n____|_";
break;
case 4:
cout << " ___\n | \\|\n |\n |\n |\n____|_";
break;
case 3:
cout << " ___\n | \\|\n O |\n |\n |\n____|_";
break;
case 2:
cout << " ___\n | \\|\n O |\n | |\n |\n____|_";
break;
case 1:
cout << " ___\n | \\|\n O |\n/|\\ |\n |\n____|_";
break;
case 0:
cout << " ___\n | \\|\n O |\n/|\\ |\n/ \\ |\n____|_";
break;
}
cout << "\n"; // For formatting purposes
return;
}
void PrintLetters() {
for(int pos = 0; pos < word.length(); pos++) {
if(knownLetters[pos] == word[pos]) cout << word[pos];
else cout << "_";
}
return;
}
// Jim
string GenerateWord() {
srand(time(NULL)); // Use the system time as a seed
int wordID = rand() % 11; // Replace 11 with the amount of words you have + 1
string word = "";
string wordList[10] = { "lannister", "danarys", "dothraki", "stark", "baratheon", "snow", "arryn", "greyjoy", "tully", "tyrell" };
return word;
}
void PlayerWin() {
cout << "yay, you guessed the word correctly! winter is coming";
cout << "Press ENTER to quit\n";
cin.get();
running = 0;
return;
}
void PlayerLose() {
cout << "You lose! may the seven gods forgive you. The word was: " << word << "\n";
cout << "Press ENTER to quit\n";
cin.get();
running = 0;
return;
}
int main() {
word = GenerateWord(); // Pick a random word
char userGuess;
while(running) {
cout << "Guess a letter, hint the word is a house in game of thrones: ";
cin >> userGuess;
bool correct = 0;
//original
//for(int pos = 0; pos < word.length; pos++) {
// The length of a string is a function so you need parens after it. - Jim
for(int pos = 0; pos < word.length(); pos++) {
if(userGuess == word[pos]) {
knownLetters[pos] = userGuess; // If the letter is in the word, mark it as guessed
correct = 1;
}
}
// Jim
int lives;
if(correct == 0) lives--; // If the letter was wrong, subtract a life
correct = 0;
PrintMan();
PrintLetters();
if(lives <= 0) PlayerLose();
// Jim
string knowLetters;
if(knowLetters == word) PlayerWin();
}
cout << "\n";
// Jim main must return a value
//original
//return;
return(0);
}
Problems:
- On line 122 you create a variable named "lives" but you do not initialize it, therefore its value is garbage/worthless. You probably meant to use the global "playerLives" instead of making an entirely new variable.
- Similar problem on line 135. You create another string named "knowLetters" but never assign it to anything, so it's an empty string. You probably meant to use the global "knownLetters" instead of a new variable.
- On line 73 you create a "word" variable, but you never assign it to anything. As a result, your GenerateWord function returns an empty string every time. wordList and wordID never get used.
got it! thank you so much, I know this is cheesy, but it was mostly proofreading, thank you for pointing out my errors.
I got the random word generator to work as well, with
string word = wordList[wordID];;
so cool. time to clean it all up, make a bigger and badder word list, and put in a lot of comments. my first game ever! (well, one built by me, and understood!)