Created
May 22, 2015 15:47
-
-
Save Andrelton/b0f64c51570ca06b6602 to your computer and use it in GitHub Desktop.
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 checker(board, word, last_idx = nil) | |
return true if word.empty? | |
#take letter from front of word | |
letter = word.slice!(0) | |
this_idx = [] | |
# change last letter to "NO" | |
if last_idx | |
board[last_idx[:row]][last_idx[:column]] = "NO" | |
end | |
# check board for letter | |
board.each_with_index do |row, idx| | |
# if this letter is included | |
row.each_with_index do |item, idx2| | |
if item == letter | |
this_idx << { row: idx, column: idx2 } | |
end | |
end | |
end | |
draw board | |
# if the letter was included | |
if this_idx.any? | |
this_idx.each do |idx| | |
# if this letter is less than one space away from last | |
if last_idx == nil || ((last_idx[:row] - idx[:row]).abs <= 1 && | |
(last_idx[:column] - idx[:column]).abs <= 1) | |
# call this method RECURSIVELY | |
# return is needed, WHY?? | |
return checker(board, word, idx) | |
else | |
return false | |
end | |
end | |
# if the letter was not included | |
else | |
return false | |
end | |
end | |
def draw(thing) | |
thing.each { |item| p item } | |
end | |
boardy = [%w(w a k e), %w(q b g m), %w(d e n t), %w(f l o p)] | |
draw(boardy) | |
# p checker(boardy, "emnldb") | |
p checker(boardy, "eqag") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment