forked from vedant1771/Hactoberfest2021
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHangGame.cpp
71 lines (70 loc) · 2.32 KB
/
HangGame.cpp
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
#include<bits/stdc++.h>
using namespace std;
int check_for_occurance(char ch , string word , string &guess){
int count = 0;
for(int i = 0; i < word.size() ; i++){
if(word[i] == ch){
guess[i] = ch;
++count;
}
}
return count;
}
void hint_provider(string &guess, string word){
int i = 0;
while(guess[i] != '*') i++;
cout << "You could try out the character " << word[i] <<"\n";
}
int main(){
vector<string> word_array{"programming","anagram","inhertiance","puzzle","encapsulation","java","template","mindgame","dynamic", "greedy", "abstraction"};
int choice ;
srand(time(NULL));
int n = rand()%word_array.size();
string word = word_array[n];
n = word.size();
string guess = string(n , '*');
cout << guess<<"\n";
int max_incorrect = min(24, max(3,n-2)) , incorrect_till_now = 0;
cout <<"Lets start the guessing game\n";
int pos[25]={0};
char ch;
while(incorrect_till_now < max_incorrect){
cin >> ch;
if(pos[ch-'a'] == 1){
cout <<"I fear! You already have shot up all occurance for this character \n ";
cout << "Please try another\n";
continue;
}
if(check_for_occurance(ch, word , guess ) != 0){
cout <<"Ah! You unlocked some characters\n";
cout << "Go on now.... ";
}else{
cout <<"This character isnt present\n";
incorrect_till_now+=1;
if((incorrect_till_now + 1) == max_incorrect){
cout <<"Here`s a hint for you \n";
cout <<"Do you want to access it (Y | N ) ?\t";
char c; cin >> c;
if(c == 'Y')
hint_provider(guess , word);
}
if(max_incorrect == incorrect_till_now){
cout <<"Sorry you are out of tries \n";
cout <<"Give the game another try \n";
cout <<"Have a nice day\n";
break;
}
cout << "Please try another character\n";
cout << "You are left with "<< max_incorrect-incorrect_till_now<<" tries\n";
}
cout << guess <<"\n";
if(guess == word){
cout <<"You won\n";
break;
}
pos[ch-'a'] = 1;
}
cin.ignore();
cin.get();
return 0;
}