[b][red]This message was edited by gorilka at 2004-4-15 9:14:2[/red][/b][hr]
It crashed after I put the comments. No more fun!

Anybody can see the mistake? (:)
1 #!/usr/bin/perl -w
2 use strict;
#to make sure all variables are initialised with my; otherwise it would generate an error
3 use diagnostics;# for more concrete error messages while writing the code
4 # it is actually not needed for execution
5
6
7 sub printResult; # a subfunction declaration, we call it three times and define it at the end
8
#instead of writing the code a few times
9 sub lowercase; # a subfunction to convert the case
10
11
12 my ($letter,
@letters, $display,
@display,$i,
@word, $word_letters, $clue,$name, $mode, $continue,$level ,
13 $wrong_letters, $guess, $correct_guess, $word);
14
15 system ("clear"); # we used a bunch of system("clear")s to make the game look more attractive
16
17 print "
Welcome to Hangman!!!
";
18
19 sleep(2);# and we also used a bunch of sleeps
20
21 print "
Now tell us your name first>> ";
22 $name=; # we did not chomp $name here but then in line 236 we needed quite of a time figuring out why it print ed that unwanted new line before the ! there, eventually we chomped it

23 print "
We will try to hang you now,$name
";
24
25 sleep(3);
26
27 system ("clear");
28
29 print "Brrrrr.....
";
30
31 sleep(3);
32
33 system("clear");
34
35 print "
Well, unless you break free, you will be hanged right after your 10th wrong letter!!! Ha - Ha - Ha!
";
36
37 sleep(5);
38
39 system("clear");
40
41 print "
***************************************************";# we needed to escape the backslash's special characte r meanig here, for the right foot, it actually prints just /

