Skip to content

Commit 1ac852f

Browse files
authored
hangman: simplify globals
* Change $tot_letters into a counter from the total number of unique letters still needing to be guessed, down to zero * Remove the need for variable $num_corr * The loop expression is simpler now because it contains 2 variables instead of 3
1 parent 8359313 commit 1ac852f

File tree

1 file changed

+3
-4
lines changed

1 file changed

+3
-4
lines changed

bin/hangman

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ use strict;
1717
srand();
1818
my( $cont ) = "y";
1919
my( %in_word, %guess, @guess, @word, @disp_word );
20-
my( $letter, $word, $x, $num_wrong, $tot_letters, $num_corr );
20+
my( $letter, $word, $x, $num_wrong, $tot_letters );
2121

2222
while( $cont =~ /^y/io ) {
2323
( %in_word, %guess, @disp_word ) = ();
@@ -29,9 +29,8 @@ while( $cont =~ /^y/io ) {
2929
}
3030
$tot_letters = scalar( keys( %in_word ) );
3131
$num_wrong = 0;
32-
$num_corr = 0;
3332
&print_noose( $num_wrong, @disp_word );
34-
while( ($num_wrong < 6) && ($num_corr < $tot_letters) ) {
33+
while( ($num_wrong < 6) && ($tot_letters > 0) ) {
3534
print "\nEnter a letter: ";
3635
$letter = <STDIN>;
3736
exit unless defined $letter;
@@ -43,7 +42,7 @@ while( $cont =~ /^y/io ) {
4342
if(! exists( $in_word{ $letter } ) ) {
4443
$num_wrong++;
4544
} else {
46-
$num_corr++;
45+
$tot_letters--;
4746
for( $x = 0; $x <= $#word; $x++ ) {
4847
if( $word[ $x ] eq $letter ) {
4948
$disp_word[ $x ] = $letter;

0 commit comments

Comments
 (0)