Skip to content

Instantly share code, notes, and snippets.

@andmcgregor
Created June 8, 2013 17:41
Show Gist options
  • Save andmcgregor/5735991 to your computer and use it in GitHub Desktop.
Save andmcgregor/5735991 to your computer and use it in GitHub Desktop.
DICE = [['A','A','E','E','G','N'],
['E','L','R','T','T','Y'],
['A','O','O','T','T','W'],
['A','B','B','J','O','O'],
['E','H','R','T','V','W'],
['C','I','M','O','T','U'],
['D','I','S','T','T','Y'],
['E','I','O','S','S','T'],
['D','E','L','R','V','Y'],
['A','C','H','O','P','S'],
['H','I','M','N','Q','U'],
['E','E','I','N','S','U'],
['E','E','G','H','N','W'],
['A','F','F','K','P','S'],
['H','L','N','N','R','Z'],
['D','E','I','L','R','X']]
SURROUNDING_INDEXES = [[1,4,5],[0,2,4,5,6],[1,3,5,6,7],[2,6,7],[0,1,5,8,9],[0,1,2,4,6,8,9,10],[1,2,3,5,7,9,10,11],[2,3,6,10,11],[4,5,9,12,13],[4,5,6,8,10,12,13,14],[5,6,7,9,11,13,14,15], [6,7,10,14,15],[8,9,13],[8,9,10,12,14],[9,10,11,13,15],[10,11,14]]
# [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15]
# 0 1 2 3
# 4 5 6 7
# 8 9 10 11
# 12 13 14 15
# Surrounding 0: 1,4,5
# Surrounding 1: 0,2,4,5,6
# ..
# Surrounding 5: 0,1,2, 4,6, 8,9,10
# index - 5 TL
# index - 4 TM
# index - 3 TR
# index - 1 left
# index + 1 right
# index + 3 BL
# index + 4 BM
# index + 5 BR
class BoggleCell
attr_accessor :index, :value
def initialize(index)
@index = index
@value = nil
@surrounding = SURROUNDING_INDEXES[index]
end
end
class BoggleBoard
def initialize
@board = (0..15).to_a.map {|i| BoggleCell.new(i) }
end
def shake!
shuffled_dice = DICE.clone.shuffle.map {|row| row.shuffle }
shuffled_dice.map! {|row| row[0] }
@board.map {|cell| cell.value = shuffled_dice[cell.index] }
end
def include?(word)
BoggleSearch.new(word.upcase, @board)
end
def to_s
board = @board.clone
str = ""
4.times do
board.shift(4).each {|cell| str += cell.value + " "}
str += "\n"
end
str
end
def inspect
@board.each do |cell|
p cell
end
end
end
class BoggleSearch
def initialize(word, board)
@board = board.clone # need clone?
@word_letters = word.split("")
start_search
end
def start_search
@board.each do |cell|
if cell == @word_letters[0] #found match
follow_path(cell, @word_letters)
end
end
end
def follow_path(cell, word_letters)
puts "Cell: #{cell}, Word letters: #{word_letters}"
end
end
example = BoggleBoard.new
example.shake!
puts example
example.inspect
example.include?("string")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment