-
Notifications
You must be signed in to change notification settings - Fork 0
/
pendu.pl
executable file
·132 lines (105 loc) · 2.24 KB
/
pendu.pl
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
#!/usr/bin/env perl
use strict;
use warnings;
my @dictionary = ("civiere", "pansement", "secouriste", "tension", "saturation");
my $randomIndex = int(rand(scalar @dictionary));
my $mysteryWord = $dictionary[$randomIndex];
#print $mysteryWord . "\n";
my $guess = "*" x (length $mysteryWord);
my @charsGuessed = ();
my @charsGuessedWrong = ();
my $lives = 10;
my @lifeState = ('
____
| |
| o
| /|\
| |
| / \
_|_','
____
| |
| o
| /|\
| |
| /
_|_','
____
| |
| o
| /|\
| |
|
_|_','
____
| |
| o
| /|
| |
|
_|_','
____
| |
| o
| |
| |
|
_|_','
____
| |
| o
|
|
|
_|_','
____
| |
|
|
|
|
_|_','
____
|
|
|
|
|
_|_','
|
|
|
|
|
_|_','
_ _');
while ($guess ne $mysteryWord and $lives > 0) {
print "--------------------------------\n";
print "$guess | already tried: { " . join(", ", @charsGuessedWrong) . " }\n";
print "What letter do you think is in the mystery word? [a-z] ";
my $charGuess = <>; chomp $charGuess;
while ((grep {$_ eq $charGuess} @charsGuessed) or $charGuess !~ /\A[a-z]\z/) {
print "The input has to be one letter in the range [a-z] and not in the already guessed letters {" . join(",", @charsGuessed) . "} ";
$charGuess = <>; chomp $charGuess;
}
push(@charsGuessed, $charGuess);
if (index($mysteryWord, $charGuess) != -1) {
my $offset = 0;
my $index = index($mysteryWord, $charGuess);
while($index != -1) {
$guess= substr($guess, 0, $index) . $charGuess . substr($guess, $index+1, length $guess);
$index = index($mysteryWord, $charGuess, $offset);
$offset = $index + 1;
}
print "Good guess!\n";
} else {
push(@charsGuessedWrong, $charGuess);
$lives -= 1;
print "Too bad: @lifeState[$lives]\n";
}
}
if($lives > 0){
print "Well done, the word was indeed $mysteryWord\n";
} else {
print "You lost, the word was $mysteryWord\n";
}