Skip to content

Instantly share code, notes, and snippets.

@Andrelton
Created May 22, 2015 15:47
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 Andrelton/b0f64c51570ca06b6602 to your computer and use it in GitHub Desktop.
Save Andrelton/b0f64c51570ca06b6602 to your computer and use it in GitHub Desktop.
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