PsionicMan has asked for the wisdom of the Perl Monks concerning the following question:
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");
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Need help with a Mastermind game
by physi (Friar) on Apr 01, 2001 at 12:29 UTC | |
Re: Need help with a Mastermind game
by physi (Friar) on Apr 01, 2001 at 18:09 UTC | |
by physi (Friar) on Apr 02, 2001 at 20:43 UTC | |
Re: Need help with a Mastermind game
by jepri (Parson) on Apr 01, 2001 at 18:25 UTC | |
by danger (Priest) on Apr 01, 2001 at 22:36 UTC | |
by chipmunk (Parson) on Apr 02, 2001 at 06:30 UTC |