summaryrefslogtreecommitdiffstats
path: root/bin/mastermind.pl
blob: b3c595ee35845ac1b51613a0566e14161f1a9d89 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#!/usr/bin/env perl
use strict;
use warnings;
use Games::Word qw/random_string_from shared_letters
                   shared_letters_by_position/;
# PODNAME: mastermind.pl

my $word = random_string_from "abcdefg", 5;
while (1) {
    print "Guess? ";
    my $guess = <>;
    chomp $guess;
    last if $guess eq $word;
    my $gears = shared_letters_by_position $guess, $word;
    my $tumblers = shared_letters($guess, $word) - $gears;
    printf "You hear $tumblers tumbler%s and $gears gear%s.\n",
           $tumblers == 1 ? '' : 's',
           $gears    == 1 ? '' : 's';
}
print "You see the drawbridge open.\n";