Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
    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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others surveying the Monastery: (3)
As of 2024-04-20 09:00 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found