From: Matt Harrison Date: 2008-11-01T07:46:48+09:00 Subject: Re: Ruby Mastermind Game --------------000608010508070301040906 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit Brian Candler wrote: > Lloyd Linklater wrote: >> Well, you know how to get the random number, so that is easy enough. >> You just store the last guess and if the new guess is closer to correct >> than the last one, you are warmer. Continue until they get it right. >> Going to 10k should be guessed in no more than 14 tries. > > I think he was asking about the Mastermind program. > > However it's pretty much the same: > > - make a 4-digit random number between 0000 and 9999 > - in a loop: > - ask for a guess > - finish if the guess is completely right > - count how many digits are correct and in the right place > - count how many digits are correct but in the wrong place > - print the counts > > You have a choice of ways to store the correct number. For example it > could be a 4-digit string: "1234". Then str[0,1] will extract the first > character, and str[3,1] will extract the last character, so you can > iterate easily enough to compare the characters. > > You may find it easier to create a 4-element Array where each element is > an integer from 0 to 9. This is easy to manipulate. However the guess > that the user enters has to be converted into this form too. > > I suggest you code it as far as you can, and put comments in (or dummy > method calls) for the bits where you are stuck. Just out of interest I have created a mastermind game. I'm still learning Ruby (I come from C++ and Perl), so I would love if one of you experts could take a peek and maybe show me how to make it more "Rubyesque". I'm always ready to learn and I'm sure there's vast room for improvement. It might also help the OP, show him how *not* to do it ;) Thanks Matt --------------000608010508070301040906 Content-Type: application/octet-stream; name="rastermind.tar.bz2" Content-Transfer-Encoding: base64 Content-Disposition: attachment; filename="rastermind.tar.bz2" QlpoOTFBWSZTWcO+JoUABH//hN2wAEB5//+34sCeqv/v//4AAQAIUASNntjqmd1u27WqsMJF ETaiT8TTFTaNTegkaaaAAaGTQ0GgD0gymTTQQRIeU9NE9I9QGg9QADajQ0NHqDQJERTJoKej Ke0TapppoAAaNAAAAAHAMIwmmIYBAMgBhGmTJhGAhoIpEmTJkAp4pp4hqPUaAAaAAGgBkfZd fKb2/pc3UnU0Ickkg06dVslBY29jhr2XYlRkcE1AmkJsaQmJQNDybPPw/dxL/p6q6afpnvsj pdhVhVVlR7XwcWYUMJoGO/vBGdAkMH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