blib/lib/Games/Hangman.pm

filter: Statementstmt 18.1% 39 / 215 Branchbran 0.0% 0 / 98 Conditioncond 0.0% 0 / 26 MC/DCmcdc 0.0% 0 / 14 Subroutinesub 42.1% 8 / 19 Podpod 0.0% 0 / 8 totaltotal 12.3% 47 / 380 CC 63 SCAR 55.5
CC
63
Cov
12%
BUILD
600.0
run
240.0
draw
156.0
CRAP 257.1
↑ Summary
1 package Games::Hangman;
2  
3 1 our $AUTHORITY = 'cpan:PERLANCAR'; # AUTHORITY
4 1 our $DATE = '2020-06-02'; # DATE
5 1 our $DIST = 'Games-Hangman'; # DIST
6 1 our $VERSION = '0.063'; # VERSION
7  
8 #use Color::ANSI::Util qw(ansibg ansifg);
9 1 ▶use Module::List qw(list_modules);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use Module::Load;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use Term::ReadKey;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use Text::WideChar::Util qw(wrap);
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use Time::HiRes qw(sleep);
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 1 ▶use 5.010001;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 1 ▶use Mo qw(build default);
Subroutine BEGIN: called CC=1 SCAR=0.0
17 1 ▶use experimental 'smartmatch';
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 1 has list => (is => 'rw');
20 1 has wl => (is => 'rw');
21 1 has list_type => (is => 'rw'); # either (w)ord or (p)hrase
22 1 has min_len => (is => 'rw');
23 1 has max_len => (is => 'rw');
24 1 has current_word => (is => 'rw');
25 1 has num_words => (is => 'rw', default=>0); # words that have been played
26 1 has num_guessed_words => (is => 'rw', default=>0); # have been guessed correctly
27 1 has guessed_letters => (is => 'rw');
28 1 has num_wrong_letters => (is => 'rw');
29  
30 1 my @pics = (
31 [
32 " ____ ",
33 " | | ",
34 " | ",
35 " | ",
36 " | ",
37 " | ",
38 " _|_ ",
39 "| |______ ",
40 "| |",
41 "|__________|"],
42 [
43 " ____ ",
44 " | | ",
45 " | o ",
46 " | ",
47 " | ",
48 " | ",
49 " _|_ ",
50 "| |______ ",
51 "| |",
52 "|__________|"],
53 [
54 " ____ ",
55 " | | ",
56 " | o ",
57 " | / ",
58 " | ",
59 " | ",
60 " _|_ ",
61 "| |______ ",
62 "| |",
63 "|__________|"],
64 [
65 " ____ ",
66 " | | ",
67 " | o ",
68 " | /| ",
69 " | ",
70 " | ",
71 " _|_ ",
72 "| |______ ",
73 "| |",
74 "|__________|"],
75 [
76 " ____ ",
77 " | | ",
78 " | o ",
79 ' | /|\ ',
80 " | ",
81 " | ",
82 " _|_ ",
83 "| |______ ",
84 "| |",
85 "|__________|"],
86 [
87 " ____ ",
88 " | | ",
89 " | o ",
90 ' | /|\ ',
91 " | | ",
92 " | ",
93 " _|_ ",
94 "| |______ ",
95 "| |",
96 "|__________|"],
97 [
98 " ____ ",
99 " | | ",
100 " | o ",
101 ' | /|\ ',
102 " | | ",
103 " | / ",
104 " _|_ ",
105 "| |______ ",
106 "| |",
107 "|__________|"],
108 [
109 " ____ ",
110 " | | ",
111 " | o ",
112 ' | /|\ ',
113 " | | ",
114 ' | / \ ',
115 " _|_ ",
116 "| |______ ",
117 "| |",
118 "|__________|"],
119 );
120  
121 0 ▶sub _word_term {
Subroutine _word_term: not called CC=2 SCAR=17.9
122 0 my $self = shift;
123 0 ▶ $self->list_type eq 'p' ? 'phrase' : 'word';
Branch: 1 branch
BranchTrueFalseTotal
$self->list_type eq 'p' ? : 0 0 0
124 }
125  
126 0 ▶sub draw {
Subroutine draw: not called CC=12 SCAR=50.5
Pod undocumented
127 0 my ($self, $message1, $message2) = @_;
128 0 state $drawn = 0;
129 0 state $buf = "";
130  
131 # move up to original row position
132 0 ▶ if ($drawn) {
Branch: 1 branch
BranchTrueFalseTotal
if ($drawn) 0 0 0
133 # count the number of newlines
134 0 my $num_nls = 0;
135 0 ▶ $num_nls++ while $buf =~ /\n/g;
Branch: 1 branch
BranchTrueFalseTotal
$buf =~ /\n/g and do { ++$num_nls } 0 0 0
136 0 printf "\e[%dA", $num_nls;
137 }
138  
139 0 $buf = "";
140  
141 # draw the hung man + right pane
142 0 my $pic = $pics[ $self->num_wrong_letters ];
143 0 for my $i (0..@$pic-1) {
144 0 $buf .= $pic->[$i];
145 0 ▶ if ($i == 1) {
Branch: 3 branches
BranchTrueFalseTotal
if ($i == 1) { } 0 0 0
elsif ($i == 2) { } 0 0 0
elsif ($i == 3) { } 0 0 0
146 0 $buf .= sprintf("%-6s #: %4d", ucfirst($self->_word_term),
147 $self->num_words);
148 } elsif ($i == 2) {
149 0 $buf .= sprintf("Guessed : %-26s", $self->guessed_letters);
150 } elsif ($i == 3) {
151 0 my $n = $self->num_words;
152 0 ▶ $buf .= sprintf("Average : %3.0f%%",
Branch: 1 branch
BranchTrueFalseTotal
$n > 1 ? : 0 0 0
153 $n>1 ? $self->num_guessed_words/($n-1)*100.0 : 0);
154 }
155 0 $buf .= "\n";
156 }
157 0 $buf .= "\n";
158  
159 0 my $word = $self->current_word;
160 0 my ($termwidth, $wordwidth);
161 {
162 0 ▶ if (eval "require Term::Size") {
Branch: 1 branch
BranchTrueFalseTotal
if (eval 'require Term::Size') { } 0 0 0
163 0 ($termwidth, undef) = Term::Size::chars(*STDOUT{IO});
164 } else {
165 0 $termwidth = 80;
166 }
167 0 $wordwidth = $termwidth-8;
168 0 $word = wrap($word, $wordwidth);
169 0 $word =~ s/\n/ /g;
170 0 my $guessed = $self->guessed_letters;
171 0 $word =~ s{([A-Za-z])}
172 0 ▶ {my $l = lc($1); index($guessed, $l) >= 0 ? $1 : "-"}egx;
Branch: 1 branch
BranchTrueFalseTotal
index($guessed, $l) != -1 ? : 0 0 0
173 }
174  
175 0 $buf .= sprintf("List : %-30s\n", $self->list);
176 0 $buf .= sprintf("%-6s: %-${wordwidth}s\n",
177 ucfirst($self->_word_term), $word);
178 0 ▶ $buf .= sprintf("Guess : %-60s\n%-60s\n", $message1 // '', $message2 // '');
Condition0%0/4
$message1 // ""
l!l
00
$message2 // ""
l!l
00
MC/DC0%0/1
$message1 // ""
$message1
MC/DC0%0/1
$message2 // ""
$message2
Truth table0%0/2
A
A= $message1
Aresult
11
00
Truth table0%0/2
A
A= $message2
Aresult
11
00
179 0 print $buf;
180 0 $drawn++;
181 }
182  
183 # borrowed from Games::2048
184 0 ▶sub read_key {
Subroutine read_key: not called CC=4 SCAR=30.0
Pod undocumented
185 0 my $self = @_;
186  
187 0 state @keys;
188  
189 0 ▶ if (@keys) {
Branch: 1 branch
BranchTrueFalseTotal
if (@keys) 0 0 0
190 0 return shift @keys;
191 }
192  
193 0 my $char;
194 0 my $packet = '';
195 0 ▶ while (defined($char = ReadKey -1)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($char = ReadKey(-1))) 0 0 0
196 0 $packet .= $char;
197 }
198  
199 0 ▶ while ($packet =~ m(
Branch: 1 branch
BranchTrueFalseTotal
if ($packet =~ / \G( \e \[ # CSI [\x30-\x3f]* # Parameter Bytes [\x20-\x2f]* # Intermediate Bytes [\x40-\x7e] # Final Byte | . # Otherwise just any character ) /gsx) 0 0 0
200 \G(
201 \e \[ # CSI
202 [\x30-\x3f]* # Parameter Bytes
203 [\x20-\x2f]* # Intermediate Bytes
204 [\x40-\x7e] # Final Byte
205 |
206 . # Otherwise just any character
207 )
208 )gsx) {
209 0 push @keys, $1;
210 }
211  
212 0 return shift @keys;
213 }
214  
215 0 ▶sub new_word {
Subroutine new_word: not called CC=9 SCAR=45.0
Pod undocumented
216 0 require Text::Unaccent;
217  
218 0 my $self = shift;
219  
220 0 my $word;
221 PICK:
222 {
223 0 my $tries = 0;
224 0 ▶ while (++$tries < 100) {
Branch: 1 branch
BranchTrueFalseTotal
if (++$tries < 100) 0 0 0
225 0 $word = $self->wl->pick();
226  
227 # deal with empty wordlists
228 0 ▶ last unless defined $word;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $word 0 0 0
229  
230 # for now we deal with ascii only
231 0 ▶ if ($word =~ /[^\x20-\x7f]/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($word =~ /[^\x20-\x7f]/) 0 0 0
232 0 $word = Text::Unaccent::unac_string("utf8", $word);
233 0 ▶ next if $word =~ /[^\x20-\x7f]/;
Branch: 1 branch
BranchTrueFalseTotal
if $word =~ /[^\x20-\x7f]/ 0 0 0
234 }
235  
236 0 ▶ next if $self->min_len && length($word) < $self->min_len;
Branch: 1 branch
BranchTrueFalseTotal
if $self->min_len and length $word < $self->min_len 0 0 0
Condition0%0/3
$self->min_len and length $word < $self->min_len
!ll&&!rl&&r
000
MC/DC0%0/2
$self->min_len and length $word < $self->min_len
$self->min_len length $word < $self->min_len
Truth table0%0/3
A and B
A= $self->min_lenB= length $word < $self->min_len
ABresult
0X0
100
111
237 0 ▶ next if $self->max_len && length($word) > $self->max_len;
Branch: 1 branch
BranchTrueFalseTotal
if $self->max_len and length $word > $self->max_len 0 0 0
Condition0%0/3
$self->max_len and length $word > $self->max_len
!ll&&!rl&&r
000
MC/DC0%0/2
$self->max_len and length $word > $self->max_len
$self->max_len length $word > $self->max_len
Truth table0%0/3
A and B
A= $self->max_lenB= length $word > $self->max_len
ABresult
0X0
100
111
238  
239 # accept
240 0 last PICK;
241 }
242 0 die "Can't find eligible word from list ".$self->list."\n";
243 }
244  
245 0 $self->current_word($word);
246 0 $self->num_words( $self->num_words+1 );
247 0 $self->guessed_letters('');
248 0 $self->num_wrong_letters(0);
249 0 $self->draw;
250 }
251  
252 0 ▶sub BUILD {
Subroutine BUILD: not called CC=24 SCAR=64.0
Pod undocumented
253 0 my $self = shift;
254  
255 # pick word-/phraselist
256 {
257 0 my $wmods = list_modules("WordList::",
258 {list_modules=>1, recurse=>1});
259 0 my @wmods = grep {!/\AWordList::Phrase::/} keys %$wmods;
260 0 s/^WordList::// for @wmods;
261  
262 0 my $pmods = list_modules("WordList::Phrase::",
263 {list_modules=>1, recurse=>1});
264 0 my @pmods = keys %$pmods; s/^WordList::// for @pmods;
265  
266 0 my ($list, $type) = @_;
267 0 ▶ if ($self->list) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->list) { } 0 0 0
268 0 $list = $self->list;
269 0 $list =~ s!/!::!g; # normalize Foo/Bar into Foo::Bar
270 0 ▶ if ($list =~ s/^WordList::Phrase:://) {
Branch: 2 branches
BranchTrueFalseTotal
if ($list =~ s/^WordList::Phrase:://) { } 0 0 0
elsif ($list =~ /^WordList::/) { } 0 0 0
271 0 $type = 'p';
272 } elsif ($list =~ /^WordList::/) {
273 0 $type = 'w';
274 } else {
275 0 $type = '';
276 }
277 0 ▶ if ($type eq 'w') {
Branch: 2 branches
BranchTrueFalseTotal
if ($type eq 'w') { } 0 0 0
elsif ($type eq 'p') { } 0 0 0
278 0 ▶ die "Unknown wordlist '$list'\n" unless $list ~~ @wmods;
Branch: 1 branch
BranchTrueFalseTotal
unless $list ~~ \@wmods 0 0 0
279 } elsif ($type eq 'p') {
280 0 ▶ die "Unknown phraselist '$list'\n" unless $list ~~ @pmods;
Branch: 1 branch
BranchTrueFalseTotal
unless $list ~~ \@pmods 0 0 0
281 } else {
282 0 ▶ if ($list ~~ @wmods) {
Branch: 2 branches
BranchTrueFalseTotal
if ($list ~~ \@wmods) { } 0 0 0
elsif ($list ~~ \@pmods) { } 0 0 0
283 0 $type = 'w';
284 } elsif ($list ~~ @pmods) {
285 0 $type = 'p';
286 } else {
287 0 die "Unknown word-/phraselist '$list'\n";
288 }
289 }
290 } else {
291 0 ▶ $type = rand() > 0.5 ? 'w':'p';
Branch: 1 branch
BranchTrueFalseTotal
rand() > 0.5 ? : 0 0 0
292 0 ▶ if ($type eq 'w') {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq 'w') { } 0 0 0
293 0 ▶ if (($ENV{LANG} // "") =~ /^id/ && "ID::KBBI" ~~ @wmods) {
Branch: 1 branch
BranchTrueFalseTotal
if (($ENV{'LANG'} // '') =~ /^id/ and 'ID::KBBI' ~~ \@wmods) { } 0 0 0
Condition0%0/5
($ENV{"LANG"} // "") =~ /^id/ and "ID::KBBI" ~~ \@wmods
!ll&&!rl&&r
000
$ENV{"LANG"} // ""
l!l
00
MC/DC0%0/2
($ENV{"LANG"} // "") =~ /^id/ and "ID::KBBI" ~~ \@wmods
$ENV{"LANG"} "ID::KBBI" ~~ \@wmods
Truth table0%0/3
A and B
A= $ENV{"LANG"}B= "ID::KBBI" ~~ \@wmods
ABresult
0X0
100
111
294 0 $list = "ID::KBBI";
295 } else {
296 0 ▶ if (@wmods > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@wmods > 1) 0 0 0
297 0 @wmods = grep {$_ ne 'ID::KBBI'} @wmods;
298 }
299 0 $list = $wmods[rand @wmods];
300 }
301 } else {
302 0 ▶ if (($ENV{LANG} // "") =~ /^id/ && "Phrase::ID::Proverb::KBBI" ~~ @pmods) {
Branch: 1 branch
BranchTrueFalseTotal
if (($ENV{'LANG'} // '') =~ /^id/ and 'Phrase::ID::Proverb::KBBI' ~~ \@pmods) { } 0 0 0
Condition0%0/5
($ENV{"LANG"} // "") =~ /^id/ and "Phrase::ID::Proverb::KBBI" ~~ \@pmods
!ll&&!rl&&r
000
$ENV{"LANG"} // ""
l!l
00
MC/DC0%0/2
($ENV{"LANG"} // "") =~ /^id/ and "Phrase::ID::Proverb::KBBI" ~~ \@pmods
$ENV{"LANG"} "Phrase::ID::Proverb::KBBI" ~~ \@pmods
Truth table0%0/3
A and B
A= $ENV{"LANG"}B= "Phrase::ID::Proverb::KBBI" ~~ \@pmods
ABresult
0X0
100
111
303 0 $list = "Phrase::ID::Proverb::KBBI";
304 } else {
305 0 ▶ if (@pmods > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@pmods > 1) 0 0 0
306 0 @pmods = grep {$_ ne 'Phrase::ID::Proverb::KBBI'} @pmods;
307 }
308 0 $list = $pmods[rand @pmods];
309 }
310 }
311 }
312 0 my $mod = "WordList::$list";
313 0 load $mod;
314 0 $self->list_type($type);
315 0 $self->list($list);
316 0 ▶ if (!defined($self->min_len)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->min_len) 0 0 0
317 0 ▶ $self->min_len($type eq 'w' ? 5 : 0);
Branch: 1 branch
BranchTrueFalseTotal
$type eq 'w' ? : 0 0 0
318 }
319 0 $self->wl($mod->new);
320 }
321 }
322  
323 0 ▶sub init {
Subroutine init: not called CC=1 SCAR=6.9
Pod undocumented
324 0 my $self = shift;
325 0 ▶ $SIG{INT} = sub { $self->cleanup; exit 1 };
Subroutine __ANON__: not called CC=1 SCAR=6.9
326 0 ▶ $SIG{__DIE__} = sub { warn shift; $self->cleanup; exit 1 };
Subroutine __ANON__: not called CC=1 SCAR=6.9
327 0 ReadMode "cbreak";
328  
329 # pick color depth
330 #if ($ENV{KONSOLE_DBUS_SERVICE}) {
331 # $ENV{COLOR_DEPTH} //= 2**24;
332 #} else {
333 # $ENV{COLOR_DEPTH} //= 16;
334 #}
335 }
336  
337 0 ▶sub cleanup {
Subroutine cleanup: not called CC=1 SCAR=6.9
Pod undocumented
338 0 my $self = shift;
339 0 ReadMode "normal";
340 }
341  
342 0 ▶sub word_guessed {
Subroutine word_guessed: not called CC=3 SCAR=24.8
Pod undocumented
343 0 my $self = shift;
344 0 my $word = $self->current_word;
345 0 my $guessed = $self->guessed_letters;
346 0 ▶ while ($word =~ /([A-Za-z])/g) {
Branch: 1 branch
BranchTrueFalseTotal
if ($word =~ /([A-Za-z])/g) 0 0 0
347 0 my $l = lc($1);
348 0 ▶ if (index($guessed, $l) < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (index($guessed, $l) == -1) 0 0 0
349 0 return 0;
350 }
351 }
352 0 1;
353 }
354  
355 0 ▶sub run {
Subroutine run: not called CC=15 SCAR=54.8
Pod undocumented
356 0 my $self = shift;
357  
358 0 $self->init;
359 WORD:
360 0 while (1) {
361 0 $self->new_word;
362 0 $self->draw;
363 KEY:
364 0 while (1) {
365 0 my $key = $self->read_key;
366 0 ▶ if (!defined($key)) {
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $key) { } 0 0 0
elsif ($key =~ /\A[A-Za-z]\z/) { } 0 0 0
367 0 sleep 0.1;
368 0 next KEY;
369 } elsif ($key =~ /\A[A-Za-z]\z/) {
370 0 my $guessed = $self->guessed_letters;
371 0 my $l = lc $key;
372 0 ▶ if (index($guessed, $l) >= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (index($guessed, $l) != -1) 0 0 0
373 0 $self->draw("You already guess letter '$l'");
374 0 next KEY;
375 }
376 0 my $word = lc($self->current_word);
377 0 $self->guessed_letters(join("", sort(split('',$guessed),$l)));
378 0 ▶ if (index($word, $l) >= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (index($word, $l) != -1) { } 0 0 0
379 # correct letter
380 0 ▶ if ($self->word_guessed) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->word_guessed) { } 0 0 0
381 0 $self->draw("Correct! Press q to quit, or Space ".
382 "for the next word");
383 0 $self->num_guessed_words( $self->num_guessed_words+1 );
384 0 my $key;
385 0 while (1) {
386 0 $key = $self->read_key(1);
387 0 ▶ if (!defined($key)) {
Branch: 3 branches
BranchTrueFalseTotal
if (not defined $key) { } 0 0 0
elsif ($key eq 'q' or $key eq 'Q') { } 0 0 0
elsif ($key eq ' ') { } 0 0 0
388 0 sleep 0.1; next;
389 ▶ } elsif ($key eq 'q' || $key eq 'Q') {
Condition0%0/3
$key eq "q" or $key eq "Q"
l!l&&r!l&&!r
000
MC/DC0%0/2
$key eq "q" or $key eq "Q"
$key eq "q" $key eq "Q"
Truth table0%0/3
A or B
A= $key eq "q"B= $key eq "Q"
ABresult
1X1
011
000
390 0 last WORD;
391 } elsif ($key eq ' ') {
392 0 next WORD;
393 }
394 }
395 } else {
396 0 $self->draw;
397 }
398 } else {
399 # wrong letter
400 0 $self->num_wrong_letters($self->num_wrong_letters+1);
401 0 ▶ if ($self->num_wrong_letters >= 7) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->num_wrong_letters >= 7) { } 0 0 0
402 0 $self->draw(
403 substr("Sorry, the " . $self->_word_term .
404 " is: " . $self->current_word,
405 0, 60),
406 "Press q to quit, or Space for the next word",
407 );
408 0 while (1) {
409 0 $key = $self->read_key(1);
410 0 ▶ if (!defined($key)) {
Branch: 3 branches
BranchTrueFalseTotal
if (not defined $key) { } 0 0 0
elsif ($key eq 'q' or $key eq 'Q') { } 0 0 0
elsif ($key eq ' ') { } 0 0 0
411 0 sleep 0.1; next;
412 ▶ } elsif ($key eq 'q' || $key eq 'Q') {
Condition0%0/3
$key eq "q" or $key eq "Q"
l!l&&r!l&&!r
000
MC/DC0%0/2
$key eq "q" or $key eq "Q"
$key eq "q" $key eq "Q"
Truth table0%0/3
A or B
A= $key eq "q"B= $key eq "Q"
ABresult
1X1
011
000
413 0 last WORD;
414 } elsif ($key eq ' ') {
415 0 next WORD;
416 }
417 }
418 } else {
419 0 $self->draw;
420 }
421 }
422 } else {
423 0 $self->draw("Not a valid guess");
424 }
425 }
426 }
427 0 $self->cleanup;
428 }
429  
430 1 1;
431 # ABSTRACT: A text-based hangman
432  
433 __END__
↑ Summary