in reply to Need help with a Mastermind game

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-- -----------------------------------

Replies are listed 'Best First'.
Re: Re: Need help with a Mastermind game
by physi (Friar) on Apr 02, 2001 at 20:43 UTC
    Even though there are some other shorter versions, I have to show you my version here, cause I spend a few sleeping-hours on it ;^)
    I use a package for the Color and Position stuff.

    #!/usr/local/bin/perl -w use strict; my $round = 0; my $colors; my @colors; my @result=(); $"="\n"; my $mm = MM->new(); while ($round < 10) { my $result; while (length $colors != 4){ print "Your 4 colors : "; $colors = <STDIN>; chomp $colors; } @colors = split //, $colors; $result = $mm->check_allright(@colors); push @result, "$colors --> $result"; if ($result =~ /4:0/){ print "Yeah, you got it after $round rounds!\n CONGRATULATIONS\n"; exit; } print "@result\n"; $colors=''; $round++; } package MM; sub new { my $self = {}; my $class = shift; bless $self, $class; my @numbers_to_colors = qw{y b g r k w}; for my $i (0..3){ $self->{ORIGINAL}->{$i}=$numbers_to_colors[int rand(6)] } return $self; } sub check_allright { my $self = shift; $self->{guess}->{0} = shift; $self->{guess}->{1} = shift; $self->{guess}->{2} = shift; $self->{guess}->{3} = shift; my $count; $self->{COLOR_GUESS}=(); $self->{COLOR_ORIGINAL}=(); for my $i (0..3){ if ($self->{guess}->{$i} eq $self->{ORIGINAL}->{$i}) { $count++ ; } else { $self->{COLOR_GUESS}->{$self->{guess}->{$i}}++; $self->{COLOR_ORIGINAL}->{$self->{ORIGINAL}->{$i}}++; } } $count ||= 0; my $color= $self->check_color(); return "$count:$color"; } sub check_color { my $self = shift; my $count; map { $count += $self->{COLOR_GUESS}->{$_} < $self->{COLOR_ORIGINAL} +->{$_} ? $self->{COLOR_GUESS}->{$_} : $self->{COLOR_ORIGINAL}->{$_} i +f exists $self->{COLOR_GUESS}->{$_}} keys %{$self->{COLOR_ORIGINAL}}; return $count || 0; } 1;
    And the good thing: It works *g*.
    ----------------------------------- --the good, the bad and the physi-- -----------------------------------