Last active
January 15, 2017 06:21
-
-
Save grepsedawk/45e5bd008bd12ae858b6388f117e6152 to your computer and use it in GitHub Desktop.
This is a possible solution to a boggle board matrix in ruby.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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