Skip to content

Instantly share code, notes, and snippets.

@grepsedawk
Last active January 15, 2017 06:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save grepsedawk/45e5bd008bd12ae858b6388f117e6152 to your computer and use it in GitHub Desktop.
Save grepsedawk/45e5bd008bd12ae858b6388f117e6152 to your computer and use it in GitHub Desktop.
This is a possible solution to a boggle board matrix in ruby.
# It should be noted that the OP requested this to
# work in a way that allows for ANY combination of
# words, not just the Boggle Standard rules
def include?(word)
word.upcase!
board.flatten!
word.each_char do |c|
return false unless board.include?(c)
board.delete_at(board.index(c))
end
true
end
def include?(word)
# Make the word upcase so we don't have to worry about incorrect casing in the include
word.upcase!
# flatten the multi dimentional array so we can easily use .include? on it
board.flatten!
# use each_char on our word, which c will give us one character of our word one by one.
# i.e. "test" will give us t, e, s, and t one at a time
word.each_char do |c|
# The next statement is pretty readable as is, but I'll flip it:
# if board doesn't include the character I'm currently iterating,
# kill the loop and return false to the overall function
return false unless board.include?(c)
# We got past the include check!
# Delete *1* of the characters that we "used", since each
# boggle board character can only be used once.
board.delete_at(board.index(c))
end
# Every character provided had a returned value, return true:
true
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment