Forum: Ruby Ruby Mastermind Game

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
6bd8ec1c0daf1b5587ea1e0f143ebcc5?d=identicon&s=25 Steve Jones (samwin)
on 2008-10-30 19:44
Hi all,

Im really stuck with a project. I need to create a mastermind program
using ruby. The details are in the following link;

http://cs105.updatelog.org/2008/10/09/lab-8-branch...

It is exercise 2 at the bottom.

Any help or sample code would be great

Cheers!!
84dc575c33a123789521d53cad0f62ae?d=identicon&s=25 Lloyd Linklater (lloyd)
on 2008-10-31 15:41
Steve Jones wrote:
> Hi all,
>
> Im really stuck with a project. I need to create a mastermind program
> using ruby. The details are in the following link;
>
> http://cs105.updatelog.org/2008/10/09/lab-8-branch...
>
> It is exercise 2 at the bottom.
>
> Any help or sample code would be great
>
> Cheers!!

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.
753dcb78b3a3651127665da4bed3c782?d=identicon&s=25 Brian Candler (candlerb)
on 2008-10-31 16:36
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.
289cf19aa581c445915c072bf45c5e25?d=identicon&s=25 Todd Benson (Guest)
on 2008-10-31 16:44
(Received via mailing list)
On Thu, Oct 30, 2008 at 1:43 PM, Steve Jones <ppksam14@hotmail.com>
wrote:
> Hi all,
>
> Im really stuck with a project. I need to create a mastermind program
> using ruby. The details are in the following link;
>
> http://cs105.updatelog.org/2008/10/09/lab-8-branch...
>
> It is exercise 2 at the bottom.
>
> Any help or sample code would be great

You're probably supposed to learn / and % on integers, but you could
use Strings/Arrays too.

Todd
8fa0f5c2bf8ad238baa4e20cb0360ebd?d=identicon&s=25 Matt Harrison (iwasinnamuknow)
on 2008-10-31 23:48
(Received via mailing list)
Attachment: rastermind.tar.bz2 (1 KB)
Brian Candler wrote:
> - make a 4-digit random number between 0000 and 9999
> 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
289cf19aa581c445915c072bf45c5e25?d=identicon&s=25 Todd Benson (Guest)
on 2008-11-01 18:00
(Received via mailing list)
On Thu, Oct 30, 2008 at 1:43 PM, Steve Jones <ppksam14@hotmail.com>
wrote:
> Hi all,
>
> Im really stuck with a project. I need to create a mastermind program
> using ruby. The details are in the following link;
>
> http://cs105.updatelog.org/2008/10/09/lab-8-branch...
>
> It is exercise 2 at the bottom.
>
> Any help or sample code would be great

Can anyone come up with a non-iterative solution (in Ruby code, I
mean)?  I tried using Matrix with no success.

Todd
This topic is locked and can not be replied to.