Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Ruby Mastermind Game

Reply
Thread Tools

Ruby Mastermind Game

 
 
Steve Jones
Guest
Posts: n/a
 
      10-30-2008
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/0...t-ii/#more-298

It is exercise 2 at the bottom.

Any help or sample code would be great

Cheers!!
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
 
 
 
Lloyd Linklater
Guest
Posts: n/a
 
      10-31-2008
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/0...t-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.
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
 
 
 
Brian Candler
Guest
Posts: n/a
 
      10-31-2008
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.
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
Todd Benson
Guest
Posts: n/a
 
      10-31-2008
On Thu, Oct 30, 2008 at 1:43 PM, Steve Jones <(E-Mail Removed)> 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/0...t-ii/#more-298
>
> 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

 
Reply With Quote
 
Todd Benson
Guest
Posts: n/a
 
      11-01-2008
On Thu, Oct 30, 2008 at 1:43 PM, Steve Jones <(E-Mail Removed)> 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/0...t-ii/#more-298
>
> 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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
c++ mastermind game console app ImmortalFire C++ 0 06-22-2008 09:23 AM
Mastermind foolsmart2005@gmail.com C++ 2 06-13-2008 05:44 PM
I need help with card game game of war in GUI enviroment judith Java 0 11-01-2006 06:11 AM
Newbie and creating a mastermind-type game in C++ Kraig C++ 6 12-10-2004 01:40 AM
Java Game- Game can't find it's graphics jar file BlackHawke Java 12 01-26-2004 06:14 AM



Advertisments