More specifically, I need help with the logic to figure out if they got (a) stuff in the right place and right color and (b) if they got stuff in the wrong place right color.

I've been working on this for the past few nights (that's probably the problem... I'm quite impatient & frustratable at night), trying 4 or 5 different ways of checking the two patterns against each other, but something always goes wrong... I'll think about it for a while, think I've discovered what went wrong, and adjust code accordingly, only to have another, seemingly identical bug pop up.

Here's the code:

#!/usr/bin/perl -w use strict; # Colors: [y]ellow [b]lue [g]reen [r]ed blac[k] [w]hite my $x; my @correctguesses; my @getridofthese; my $y; my $their_guess; my @their_guess; my $guess_count = 0; my $theyre_incorrect = 1; my $number_completly_right; my $number_correct_color; my @pattern_to_match; my @previous_guesses; my @numbers_to_colors = qw{y b g r k w}; print "\nMastermind!!!\nColors are: [y]ellow [b]lue [g]reen [r]ed blac +[k] [w]hite\n"; for($x=0;$x<=3;$x++) { $pattern_to_match[$x]=$numbers_to_colors[rand 5]; } while ($guess_count <=9 && $theyre_incorrect) { my @newpattern_to_match = @pattern_to_match; $number_completly_right = 0; $number_correct_color = 0; $their_guess = <STDIN>; chomp $their_guess; if ($their_guess eq "showme") { print @pattern_to_match; print "\n"; next; } print "$number_correct_color\n"; $previous_guesses[$guess_count]{"guess"} = $their_guess; $their_guess =~ /([ybgrkw])([ybgrkw])([ybgrkw])([ybgrkw])/; @their_guess = ($1,$2,$3,$4); for ($x=0;$x<=3;$x++) { if ($their_guess[$x] eq $newpattern_to_match[$x]) { $number_completly_right++; @getridofthese = (@getridofthese, $x); @correctguesses = (@correctguesses, $x); next; } } foreach(@correctguesses) { splice(@their_guess,$_,1); } foreach(@getridofthese) { splice(@newpattern_to_match,$_,1); } for($x=0;$x<@their_guess;$x++) { for($y=0;$y<@correctguesses;$y++) { if($their_guess[$x] eq $pattern_to_match[$y]) { $number_correct_color++; splice(@newpattern_to_match,$y,1); } } } $previous_guesses[$guess_count]{"reply"} = "$number_completly_righ +t, $number_correct_color"; print "\nMastermind!!!\nColors are: [y]ellow [b]lue [g]reen [r]ed +blac[k] [w]hite\n"; for ($x=$guess_count;$x>=0;$x--) { print $x . " | " . $previous_guesses[$x]{"guess"} . " | " . $p +revious_guesses[$x]{"reply"} . "\n"; } $theyre_incorrect = 0 if ($number_completly_right == 4); $guess_count++; }

(Yes, yes, I know I should put that stuff in subs.. maybe later)

Now, if something in the logic seems completely stupid, it probably is... like I said, the logic checking is an evolving (although, the way things are going, perhaps that's devolving) beast. If something didn't work, I try to adjust it so it does.

To give you an idea of what happens, here's some sample i/o (stripped of unnecessary stuff):

0 | bbbb | 2, 4

This would indicate that of the four colors I guessed, two were in the right place and right color, and four were in the wrong place but right color. Now, of course, as there are only 4 colors in the pattern I'm trying to figure out, this is not the output I am going for. By way of the showme debugging "cheat code", I can see that, in the above case, the pattern it was matching against was bbrk.

Monks, I stand before you a broken and miserable person. I'm fighting this script, and, unfortuantly, it appears to be winning. Please help.

(ps: on the plus side, I've learned how to use splice while making this :)

--Psi
print(pack("h*","e4f64702566756e60236c6f637560247f602265696e676021602075627c602861636b65627e2")."\n");


In reply to Need help with a Mastermind game by PsionicMan

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



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.