pendu.pl
Le Pendu, french edition of the schoolboys well known game.
Copyright 2000-2007 Etienne LEMEE <coding AT etilem DOT net>
This piece of code is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
#!/usr/bin/perl -w use strict; my @words = (); my $guess_me = ""; my %seen = (); my $try_again = 1; my $count = 0; my $el_sec -= time; @words = load_words_from(); $guess_me = choose_myst_word_from(\@words); while ($try_again) { print_board($guess_me, \%seen); print "Votre choix : "; my $choice = <STDIN>; chomp $choice; print $guess_me, "\n" if ($choice eq "tell"); $count++; $seen{$choice} = 1; if (found_guess($guess_me, \%seen)) { $try_again = 0; } } $el_sec += time; printf "\nBravo, '%s' trouvé en %d coups et %d secondes !\n", $guess_me, $count, $el_sec; printf "Vous êtes donc %d%% efficace en situation de stress ;-)\n", 10000 / $count / $el_sec; sub found_guess { my $guess = shift; my $seen_ref = shift; while (my $letter = chop $guess) { unless ($seen_ref->{$letter}) { return 0; } } return 1; } sub print_board { my $guess = reverse shift; my $seen_ref = shift; print "\n"; while (my $letter = chop $guess) { if ($seen_ref->{$letter}) { printf "%s", $letter; } else { print "_"; } } print "\n\n"; } sub load_words_from { my @words = (); while (<DATA>) { chomp; push @words, $_; } return @words; } sub choose_myst_word_from { my $list_ref = shift; srand; return $list_ref->[rand(@$list_ref)]; }
__DATA__ a acre acres rest of the DATA only found in the pendu.pl script.