-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathHangman.cpp
More file actions
115 lines (95 loc) · 2.54 KB
/
Hangman.cpp
File metadata and controls
115 lines (95 loc) · 2.54 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
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
#include<iostream>
#include<ctime>
#include<cstdlib>
#include<windows.h>
using namespace std;
char blank[6]={'_','_','_','_','_','_'};
void layout();
int check(char &, int &, char arr[][6]);
int main(){
char inp;
int catcher=1,lives=8;
char words[6][6] {
{'b','a','t','m','a','n'},
{'k','i','l','l','e','r'},
{'b','e','a','u','t','y'},
{'b','a','n','a','n','a'},
{'t','a','x','i','l','a'},
{'m','o','b','i','l','e'},
};
srand(time(0));
int num=rand() % 6 + 1;
while(blank[0]=='_' || blank[1]=='_' || blank[2]=='_' || blank[3]=='_' || blank[4]=='_' || blank[5]=='_' )
{
layout();
cout<<"Enter a guess in Lower case, You have "<<lives<<" lives left \n"<<endl;
cin>>inp;
catcher=check(inp,num, words);
if(catcher==0)
{
cout<<"\nWrong guess, 1 life depleted"<<endl;
lives--;
Sleep(1000);
}
if(lives==0)
break;
}
layout();
switch(lives)
{
case 0:
cout<<" You ran out of lives, the correct word was ";
for(int k=0;k<=5;k++)
{
cout<<words[num-1][k];
}
cout<<endl;
system("pause");
break;
default:
cout<<" Congrats, you won!"<<endl;
system("pause");
break;
}
}
//Checking function//
int check(char &inp, int &num, char arr[][6]){
int j=0;
for(int i=0;i<6;i++)
{
if(inp==arr[num-1][i])
{
blank[i]=arr[num-1][i];
j=1;
}
else
{
continue;
}
}
return j;
}
//Layout function//
void layout()
{
system("cls");
cout<<"\n"<<endl;
cout<<"\n"<<endl;
cout<<"\n"<<endl;
cout<<"\n"<<endl;
cout<<" ******************************************************************************************************************************"<<endl;
cout<<" * Hangman *"<<endl;
cout<<" * * "<<endl;
cout<<" * Guess the word correctly. Each wrong guess deplets one life, Guess the correct word before you die! *"<<endl;
cout<<" ******************************************************************************************************************************"<<endl;
cout<<"\n"<<endl;
cout<<"\n"<<endl;
cout<<" ";
for(int i=0;i<=5;i++)
{
cout<<" "<<blank[i];
}
cout<<"\n";
cout<<"\n";
cout<<"\n";
}