Ruby Mastermind Game

L

Lloyd Linklater

Steve said:
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-branches-and-loops-part-ii/#more-298

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

Brian Candler

Lloyd said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top