42 print "
* *";
43 print "
* --| Remember!!! You are allowed |-- *";
44 print "
* O only 10 wrong guesses until O *";
45 print "
* _^_ you get hanged!!! _^_ *";
46 print "
* | | *";
47 print "
* / \ / \ *";
48 print "
***************************************************
";
49
50 sleep(6);
51
52
#maybe this will display the menu for single and double player mode, for
53
#quit mode and maybe for giving clues
54
#we had started to code a double player mode where the word and clue would be input by the first user for the second to guess instead of letting the program choose randomly from the files but we abandoned this idea eventually
55
56
57
58 print "
*---------------------------------------------------*
";
59 print "
* Rules here bla bla bla etc.... *
";
60 print "
*---------------------------------------------------*
";
61 print "
* Press p to Play *
";
62 print "
* Press q to Quit *
";
63 print "
*----------------------------------------------------*
";
64
65
66 print "
Your choice:
>>";
67
68
69 $mode=;
70 chomp($mode);
71
72 system("clear");
73
74 if ($mode eq "q" or $mode eq "Q")
#here we decided that we shouldn't take in account the case of q when it is entere d but the user still must be attentive enough cos our system will exit anytime they enter something else inadvertentl y, yet we could've provided some error message giving the player a chance in case he wasn't really meaning to quit bu t he hit the wrong key
75 {exit;}
76
77
78
79
80 else {
81
82
#here is the difficulty level menu, we proud on that especially

83
84 print"
________________________________________________________________________________";
85 print"
|* This is really cool you want us to hang you but please choose your level first!*|";
86 print"
|* Choose a number as indicated below: *|";
87 print"
|* *|";
88 print"
|* --| *|";
89 print"
|* ------O-------> 1.Press 1 for short words up to 4 letters. *|";
90 print"
|* _^_ *|";
91 print"
|* ------|-------> 2.Press 2 for longer words up to 7 letters. *|";
92 print"
|* / \ *|";
93 print"
|* --------------> 3.Press 3 or die hard , for words over 7 letters of length. *|";
94 print"
|*________________________________________________________________________________*|";
95
96 print"
";
97
98 print"
So, which level do you choose? >> ";
99 $level=;
100
101 chomp($level);
102
103
104
105
106
107
108
109
110 my
@lines = "";#these are to initialise arraeys of words and lines to conatin the empty string at first
111 my
@words = "";
112 my $line_count = 0;# to count all the lines and words
113 my $word_count = 0;
114 my $rand_line = 0;#we need this for the random line and word number
115 my $rand_word = 0;
116
117
118 if ($level eq "1"){
119
120
121
122
#open a different file from three for each mode
123
124 open( DICTIONARY, "easy_list" ) or die "Cannot open file: $!
";
125
126
127
128
129
130 }
131
132 elsif ($level eq "2"){
133
134 open( DICTIONARY, "medium_list" ) or die "Cannot open file: $!
";
135
136 }
137 else {
138
139 open( DICTIONARY, "hard_list" ) or die "Cannot open file: $!
";
140
141
142 }
143
144
145
146
147
148
@lines = ;
#copy the file in the
@lines before closing it
149 close( DICTIONARY );
150 $line_count =
@lines;
#the number of lines
151 $rand_line = int(rand $line_count);
#choose a random one from all lines
152
@words = split(/,/, $lines[$rand_line] );
#actually, we do not really need this, because we have no commas and just o ne word at a line, and indeed we have a few things that we do not really make us eof here, but we figured it the hard er way out to make the code reusable in the more general case when we input a simple text file, assigned on execution , or when our easy_list, medium_list and hard_list files were simple text conating files with a certain number of wor ds and commmas on each line;
153
154 $word_count =
@words;
#count words
155 $rand_word = int(rand $word_count);#choose a random number from number of words ...well, although we already got that by simply choosing a random line
156 my $word = $words[$rand_word];
#access the word initialised by that number and store it in $word variable
157
#print "$word";
#this was needed for debugging perposes only

158
159
160
161
162
163
164 $i="";
#this is our analysis program implemented

165
166
167
168 while ( $word =~ m/[aoueiAOUEI]/g ) {
169
170 $i ++;
171
172
173
174 }
175
176
177
#print "$i vowels found
";
178
179
180
181 chomp($word);
182
183 print "
Do you want a clue? If yes, just press 1!
>>";
184
185 $clue=;
186
187 if ( $clue == 1 ) {
188
189
190 print "
CLUE: $i of the letters are vowels here.
This should make it easier, huh?

";
191
192 }
193
194
195 else {
196
197 print "OK, anyway, you missed a chance in a million!
";
198
199 }
200201
202
203 sleep(4);
204
205
206
207 print"
___________________________________________________________________
";
208 print "You will be given 10 sec to read the rules!!! So read quickly!

";
209 print"
___________________________________________________________________
";
210
211
212
213 sleep(4);
214
215
216 system("clear");
217
218
219
220 print "
*****************************************************************************
";
221 print "*Now let's get to the real thing!!! Here is where you can get your word and*
";
222 print "*results. Let me tell you that you can try and guess the whole word too! *
";
223 print "* You will be hanged for wrong letters only, *
";
224 print "* so don't be afraid to guess the whole word!!!

*
";
225 print "******************************************************************************
";
226
227
228
229
230 sleep(10);
231
232 system("clear");
233
234
235 chomp($name);#now here we had to chomp it cos it would print the ! on a new line
236
237
238 print "
Think $name !
";
239
240
241
Comments
242 sleep(4);
243
244
245 system("clear");
246
247 #print "This is the $clue variable. For debbugging purposes only!";
248
249
250
251
252
253
254
255
256 $wrong_letters = 0; #initialise it to 0
257 @letters = ();
258 @word = split(//, $word); #to place the $word string in the @word array which will contain easch letter as single ele ments, this is basically how we split a word into letters:
259 #@chars = split(//, $word);
260
261 #a sentence into words:
262 #@words = split(/ /, $sentence);
263
264 #and a paragraph into sentences:
265 #@sentences = split(/./, $paragraph);
266
267
268 #and then we save the single elements as elements that can be accessed by indexes in some array
269
270
271
272
273
274
275
276
277
278 $display = $word; #we need another variable to hold the word , we will later encrypt that and keep our word in $word variable intact
279 $display =~s/[a-zA-Z]/-/g; #substitute any letters by -
280 @display = split(//, $display); # split $display string into its elements (slashes) and place them in the @displae ar ray
281 $correct_guess = 0; # there are no correct guesses yet
282
282
283
284
285 &printResult; #here we call &printResult subfunction, this is the syntax to call it, it is easier than defining it ag ain anytime we call it , we just defined it at the end
286
287
288
289 while($wrong_letters < 10 and $correct_guess == 0) {
290 my ($index, $correct_letter) = (0,0); #if you still are allowed to say wrong letters and you did not guess the word a t once , $correct_guess == 0 means you did not guessed the whole word at once, otherwise it is set to 1 and the progr am exits saying that you won before that , see line 460
291
292 print "
Letter, please.";
293 print"
Letter:";
294 $letter = ;
295 chomp($letter);
296
297 $letter = lowercase($letter); #in case user entered un uppercase letter
298
299 if (join("", @letters)=~$letter){ #this is an example of how the join function works:
300 #The join function takes a list of values
301 #and glues them together with a glue string between each list element.
302 # It looks like this:
303 #$bigstring = join($glue,@list);
304 #the @letters array keeps track of the letters the user already already said( see line 432), that is each element
305 #in @letters array represents a char the user already attempted to guess, then here in line 299
306 # all of these attempted letters are "glued" by "" and compared if they match the letter that the user
307 #just entered ($letter) and if any of them matches the following statement is printed:
308
309 print"
You already guessed that letter.
";
310 next;
311 }
312 push(@letters, $letter); # here the newly enetered letter is inserted in the array that keeps track of all the
313 #attempted letters already, but of course only if it is a new one, beware the "next;" in line 310
314 #it doesn't hurt affording an example
315 #Like last, next alters the ordinary sequential flow of execution. However, next causes execution to skip past the re st of the innermost enclosing looping block without terminating the block.[2] It is used like this:
316
317 #[2] If there's a continue block for the loop, which we haven't discussed, next goes to the beginning of the continue block rather than the end of the block. Pretty close.
318
319 #while (something) {
320 # firstpart;
321 # firstpart;
322 # firstpart;
323 # if (somecondition) {
324 # somepart;
325 # somepart;
326 # next;
327 # }
328 # otherpart;
329 # otherpart;
330 # next comes here
331 #}
332 #If somecondition is true, then somepart is executed, and otherpart is skipped around.
333
334 #Once again, the block of an if statement doesn't count as a looping block.
335
336 #the examples we found here , by the way
337 foreach(@word) {
338 if (lowercase($_) eq $letter){ #it translates the whole word's letters into lowercase and checks if
339 #any of them matches the letter entered by the user
340 display[$index]=$_; #here it access the exact element of the array called @display that matches the letter
341 # (which contains the little dashes only
342 $correct_letter = 1; #and it sets $correct_letter to 1 so that you automatically go
343 #to line 389 where it will print "Yes, there is such letter!" if this varibale
344 #is not equal to 0, that is , that is why we need to set $correct_letter = 1 here in this case
345 }
346 $index +=1; #we need to increment the index of the array , we don't want it to reveal the same word any time a right letter was guessed
347 }
348 $display = join("", @display);
349 if ($display !~ m/-/) { #if we have to reaveal another letter
350
351
352
353
354
355
356
357
358
359
360 361
362
363
364
365 &printResult; #call print results
366
367
368
369
370
371
372
373
374
375
376 last; #this is to break out of the loop, like the "break" statement in C
377 }
378
379 if ($correct_letter == 0) { # this is NOT executed ONLY IF the user hasn't guessed even a single letter so far
380 # but absolutely anyime they enter a wrong value, even if they got
381 # 2 or 5 correct values, so far, because any time they enter a wrong value it gets back
382 # to 0 (see line 342); we do not have to keep track of the number of right letters,
383 # our variable $correct_letters is always either 0 or 1;
384 $wrong_letters += 1;# yet we need to keep track of the wrong letters since we should not allow them to exceed 10!
385 print "
You guessed wrong! It will get worse soon!";
386 }
387 else {print"
Yes , there is such a letter!!!";}
388
389
390
391
392 &printResult; #call print results instead of taking the time to redifine it and execute cosequentially,
393 #subs make the code reusable and more readable, they also save time and space and TIME!!!
394 #(not sure about money)
395
396
397
398
399
400
401
402 if ($wrong_letters != 10 ) { print "Well....hm"; } #Well, if wrong letters are not 10 we just propose to say "Hm..... ..Well ....."
403 # or anything that is neither good nor bad because if the types a correct letter
404 # it will be cool, and $wrong_letter will not equal 10 , but if they also have no clue
405 # and just hit space or enter waiting to see the whole hangman appear, or if they guess #the same letter for the second time, or who knows, while this condition will still
406 # be true but we can not really predict if the case is good or bad, so we just will say # "Well, hm...", maybe we had rather type a # in front of the whole line 410 and get ri d of it simply, but we thought we'd keep it for the sake of interactiveness and userfriendliness, but surely not for the sake of informativeness ha ha:D (maybe we should write new comments before submitting this, or we should check if the prof really reads all the comments, hm )
407
408
409
410
411
412
413
414
415
416
417
418 else{
419
420
421
422
423 sleep(1);
424
425
426 print"
Sorry, you lose.
"; # tova e yasno ve4e.......
427 print"The word was $word.
"; #i tova, vse pak da znaem koya e dumata, dobre 4e ani se seti da mu kazhem
428 # na gorkiya user kato go besim za kakva tapotiya bilo
429 # ama ako pitate men, ako usera e sestra mi, za neya tryabva specialno edno
430 # print"Aide be, press Enter, nai nakraya!
"; shtoto tya ne se seshta hm......
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450 sub printResult{ #defining the printResult subfunction
451
452
453
454
455
456
457
458
459
460
461
462
463
464 print "
--------------------------------------------------------";
465 print"
Word: $display"; #this is to display the crypted word, as you see it is to be updated frequently
466 print"
Guess list: [".join(",",@letters)."]"; #this is to separate the letters by commas, and then print them in squ are brackets
467 print"
";
468 print"--------------------------------------------------------------";
469 print "
";
470 if ($wrong_letters > 0 && $wrong_letters < 10) {print"
Try another guess!
";} #this is the process of hanging
471 elsif ($wrong_letters == 10) { print"You were hanged!
--|
O
_^_
|
/\
";}
472
473
474
475
476 #and this is a very lazy and inefficient way to write the prints, we had to cut and add spaces after each sample run
477 #and they are still kinda skewed I think ha ha
478 if ( $wrong_letters == 1) { print"-
";}
479 if ( $wrong_letters == 2) { print"-- ";}
479 if ( $wrong_letters == 2) { print"-- ";}
480 if ( $wrong_letters == 3) { print" --| ";}
481 if ( $wrong_letters == 4) { print" --|
O";}
482 if ( $wrong_letters == 5) { print" --|
O
^";}
483 if ( $wrong_letters == 6) { print" --|
O
_^";}
484 if ( $wrong_letters == 7) { print" --|
O
_^_";}
485 if ( $wrong_letters == 8) { print" --|
O
_^_
|";}
486 if ( $wrong_letters == 9) { print" --|
O
_^_
|
/";}
487
488
489
490
491 if($correct_guess != 1 and $wrong_letters < 10){ #well this is to try guess it all
492 print "
Wanna try the whole word?
Try, it doesn't count for a wrong letter!
Or just hit enter if you don't kn ow";
493 print"
Guess:";
494 $guess=;
495 chomp($guess);
496 print"
Well...
"; #and it always prints Well ... of course
497 }
498
499 if (lowercase($guess) eq lowercase($word)) { #this is in case the user guesse the whole word once at a time
500
501 print"
Is it that easy , really? Cos this is it, yeah.
";
502
503 $correct_guess = 1; # and it exits here (juast see above)
504
505
506 }
507
508
509 else {print "
Incorrect guess.
";}
510
511 }
512
513 sub lowercase { #and this is the lowercase defined, though I think we could've just made a simple use of lc, huh? I don't know
514 my $temp = $_[0];
515 $temp =~ tr/[A-Z]/[a-z]/; #and this is really simple
516 return $temp;
517 }
518
519 }}}# they shoul 520 #Now I wonder if it will run again after all this commenting stuff ha ha
521 # half a minute later --> nope, it didn't !
d be three
This is the error message:
[rpy020@linux project]$ ./hangman.pl project
syntax error at ./hangman.pl line 340, near "display["
Execution of ./hangman.pl aborted due to compilation errors (#1)
Uncaught exception from user code:
syntax error at ./hangman.pl line 340, near "display["
Execution of ./hangman.pl aborted due to compilation errors.
A big Hug if you went through all of this ha ha