Created
August 19, 2016 03:50
-
-
Save knowsuchagency/d0403637bf1a7fd4fd4b0d93de83280b to your computer and use it in GitHub Desktop.
https://repl.it/Co4Z/12 created by knowsuchagency
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <string> | |
#include <vector> | |
#include <cstdlib> | |
#include <ctime> | |
#include <set> | |
using namespace std; | |
vector<int> get_indexes(string input_string, char letter){ | |
/* Return a vector of integers indicating the indexes where the char is | |
found in the string. */ | |
vector<int> output; | |
for(int i = 0; i < input_string.size(); i++){ | |
if (input_string[i] == letter){ | |
output.push_back(i); | |
} | |
} | |
return output; | |
} | |
void update_guess(string& guess_string, string secret_word, char letter){ | |
/* Modify the guess string in place, replacing the characters in the | |
string with the corresponding letter in the secret word. */ | |
// We need letter to be a string for the .replace method to work | |
// so we'll consruct it | |
string sletter(1, letter); | |
for (int i: get_indexes(secret_word, letter)){ | |
guess_string.replace(i, 1, sletter); | |
} | |
} | |
int main(){ | |
// create a set of words we can use as potential answers | |
set <string> words_set = { | |
"ye", | |
"though", | |
"aye", | |
"walk", | |
"through", | |
"the", | |
"valley", | |
"of", | |
"the", | |
"shadow", | |
"of", | |
"death", | |
"aye", | |
"shall", | |
"fear", | |
"no", | |
"evil", | |
"fore", | |
"aye", | |
"am", | |
"the", | |
"meanest", | |
"son", | |
"of", | |
"ayy", | |
"bitch", | |
"in", | |
"the", | |
"valley", | |
}; | |
// construct a vector from the set of words | |
vector <string> secret_words(words_set.begin(), words_set.end()); | |
// seed random | |
srand(time(NULL)); | |
// Choose a secret word from the secret words at random | |
int random_index = rand() % secret_words.size(); | |
string secret_word = secret_words[random_index]; | |
// set guess counter and max number of guesses | |
int guesses = 0; | |
int max_guesses = 5; | |
// set placeholder for incorrect guesses | |
set<char> wrong_guesses; | |
// create a guess string of equal length to the secret | |
// word made up of '.' | |
string guess_string(secret_word.size(), '.'); | |
// Prints at the beginning of every game | |
cout << "Let's play some hangman!" << endl << endl; | |
// Begin the game loop | |
while (guesses <= max_guesses){ | |
// Get user input | |
string user_input; | |
cout << "Type in a letter:" << endl << endl; | |
cin >> user_input; | |
// Make sure they only type in one letter | |
if (user_input.size() > 1) { | |
cout << endl << "You can only type in ONE character" << endl; | |
} | |
// The letter they typed in was in the answer | |
else if (secret_word.find(user_input) != string::npos) { | |
// Convert the input string to a character | |
char letter = user_input.front(); | |
// Update the guess_string | |
update_guess(guess_string, secret_word, letter); | |
// Win condition; exit loop and return 0 | |
if (guess_string == secret_word){ | |
cout << endl << "You win! The secret word was " << secret_word + '!' << endl; | |
return 0; | |
} | |
cout << endl << "Good guess!" << endl; | |
} | |
// The letter they typed in was not in the secret_word | |
else | |
{ | |
cout << endl << "Shitty Guess" << endl; | |
// append guess to wrong guesses | |
wrong_guesses.insert(user_input.front()); | |
// increment guesses | |
guesses++; | |
} | |
// endif | |
// calculate remaining guesses | |
int remaining_guesses = max_guesses - guesses; | |
// variable to print guess if only one guess remaining else guesses | |
string guess_or_guesses = (remaining_guesses==1) ? "guess":"guesses"; | |
// construct string from wrong guess set | |
string swrong_guesses(wrong_guesses.begin(), wrong_guesses.end()); | |
// print correct guesses | |
cout << endl << "Your correct guesses thus far: " << guess_string << endl; | |
// print wrong guesses | |
if (!wrong_guesses.empty()){ | |
cout << "Wrong guesses thus far: {" << swrong_guesses << '}' << endl; | |
} | |
// print the remaining number of turns | |
cout << "You have " << remaining_guesses << ' ' + guess_or_guesses << " remaining" << endl << endl; | |
} | |
// You lost | |
cout << "Lost. You suck. The word was: " << secret_word << endl; | |
// test | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
gcc version 4.6.3 | |
>>> Let's play some hangman! | |
Type in a letter: | |
u | |
Shitty Guess | |
Your correct guesses thus far: ...... | |
Wrong guesses thus far: {u} | |
You have 4 guesses remaining | |
Type in a letter: | |
i | |
Shitty Guess | |
Your correct guesses thus far: ...... | |
Wrong guesses thus far: {iu} | |
You have 3 guesses remaining | |
Type in a letter: | |
e | |
Good guess! | |
Your correct guesses thus far: ....e. | |
Wrong guesses thus far: {iu} | |
You have 3 guesses remaining | |
Type in a letter: | |
r | |
Shitty Guess | |
Your correct guesses thus far: ....e. | |
Wrong guesses thus far: {iru} | |
You have 2 guesses remaining | |
Type in a letter: | |
y | |
Good guess! | |
Your correct guesses thus far: ....ey | |
Wrong guesses thus far: {iru} | |
You have 2 guesses remaining | |
Type in a letter: | |
l | |
Good guess! | |
Your correct guesses thus far: ..lley | |
Wrong guesses thus far: {iru} | |
You have 2 guesses remaining | |
Type in a letter: | |
v | |
Good guess! | |
Your correct guesses thus far: v.lley | |
Wrong guesses thus far: {iru} | |
You have 2 guesses remaining | |
Type in a letter: | |
a | |
You win! The secret word was valley! |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment