-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhangman.cpp
More file actions
149 lines (126 loc) · 3.27 KB
/
hangman.cpp
File metadata and controls
149 lines (126 loc) · 3.27 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
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
/*
Hangman
(c) Afaan Bilal
Play hangman!
*/
#include <iostream>
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <time.h>
#include <string>
#include <fstream>
using namespace std;
const int numWords = 50;
string wordlist[numWords];
string input;
size_t f;
int sel = 0;
int maxTries = 10;
int tries = 0;
void cls();
void display();
bool checkIfWon();
int main()
{
ifstream file("hangman.txt");
if(file.is_open())
{
for(int i = 0; i < numWords; ++i)
{
file >> wordlist[i];
}
}
srand(time(NULL));
sel = rand() % numWords;
char c = ' ';
display();
cout << endl << "\tEnter an alphabet: ";
c = _getch();
while (c != '.')
{
c = toupper(c);
f = input.find(c);
if (f == string::npos)
{
input.push_back(c);
if (wordlist[sel].find(c) == string::npos)
{
tries++;
if (tries >= maxTries)
{
display();
cout << endl << "\t\tSorry! You have lost!" << endl << endl;
cout << "\t\tThe word was " << wordlist[sel] << endl << endl;
break;
}
}
display();
if (checkIfWon())
{
cout << endl << "\t\tCongratulations! You have won!" << endl << endl;
break;
}
}
else
{
cout << endl << "\tAlphabet already entered." << endl;
}
cout << endl << "\tEnter an alphabet: ";
c = _getch();
}
}
void cls()
{
system("@cls || clear");
}
void drawHangman()
{
cout << endl;
switch (tries)
{
case 10:
cout << "\t\t YOU KILLED ME!" << endl;
cout << "\t\t\t _________ " << endl;
case 9: cout << "\t\t\t | |" << endl;
case 8: cout << "\t\t\t --- |" << endl;
case 7: cout << "\t\t\t | | |" << endl;
case 6: cout << "\t\t\t --- |" << endl;
case 5: cout << "\t\t\t ----|---- |" << endl;
case 4: cout << "\t\t\t | |" << endl;
case 3: cout << "\t\t\t | |" << endl;
case 2: cout << "\t\t\t --- |" << endl;
case 1: cout << "\t\t\t / \\ |" << endl;
cout << "\t\t\t |" << endl;
cout << "\t\t\t____________|" << endl;
}
cout << endl << endl;
}
void display()
{
cls();
cout << endl << "\t\t\t" << "HANGMAN" << endl << endl;
cout << "\thttps://github.com/AfaanBilal/hangman" << endl << endl;
drawHangman();
cout << "\tPress '.' to exit." << endl;
cout << "\tTries left: " << (maxTries - tries) << endl << endl;
cout << "\t\t ";
for (int i = 0; i < wordlist[sel].length(); i++)
{
f = input.find(wordlist[sel][i]);
if (f == string::npos)
cout << "_" << " ";
else
cout << wordlist[sel][i] << " ";
}
cout << endl;
}
bool checkIfWon()
{
for (int i = 0; i < wordlist[sel].length(); i++)
{
if (input.find(wordlist[sel][i]) == string::npos)
return false;
}
return true;
}