0: My endless quest for diversions caused me to write this
1: Mastermind game knockoff. Maybe should be Monkmind?... <p>
2:
3: Search revealed only one implementation, [68803|here], so I thought
4: I'd offer this alternative. <p>
5:
6: YuckFoo <p>
7:
8: <code>
9: #!/usr/bin/perl
10:
11: use strict;
12:
13: my $DIGITS = 6;
14:
15: print "\nFind the $DIGITS digit number:\n\n";
16: print "* = Right digit, right position.\n";
17: print "+ = Right digit, wrong position.\n";
18:
19: my $code = sprintf("%*.*d", $DIGITS, $DIGITS, int(rand(10**$DIGITS)));
20:
21: my (@trys, $try, $inp, $i);
22:
23: while ($try->{guess} ne $code) {
24:
25: print "\nGuess: ";
26:
27: chomp($inp = <>);
28:
29: $try = {};
30: push (@trys, $try);
31: $try->{guess} = $inp;
32: $try->{score} = score($inp, $code);
33:
34: print "\n";
35: for ($i = 0; $i < @trys; $i++) {
36: $try = $trys[$i];
37: printf (STDOUT "%3d %-*.*s %s\n",
38: $i+1, $DIGITS, $DIGITS, $try->{guess}, $try->{score});
39: }
40: }
41:
42: #-----------------------------------------------------------
43: sub score {
44:
45: my ($gues, $code) = @_;
46:
47: my @codes = split('', $code);
48: my @guess = split('', $gues);
49: my ($str, $i, %codes, @retry);
50:
51: # check for number in right position
52: for ($i = 0; $i < @codes; $i++) {
53: if ($codes[$i] eq $guess[$i]) {
54: $str .= '*';
55: }
56: else {
57: $codes{$codes[$i]}++;
58: push (@retry, $guess[$i]);
59: }
60: }
61:
62: # check for number in wrong position
63: for $i (@retry) {
64: if ($codes{$i}-- > 0) { $str .= '+'; }
65: }
66:
67: return $str;
68: }
69:
70: </code> In reply to Mastermind Game by YuckFoo
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |