0

I'm making a player vs computer Mastermind (the board game) program as part of learning Ruby.

If you don't know Mastermind, basically it's a two-player game where one player creates a code of four colours (i.e. "red red blue yellow") and the opponent tries to guess that code.

The game initializes with a set of available colours and an empty code:

@@colours = ["RED", "BLUE", "YELLOW", "GREEN", "ORANGE", "WHITE"]
@code = []

I can ask the user to set a code like this:

puts "Please set your code:"
code = gets.chomp.downcase.split("")
@code << code

The user inputs the code like so: rgby => code = ["RED", "GREEN", "BLUE", "YELLOW"].

(There will be a method which changes, i.e. "r" to "RED", please assume that happens somewhere in my program.)

I want to make sure the player can only use colours included in @@colours, i.e. pxgb would not work.

Pseudo-code would be along the lines of:

If all input colours are found in @@colours, update @code.

or conversely

If any input colours are not found in @@colours, do not update @code (and possibly do something else).

How do I write this? I'm not too familiar with error handling so I was trying to do it with out but I'm willing to learn if that's the best way to go.

4
  • Are duplicates allowed, e.g. ["RED'", "RED", "GREEN", "GREEN"]? Commented Jun 1, 2016 at 16:04
  • Duplicates are allowed. Commented Jun 1, 2016 at 16:09
  • 1
    @Stefan, what is the wavelength of "RED'"? Commented Jun 1, 2016 at 16:25
  • 1
    @CarySwoveland uhm, well, ' most certainly denotes a complementary color. Yeah that's it, RED' is CYAN. Definitely not a typo ;-) Commented Jun 1, 2016 at 19:17

3 Answers 3

2

You could do the following to check if code has only elements from @@colours

(code - @@colours).empty?

Above expression will be true if code contains only elements from @@colours, else it will be false.


PS: In the code sample in question, code is once shown as containing input from gets and another time shown as an array of processed inputs. In the above solution, I am assuming code = ["RED", "GREEN", "BLUE", "YELLOW"]

Sign up to request clarification or add additional context in comments.

Comments

0

You should have a look at the Set object and subset? function.

http://ruby-doc.org/stdlib-2.3.1/libdoc/set/rdoc/Set.html

You can convert your arrays to Sets by using the to_set function on your array

Comments

0

You could just subtract the allowed elements from the input. If the result isn't empty, then there was an element in the input that is not in the list of allowed values:

allowed = [1,2,3]

input = [1,2]
input - allowed #=> []

input = [1,5]
input - allow   #=> [5]

As a bonus this returns the element that is not allow and can be used in an error message.

1 Comment

it seems very similar with @WandMaker answer

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.