#!/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 = ; @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 not 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 suppliedi: 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 correct 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_right, $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"} . " | " . $previous_guesses[$x]{"reply"} . "\n"; } $theyre_incorrect = 0 if ($number_completly_right == 4); $guess_count++; } #### ----------------------------------- --the good, the bad and the physi-- -----------------------------------