-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnew.cpp
More file actions
547 lines (499 loc) · 19.5 KB
/
new.cpp
File metadata and controls
547 lines (499 loc) · 19.5 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
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
#include <iostream>
#include <string>
#include <SFML/Graphics.hpp>
#include <SFML/Audi0.hpp>
#include <stdlib.h>
#include <ctime>
#include <time.h>
using namespace std;
class Game
{
protected:
int tries;
public:
static int max_tries;
static int losses;
static int wins;
int isLost()
{
if (tries > max_tries)
{
return 1;
}
else
{
return 0;
}
}
};
int Game::max_tries = 6;
int Game::wins = 0;
int Game::losses = 0;
class Hangman : public Game
{
private:
string correctString;
string guessedString;
public:
string hint;
Hangman()
{
tries = 0;
}
int getTries()
{
return tries;
}
void setTries(int number)
{
tries = number;
}
void increaseTries()
{
setTries(getTries() + 1);
}
string getCorrectString()
{
return correctString;
}
void setCorrectString(string word)
{
correctString = word;
}
string getGuessedString()
{
return guessedString;
}
void setGuessedString()
{
guessedString = "";
for (int i = 0; i < correctString.length(); i++)
{
guessedString = guessedString + "_";
}
}
void setGuessedString(string word)
{
guessedString = word;
}
bool isWon()
{
if (guessedString == correctString)
{
return true;
}
else
{
return false;
}
}
bool guessWord(char letter)
{
bool correctness = false;
for (int i = 0; i < correctString.length(); i++)
{
if (letter == correctString[i])
{
int sizeOfString = correctString.length();
guessedString = guessedString.substr(0, i) + correctString.substr(i, 1) + guessedString.substr(i + 1, sizeOfString - (i + 1));
correctness = true;
}
}
return correctness;
}
};
int main()
{
srand(time(0));
string words[] = {"PRIVACY", "INTEGER", "TERNARY", "SOFTWARE", "COMPILER"};
string hints[] = {"keeping yourself and your personal information data safe when off and online", "a data type in programming languages, representing a whole number positive or negative", "numbers expressed in the base of three, instead of 10, using 0, 1 and 2 only", "It is a collection of data or computer instructions that tell the computer how to work", "It is a program that translates source code into object code"};
Hangman hg;
int c = (rand() % 5);
hg.hint = hints[c];
hg.setCorrectString(words[c]);
hg.setGuessedString();
bool playing = false;
int positions[20][2];
bool choice[40];
for (int i = 0; i < 40; i++)
{
choice[i] = false;
}
sf::RenderWindow window(sf::VideoMode(900, 900), "HANGMAN!");
sf::Texture texture;
if (!texture.loadFromFile("images\\squared-paper.jpg"))
return EXIT_FAILURE;
sf::Sprite sprite(texture);
sf::Font font;
font.loadFromFile("Fonts\\GreatVibes-Regular.otf");
sf::Font font1;
font1.loadFromFile("Fonts\\Montserrat-Light.otf");
sf::SoundBuffer buffer;
buffer.loadFromFile("Audio\\Wrong-answer-sound-effect.wav");
sf::Sound sound;
sound.setBuffer(buffer);
sf::SoundBuffer buffer1;
if (!buffer1.loadFromFile("Audio\\Vip.wav"))
return -1;
sf::Sound soundintro;
soundintro.setBuffer(buffer1);
sf::SoundBuffer buffer2;
if (!buffer2.loadFromFile("Audio\\wrong.wav"))
return -1;
sf::Sound soundwrong;
soundwrong.setBuffer(buffer2);
sf::SoundBuffer buffer3;
if (!buffer3.loadFromFile("Audio\\correct.wav"))
return -1;
sf::Sound soundcorrect;
soundcorrect.setBuffer(buffer3);
// Create a text
sf::Text text("Hangman", font);
text.setCharacterSize(120);
text.setStyle(sf::Text::Bold);
text.setFillColor(sf::Color::Red);
text.setPosition(200, 0);
sf::Text text1("Play", font);
text1.setCharacterSize(60);
text1.setStyle(sf::Text::Underlined);
text1.setStyle(sf::Text::Bold);
text1.setFillColor(sf::Color::Red);
sf::IntRect r1(370, 500, 120, 60);
text1.setPosition(370, 500);
sf::Text text2("Rules", font);
text2.setCharacterSize(60);
text2.setStyle(sf::Text::Underlined);
text2.setStyle(sf::Text::Bold);
text2.setFillColor(sf::Color::Red);
sf::IntRect r2(370, 600, 140, 60);
text2.setPosition(370, 600);
sf::Text text3("Close", font);
text3.setCharacterSize(60);
text3.setStyle(sf::Text::Underlined);
text3.setStyle(sf::Text::Bold);
text3.setFillColor(sf::Color::Red);
sf::IntRect r3(370, 700, 120, 60);
text3.setPosition(370, 700);
sf::Text text4(hg.getGuessedString(), font1);
text4.setCharacterSize(80);
text4.setStyle(sf::Text::Underlined);
text4.setStyle(sf::Text::Bold);
text4.setFillColor(sf::Color::Red);
text4.setPosition(1000, 00);
sf::Text text5("Guess the word", font1);
text5.setCharacterSize(60);
text5.setStyle(sf::Text::Underlined);
text5.setStyle(sf::Text::Bold);
text5.setFillColor(sf::Color::Red);
text5.setPosition(1000, 00);
sf::Text text6("Rules", font);
text6.setCharacterSize(120);
text6.setStyle(sf::Text::Underlined);
text6.setStyle(sf::Text::Bold);
text6.setFillColor(sf::Color::Red);
text6.setPosition(1000, 00);
string s = "Hints will be displayed for the word. Press the letter\n to complete the word to be guessed. If the letter\n you typed is right than it will appear in the string\n of word else the hangman will start to build . \nDifferent sounds of right and wrong typed letters will\n be played . If the user guessed it right than he/she\n will win and loose if all the body parts of hangman\n are displayed and game continues till the word\n is guessed fully . Quit the game by clicking close\n button.";
sf::Text text7(s, font1);
text7.setCharacterSize(30);
text7.setStyle(sf::Text::Underlined);
text7.setStyle(sf::Text::Bold);
text7.setFillColor(sf::Color::Red);
text7.setPosition(1000, 00);
sf::Text text8("Hint: " + hg.hint.substr(0, 58) + "\n" + hg.hint.substr(58, 58), font1);
text8.setCharacterSize(22);
text8.setStyle(sf::Text::Underlined);
text8.setStyle(sf::Text::Bold);
text8.setFillColor(sf::Color::Red);
text8.setPosition(1000, 00);
sf::Text text9("Wins: " + to_string(hg.wins) + "\nLosses: " + to_string(hg.losses), font1);
text9.setCharacterSize(70);
text9.setStyle(sf::Text::Underlined);
text9.setStyle(sf::Text::Bold);
text9.setFillColor(sf::Color::Green);
text9.setPosition(1000, 00);
sf::CircleShape circle(30);
circle.setTexture(&texture);
circle.setOutlineColor(sf::Color::Black);
circle.setOutlineThickness(5);
circle.setPosition(410, 200);
sf::RectangleShape rectangle[20];
rectangle[0].setSize(sf::Vector2f(5, 100));
rectangle[0].setTexture(&texture);
rectangle[0].setOutlineColor(sf::Color::Black);
rectangle[0].setOutlineThickness(5);
rectangle[0].setPosition(437, 270); //437,270
rectangle[1].setSize(sf::Vector2f(5, 50));
rectangle[1].setTexture(&texture);
rectangle[1].setRotation(45);
rectangle[1].setOutlineColor(sf::Color::Black);
rectangle[1].setOutlineThickness(5);
rectangle[1].setPosition(427, 295); //427,295
rectangle[2].setSize(sf::Vector2f(5, 50));
rectangle[2].setTexture(&texture);
rectangle[2].setPosition(427, 295);
rectangle[2].setRotation(315);
rectangle[2].setOutlineColor(sf::Color::Black);
rectangle[2].setOutlineThickness(5);
rectangle[2].setPosition(450, 297); //450,297
rectangle[3].setSize(sf::Vector2f(5, 75));
rectangle[3].setTexture(&texture);
rectangle[3].setRotation(15);
rectangle[3].setOutlineColor(sf::Color::Black);
rectangle[3].setOutlineThickness(5);
rectangle[3].setPosition(427, 365); //427,365
rectangle[4].setSize(sf::Vector2f(5, 75));
rectangle[4].setTexture(&texture);
rectangle[4].setRotation(345);
rectangle[4].setOutlineColor(sf::Color::Black);
rectangle[4].setOutlineThickness(5);
rectangle[4].setPosition(450, 367); //450,367
rectangle[5].setSize(sf::Vector2f(5, 125));
rectangle[5].setTexture(&texture);
rectangle[5].setOutlineColor(sf::Color::Black);
rectangle[5].setOutlineThickness(5);
rectangle[5].setPosition(300, 340); //300,340
rectangle[6].setSize(sf::Vector2f(5, 125));
rectangle[6].setTexture(&texture);
rectangle[6].setOutlineColor(sf::Color::Black);
rectangle[6].setOutlineThickness(5);
rectangle[6].setPosition(300, 210); //300,210
rectangle[7].setSize(sf::Vector2f(5, 125));
rectangle[7].setTexture(&texture);
rectangle[7].setRotation(270);
rectangle[7].setOutlineColor(sf::Color::Black);
rectangle[7].setOutlineThickness(5);
rectangle[7].setPosition(305, 278); //300,277
positions[0][0] = 437;
positions[0][1] = 270;
positions[1][0] = 427;
positions[1][1] = 295;
positions[2][0] = 450;
positions[2][1] = 297;
positions[3][0] = 427;
positions[3][1] = 365;
positions[4][0] = 450;
positions[4][1] = 367;
positions[5][0] = 300;
positions[5][1] = 340;
positions[6][0] = 300;
positions[6][1] = 210;
positions[7][0] = 305;
positions[7][1] = 278;
positions[8][0] = 410;
positions[8][1] = 200;
positions[9][0] = 370;
positions[9][1] = 500;
positions[10][0] = 370;
positions[10][1] = 600;
positions[11][0] = 370;
positions[11][1] = 700;
positions[12][0] = 220;
positions[12][1] = 700;
positions[13][0] = 150;
positions[13][1] = 600;
soundintro.play();
while (window.isOpen())
{
sf::Event event;
while (window.pollEvent(event))
{
if (event.type == sf::Event::Closed)
{
window.close();
}
if (event.type == sf::Event::MouseButtonPressed)
{
if (r1.contains(event.mouseButton.x, event.mouseButton.y))
{
std::cout << "Play Button pressed\n";
c = (rand() % 5);
hg.hint = hints[c];
hg.setCorrectString(words[c]);
hg.setGuessedString();
hg.setTries(0);
text8.setString("Hint: " + hg.hint.substr(0, 58) + "\n" + hg.hint.substr(58, 58));
text4.setString(hg.getGuessedString());
for (int i = 0; i < 40; i++)
{
choice[i] = false;
}
rectangle[0].setPosition(1000, 0);
rectangle[1].setPosition(1000, 0);
rectangle[2].setPosition(1000, 0);
rectangle[3].setPosition(1000, 0);
rectangle[4].setPosition(1000, 0);
rectangle[5].setPosition(1000, 0);
rectangle[6].setPosition(1000, 0);
rectangle[7].setPosition(1000, 0);
circle.setPosition(1000, 0);
text1.setPosition(1000, 0);
r1.left = 1000;
text2.setPosition(1000, 0);
r2.left = 1000;
text3.setPosition(1000, 0);
r3.left = 1000;
text9.setPosition(1000, 0);
text4.setCharacterSize(80);
text5.setString("Guess the Word!");
text6.setPosition(1000, 0);
text7.setPosition(1000, 0);
text4.setPosition(positions[12][0], positions[12][1]);
text5.setPosition(positions[13][0], positions[13][1]);
text8.setPosition(50, 500);
soundintro.pause();
soundcorrect.play();
playing = true;
}
if (r2.contains(event.mouseButton.x, event.mouseButton.y))
{
std::cout << "Rules Button pressed\n";
rectangle[0].setPosition(1000, 0);
rectangle[1].setPosition(1000, 0);
rectangle[2].setPosition(1000, 0);
rectangle[3].setPosition(1000, 0);
rectangle[4].setPosition(1000, 0);
rectangle[5].setPosition(1000, 0);
rectangle[6].setPosition(1000, 0);
rectangle[7].setPosition(1000, 0);
circle.setPosition(1000, 0);
text2.setPosition(1000, 0);
r2.left = 1000;
text3.setPosition(1000, 0);
r3.left = 1000;
text6.setPosition(310, 150);
text7.setPosition(50, 300);
text1.setPosition(370, 650);
r1.top = 650;
sound.play();
}
if (r3.contains(event.mouseButton.x, event.mouseButton.y))
{
std::cout << "Exit Button pressed\n";
sound.play();
window.close();
}
}
if (event.type == sf::Event::TextEntered)
{
if (playing)
{
if (event.text.unicode < 128)
if (isalnum(static_cast<char>(event.text.unicode)))
{
std::cout << "ASCII character typed: " << static_cast<char>(event.text.unicode) << std::endl;
std::cout << "ASCII character typed: " << event.text.unicode << std::endl;
if (choice[toupper(event.text.unicode) - 65] == true)
{
std::cout << "Already guessed\n";
}
else
{
choice[toupper(event.text.unicode) - 65] = true;
if (hg.guessWord(toupper(static_cast<char>(event.text.unicode))))
{
std::cout << "Correct Choice\n";
soundcorrect.play();
text4.setString(hg.getGuessedString());
if (hg.isWon())
{
text5.setString("You Won!!!!");
text4.setCharacterSize(50);
text4.setPosition(120, positions[12][1]);
text4.setString("You guessed \"" + hg.getCorrectString() + "\"");
soundintro.play();
playing = false;
text1.setString("Play Again!");
text1.setPosition(300, 800);
r1.top = 800;
r1.left = 300;
hg.wins++;
cout << "Losses=" << hg.losses << endl;
cout << "Wins=" << hg.wins << endl;
text9.setString("Wins: " + to_string(hg.wins) + "\nLosses: " + to_string(hg.losses));
text9.setPosition(550, 200);
}
}
else
{
soundwrong.play();
std::cout << "Wrong choice\n";
hg.increaseTries();
if (hg.getTries() == 1)
{
rectangle[5].setPosition(positions[5][0], positions[5][1]);
}
if (hg.getTries() == 2)
{
rectangle[6].setPosition(positions[6][0], positions[6][1]);
}
if (hg.getTries() == 3)
{
rectangle[7].setPosition(positions[7][0], positions[7][1]);
}
if (hg.getTries() == 4)
{
circle.setPosition(positions[8][0], positions[8][1]);
}
if (hg.getTries() == 5)
{
rectangle[0].setPosition(positions[0][0], positions[0][1]);
}
if (hg.getTries() == 6)
{
rectangle[1].setPosition(positions[1][0], positions[1][1]);
rectangle[2].setPosition(positions[2][0], positions[2][1]);
}
if (hg.getTries() == 7)
{
rectangle[3].setPosition(positions[3][0], positions[3][1]);
rectangle[4].setPosition(positions[4][0], positions[4][1]);
}
if (hg.isLost())
{
text5.setString("You Lost!!!!");
text4.setCharacterSize(50);
text4.setPosition(120, positions[12][1]);
text4.setString("You didnt guess \"" + hg.getCorrectString() + "\"");
playing = false;
text1.setString("Play Again!");
text1.setPosition(300, 800);
soundintro.play();
r1.top = 800;
r1.left = 300;
hg.losses++;
cout << "Losses=" << hg.losses << endl;
cout << "Wins=" << hg.wins << endl;
text9.setString("Wins: " + to_string(hg.wins) + "\nLosses: " + to_string(hg.losses));
text9.setPosition(550, 200);
}
}
}
}
}
}
}
window.clear();
window.draw(sprite);
window.draw(circle);
window.draw(text);
window.draw(text1);
window.draw(text2);
window.draw(text3);
window.draw(text4);
window.draw(text5);
window.draw(text6);
window.draw(text7);
window.draw(text8);
window.draw(text9);
window.draw(rectangle[0]);
window.draw(rectangle[1]);
window.draw(rectangle[2]);
window.draw(rectangle[3]);
window.draw(rectangle[4]);
window.draw(rectangle[5]);
window.draw(rectangle[6]);
window.draw(rectangle[7]);
window.display();
}
return 0;
}