PUH, what a debug session :-)

here's my anotated code, with all the changes I've made.

#!/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 bl +ac +[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>; @correctguesses=(); ### THIS MUST BE CLEARED HERE !!!! 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); ##### search for correct positons and colors !!! for ($x=0;$x<=3;$x++) { if ($their_guess[$x] eq $newpattern_to_match[$x]) { $number_completly_right++; @getridofthese = (@getridofthese, $x); # +## you can say push @getridofthese, $x @correctguesses = (@correctguesses, $x); # +## see above next; # +## there's nothing more after that, so no next is needed } } foreach(@correctguesses) { splice(@their_guess,$_,1); # +## Ok, kill that entrys, which allready fits the corect place & color + option } foreach(@getridofthese) { splice(@newpattern_to_match,$_,1); # +## The same for @newpattern_to_match } ###### Now in @newpattern_to_match only those colors exists which ar n +ot in the right position !!! #### Check for right colors here #### Here is the bug !!!! #### What are you going to do here ? #### in @their_guess are still ALL the letters which the user supplied +i: makes $x going from 0..3 #### $y goes from 0..(theNumberofcorrectPositionsFound) #### AHHH you maybe mean that one: my @notcorrectguesses = (0,1,2,3); for $y (@correctguesses) { splice(@notcorrectguesses,$_,1); } ### Now @notcorrectguesses contains only that indices which got no cor +rect position&color for($x=0;$x<@their_guess;$x++) { # for($y=0;$y<@correctguesses;$y++) { for $y (@notcorrectguesses){ 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_ri +ght, $number_correct_color"; print "\nMastermind!!!\nColors are: [y]ellow [b]lue [g]reen [r]e +d +blac[k] [w]hite\n"; for ($x=$guess_count;$x>=0;$x--) { print $x . " | " . $previous_guesses[$x]{"guess"} . " | " . +$previous_guesses[$x]{"reply"} . "\n"; } $theyre_incorrect = 0 if ($number_completly_right == 4); $guess_count++; }
I hope this works. It looked good in the first few tests.
The rest is your task ;-)
----------------------------------- --the good, the bad and the physi-- -----------------------------------

In reply to Re: Need help with a Mastermind game by physi
in thread 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.