-
Notifications
You must be signed in to change notification settings - Fork 27
Expand file tree
/
Copy pathHangman.cpp
More file actions
53 lines (49 loc) · 1.2 KB
/
Hangman.cpp
File metadata and controls
53 lines (49 loc) · 1.2 KB
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
// Hangman.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
#include <string>
std::string words [] = { "hello", "python" };
void user_guess(std::string ¤t, std::string &word) {
std::cout << "Current state: " << current << std::endl;
char guess;
std::cin >> guess;
if (word.find(guess) != std::string::npos &&
current.find(guess) == std::string::npos) {
char *c, *w;
for (c = ¤t[0], w = &word[0];
c < ¤t[0] + current.length();
++c, ++w) {
*c = *w == guess ? *w : *c;
}
}
else {
std::cout << "Nope! " << guess << " is not in the word." << std::endl;
}
}
int main()
{
for (std::string word : words) {
int remaining = 5;
std::string current = std::string(word.length(), '*');
std::string old_state;
bool broke = false;
while (remaining > 0) {
old_state = current;
user_guess(current, word);
if (old_state == current) {
--remaining;
std::cout << "Remaining guesses: " << remaining << std::endl;
}
if (current == word) {
std::cout << "You won! - " << word << std::endl;
broke = true;
break;
}
}
if (!broke) {
std::cout << "You lost. - " << word << std::endl;
}
}
return 0;
}