I couldn't quite follow what you were up to there (lack of doco) so I just went ahead and wrote my own routine. My approach is not ideal, so it someone would care to golf this into better form I would appreciate it.

If I may be so bold as to offer some advice, your lack of documentation (and certain comments you make) indicates that you did not flow-chart or otherwise plan out the parts of this code. You should know your algorithm works before you try to implement it. Try to avoid mixing the two, because your mind can lose track switching between the two contexts.

You probably just 'wanted to dive into the problem', but I assure you that coding and working out algorithms are two completely different things. Coding should be the process of implementing the algorithm.

'Logic errors' are usually a result of not having an algorithm worked out beforehand. You hope that you can wing it, and often you can. But if you can't you are often left with a lot of trouble.

In these cases you are usually better off chucking the code out and then doing it properly. Trying to debug it can take hours to spot the one subtle bug that is skewing the results.

On the up side, ++ for the warnings, strict and good variable names

#!/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_completely_right; my $number_correct_colour; my @pattern_to_match; my @previous_guesses; my @numbers_to_colours = qw{y b g r k w}; print "\nMastermind!!!\nColors are: [y]ellow [b]lue [g]reen [r]ed blac +[k] [w]hite\n"; print "Please guess four pegs\n\n"; #Choose our hidden pegs foreach (0..3) { $pattern_to_match[$_]=$numbers_to_colours[rand 5]; } my $pattern_to_match=join "", @pattern_to_match; #Main loop while ($guess_count <=9 ) { #Set our default variables my @new_pattern_to_match = @pattern_to_match; my $new_pattern_to_match = join "", @pattern_to_match; $number_completely_right = 0; $number_correct_colour = 0; $their_guess = <STDIN>; chomp $their_guess; #Cheat. Print out answer if they type 'showme' if ($their_guess eq "showme") { print @pattern_to_match; print "\n"; next; } #Check for a proper input an complain if we don't get it. do{print "Incorrect guess. Please enter four letters as described ab +ove\n"; next; } unless $their_guess=~ /^([ybgrkw])([ybgrkw])([ybgrkw])([ybgrkw])$/; #Push their guess onto the guess stack $previous_guesses[$guess_count]{guess} = $their_guess; #Check to see if any character of their guess occurs in the correct #answer string. If it does, remove it from the answer string and #move onto the next character of their guess. @their_guess = split //,$their_guess; foreach my $x (@their_guess) { my $z=index($new_pattern_to_match,$x); if (($z+1)) { substr $new_pattern_to_match,$z,1," "; $number_correct_colour++; } } #Now check for each character in it's right place. If it is there, #decrement the right colour, wrong place counter do{$number_completely_right++;$number_correct_colour--;} if $pattern_ +to_match=~ /$their_guess[0].../; do{$number_completely_right++;$number_correct_colour--;} if $pattern_ +to_match=~ /.$their_guess[1]../; do{$number_completely_right++;$number_correct_colour--;} if $p +attern_to_match=~ /..$their_guess[2]./; do{$number_completely_right++;$number_correct_colour--;} if $p +attern_to_match=~ /...$their_guess[3]/; $previous_guesses[$guess_count]{"reply"} = "$number_completely +_right, $number_correct_colour"; 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"; } do{print "Yay! You win!\n\n\n";exit;} if ($number_completely_ +right == 4); $guess_count++; } print "You lost.\n\n\n";

____________________
Jeremy
I didn't believe in evil until I dated it.


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