-
Notifications
You must be signed in to change notification settings - Fork 0
/
Source.cpp
397 lines (322 loc) · 6.77 KB
/
Source.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
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
#include "iGraphics.h"
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>
#define MAXATTEMPT 5
int tryleft = MAXATTEMPT;
char word_to_guess[100];
char str[15];
char str1[15];
char c[3];
int len=0;
int easycnt;
int hardcnt;
int wordnumber;
int ispresent[26];
int isguessed[26];
int totword;
int step=0; //Controlling VAriable
int flag=0;
int flag2=0;
int difficulty=0;
int x11=450; //for (iMouse Function)
int x22=250+x11;
int y11=500;
int y22=50+y11;
int cartoon_posX = 210; //Cartoon-Line Variable
int cartoon_posY = 70;
int cartoon_go_up = 32; //cartoon_posY 32 kore barbe
int line_x1 = 256; //Line position x1 y1 fixed thakbe
int line_y1 = 463;
int line_x2 = 256;
int line_y2 = 253;
int line_go_up = 42; //y2 ta 46 kore barbe
//Function_Part
void clearstate(){
int i = 0;
for(i = 0;i<26;++i){
ispresent[i] = 0;
isguessed[i] = 0;
}
}
int cnteasy(){
FILE *fp = fopen("words_easy.txt","r");
char s[100];
int tot = 0;
while(fscanf(fp,"%s",s)!=EOF){
tot++;
}
fclose(fp);
return tot;
}
int cnthard(){
FILE *fp = fopen("words_hard.txt","r");
char s[100];
int tot = 0;
while(fscanf(fp,"%s",s)!=EOF){
tot++;
}
fclose(fp);
return tot;
}
void setword(FILE *fp, int wordnumber){
int cur = 0;
do{
fscanf(fp,"%s",word_to_guess);
cur++;
}while(cur<=wordnumber);
fclose(fp);
}
void setpresence(){
int l = strlen(word_to_guess);
int i;
for(i = 0; i<l; ++i){
ispresent[word_to_guess[i]-'a'] = 1;
}
}
void printpresence(){
int i;
for(i = 0; i<26; ++i){
if(ispresent[i]){
//printf("%c is present\n",i+'a');
}
}
}
int printword(){
int l = strlen(word_to_guess);
int i; int gap = 0;
for(i = 0; i<l; ++i){
if(isguessed[word_to_guess[i]-'a']){
str1[i]=word_to_guess[i];
}
else{
str1[i] = '*';
gap = gap + 1;
}
iSetColor(0, 0, 160);
iText(463, 225, str1, GLUT_BITMAP_TIMES_ROMAN_24);
}
return (gap); //str1 ta iText e print korte hobe
}
void updatestate(char c){
if(ispresent[c-'a']){
printf("Correct Guess\n");
isguessed[c-'a'] = 1;
}
else{
printf("Wrong Guess\n");
tryleft--;
}
//printf("%d chances left\n",*left);
}
void drawRectangle()
{
if(flag==0)
{
iSetColor(0, 0, 0);
iRectangle(450, 500, 250, 50);
}
if(flag==1)
{
iSetColor(255, 13, 37);
iRectangle(450, 500, 250, 50);
if(flag==1)
{
iSetColor(0, 0, 0);
iText(460, 520, str, GLUT_BITMAP_TIMES_ROMAN_24);
}
}
}
void drawRectangle_2()
{
iSetColor(255, 0, 0);
iFilledRectangle(600, 500, 100, 50);
iSetColor(0, 0, 0);
iText(615, 515, "HARD", GLUT_BITMAP_TIMES_ROMAN_24);
iSetColor(41, 52, 180);
iFilledRectangle(450, 500, 100, 50);
iSetColor(255, 255, 255);
iText(465, 515, "EASY", GLUT_BITMAP_TIMES_ROMAN_24);
}
void drawRectangle3()
{
iSetColor(255, 255,255);
iFilledRectangle(450, 115, 250, 350);
}
void iDraw()
{
iClear();
iShowBMP(0, 0, "Background\\background.bmp");
printword();
if(step==0)
{
drawRectangle();
}
if(step==1)
{
iShowBMP(cartoon_posX, cartoon_posY, "Background//hangman.bmp");
iSetColor(0, 0, 0);
iLine(line_x1, line_y1, line_x2, line_y2);
drawRectangle_2();
}
if(step==2)
{
iShowBMP(cartoon_posX, cartoon_posY, "Background//hangman.bmp");
iSetColor(0, 0, 0);
iLine(line_x1, line_y1, line_x2, line_y2);
drawRectangle3;
if(flag2==1)
{
iText(460, 120, "WIN", GLUT_BITMAP_TIMES_ROMAN_24);
}
else
{
iText(460, 120, "LOOSE", GLUT_BITMAP_TIMES_ROMAN_24);
}
}
}
/*
function iMouseMove() is called when the user presses and drags the mouse.
(mx, my) is the position where the mouse pointer is.
*/
void iMouseMove(int mx, int my)
{
}
/*
function iMouse() is called when the user presses/releases the mouse.
(mx, my) is the position where the mouse pointer is.
*/
void iMouse(int button, int state, int mx, int my)
{
if(step==0)
{
if(button == GLUT_LEFT_BUTTON && state == GLUT_DOWN)
{
printf("%d %d\n", mx, my);
if(mx>=x11 && mx<=x22 && my>=y11 && my<=y22)
{
flag=1;
}
else
{
flag=0;
}
}
}
if(step==1)
{
if(button == GLUT_LEFT_BUTTON && state == GLUT_DOWN)
{
if(mx>=450 && mx<=550 && my>=500 && my<=550)
{
difficulty=1;
step=2;
}
if(mx>=600 && mx<=700 && my>=500 && my<=550)
{
difficulty=2;
step=2;
}
}
}
if(step==2)
{
if(difficulty==1)
{
srand(time(0));
FILE *fread;
fread = fopen("words_easy.txt","r");
totword = easycnt;
wordnumber = rand()%totword;
setword(fread,wordnumber);
fclose(fread);
setpresence();
//printpresence();
int gap = printword();
if(gap==0){
flag2=1;
}
else
flag2=0;
//printf("Guess a character: ");
//scanf("%s",c);
//3rd part
updatestate(c[0]);
//3rd part
}
if(difficulty==2)
{
srand(time(0));
FILE *fread;
fread = fopen("words_hard.txt","r");
totword = hardcnt;
wordnumber = rand()%totword;
setword(fread,wordnumber);
fclose(fread);
setpresence();
printpresence();
int gap = printword();
if(gap==0){
flag2=1; //akta flag dhore nibi and sei flag ta idraw te print korb j win
}
else
{
flag2=0;
}
updatestate(c[0]); //j charecter tar input nibi oi string tar nam c db
}
}
}
/*
function iKeyboard() is called whenever the user hits a key in keyboard.
key- holds the ASCII value of the key pressed.
*/
void iKeyboard(unsigned char key)
{
if(step==0)
{
if(flag == 1)
{
if(key== '\r')
{
step=1;
}
else
{
str[len] = key;
len++;
}
}
}
}
/*
function iSpecialKeyboard() is called whenver user hits special keys like-
function keys, home, end, pg up, pg down, arraows etc. you have to use
appropriate constants to detect them. A list is:
GLUT_KEY_F1, GLUT_KEY_F2, GLUT_KEY_F3, GLUT_KEY_F4, GLUT_KEY_F5, GLUT_KEY_F6,
GLUT_KEY_F7, GLUT_KEY_F8, GLUT_KEY_F9, GLUT_KEY_F10, GLUT_KEY_F11, GLUT_KEY_F12,
GLUT_KEY_LEFT, GLUT_KEY_UP, GLUT_KEY_RIGHT, GLUT_KEY_DOWN, GLUT_KEY_PAGE UP,
GLUT_KEY_PAGE DOWN, GLUT_KEY_HOME, GLUT_KEY_END, GLUT_KEY_INSERT
*/
void iSpecialKeyboard(unsigned char key)
{
if(key == GLUT_KEY_END)
{
exit(0);
}
}
int main(){
easycnt = cnteasy();
hardcnt = cnthard();
printf("%d %d\n",easycnt,hardcnt);
//clearstate();
iInitialize(800, 600, "HangMan");
/*if(tryleft==0){
printf("You lose\n");
}
else{
printf("Congratulations! You have beaten the hangman\n");
}*/
return 0;
}