/
4, Hangman.cpp
181 lines (142 loc) · 4.69 KB
/
4, Hangman.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
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
#include <iostream>
#include<cstring>
#define MAX_TRIAL 10
using namespace std;
int main(){
system("color B1");//background and text colour
struct player{
char name[20];
int wins;
int loses;
};
player player_1={"",0,0};//initializing the struct variable
player player_2={"",0,0};
char word[100];//stores the entered word maximum of 100 character
int contin,temp2;
int clue;
cout<<"-------------------------------------------Hangman Game v1.0------------------------------------\n";
cout<<"\n\t The Hangman Game is a game played with two players The first player Enters any word and gives \n";
cout<<"\n\t some clues to his opponent then the second one guesses each and every characters of the word\n";
cout<<"+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~+\n\t\t you have 10 chances to gues the word:\n";
//read players name
cout<<"\nplayer-1 Enter your name =>";
cin>>player_1.name;
cout<<"\nplayer-2 Enter your name =>";
cin>>player_2.name;
do{
int correct=1;//count the frequency of the entered alphabet
int trial=0; //counts number of trials
char guess; //stores the guessed character
char temp[20];
char word_table[]={"_______________"}; //holds the characters of the word and reveals the characters for correct guesses.
system("CLS");
cout<<"------------------------ Hangman Game ----------------------------";
cout<<"\n\n\t\tRound "<<player_1.wins+player_1.loses+1;
//list of catagories for the word to be entered
cout<<"\n\n\tTHE WORD IS : ";
cout<<"\n\n\t01. Name of a person";
cout<<"\n\n\t02. Cities or towns in Ethiopia ";
cout<<"\n\n\t03. Name of country in Africa";
cout<<"\n\n\t04. Name of country in the world";
cout<<"\n\n\t05. Name of an animal";
cout<<"\n\n\t06. No clues";
cout<<"\n\n\t"<<player_1.name<<" select Your Option (1-6) => ";
cin>>clue;
a:
cout<<endl<<player_1.name<<" Enter word => "; //enter the word tha is going to be guessed
cin>>word;
int word_length=strlen(word);
for(int i=0;i<word_length; i++){
word[i]=tolower(word[i]);
}
for(int j=0;j<word_length;j++)
{
if(isalpha(word[j])==0){
cout<<"\n\t\tThe word can not contain number (must be aplhabet)!\n";
goto a;
}
system("CLS"); //Clears the screen and the word disapears
cout<<"\t\t\t Hangman Game v1.0\n";
cout<<"\t\t"<<player_1.name<<" vs "<<player_2.name<<"\t\t\tRound "<<player_1.wins+player_1.loses+1;
switch(clue){
case 1:
cout<<"\n\n\t\t\tThe word is Name of a person ";
break;
case 2:
cout<<"\n\n\t\t\tThe word is City or town in Ethiopia ";
break;
case 3:
cout<<"\n\n\t\t\tThe word is Name of country in Africa ";
break;
case 4:
cout<<"\n\n\t\t\tThe word is Name of country in the world ";
break;
case 5:
cout<<"\n\n\t\t\tThe word is Name of an animal ";
break;
default:
cout<<"\n\n\t\t\tNo clues ";
}
do{
cout<<endl<<"\n\t\t"<<player_2.name<<" you have "<<MAX_TRIAL-trial<<" chances to guess the word:";
cout<<"\n\nGuess the word: ";
cin>>guess;
guess=tolower(guess);
for(int i=0;i<word_length;i++)
{
if(word[i]==guess)
{
word_table[i]=word[i];
correct++;
}
else
word_table[i]=word_table[i];
}
cout<<"\t\t";
if(correct>1)
trial;
else
trial++;
correct=1;
for(int i=0;i<word_length;i++)
{
cout<<word_table[i]<<" ";
}
if(strcmp(word,word_table)!=1)
{
cout<<endl<<"\n\t\t "<<player_2.name<<" you won the game\n";
player_2.wins+=1;
player_1.loses+=1;
break;
}
if(trial==10){
cout<<endl<<endl<<"The word is "<<word;
cout<<endl<<"\n\t\t "<<player_1.name<<" you won the game\n";
player_1.wins+=1;
player_2.loses+=1;
break;
}
}while(MAX_TRIAL>trial);
cout<<"\t\t THE FINAL RESULT \n"<<endl;
cout<<" Name \tWins \tLoses \n";
cout<<player_1.name<<"\t"<<player_1.wins<<"\t "<<player_1.loses<<"\n";
cout<<player_2.name<<"\t"<<player_2.wins<<"\t "<<player_2.loses<<"\n";
cout<<"\t\t\ttotal plays= "<<player_1.wins+player_1.loses;
break;
}
strcpy(temp,player_1.name);
strcpy(player_1.name,player_2.name);
strcpy(player_2.name,temp);
temp2=player_1.loses;
player_1.loses=player_2.loses;
player_2.loses=temp2;
temp2=player_1.wins;
player_1.wins=player_2.wins;
player_2.wins=temp2;
cout<<"\nEnter 0 to continue and other umber to leave \n";
cin>>contin;
system("CLS");
cout<<"\t\t\t Hangman Game v1.0\n";
}while(contin==0);
return 0;
}