Skip to content

Instantly share code, notes, and snippets.

@andmcgregor
Created June 9, 2013 04:24
Show Gist options
  • Save andmcgregor/5737633 to your computer and use it in GitHub Desktop.
Save andmcgregor/5737633 to your computer and use it in GitHub Desktop.
Brute force boggle
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]]
class BoggleCell
attr_accessor :index, :value, :surrounding
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).start_search(word.length)
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)
@word = word
@board = board.clone # need clone?
@word_letters = word.split("")
@history = []
@counter = 0
#start_search
end
def start_search(length)
results = []
@board.each do |cell|
find_surrounding(cell, [cell.index], length)
end
results.uniq
end
def find_surrounding(cell, trace, length)
if length != 1
length -= 1
cell.surrounding.each do |cell_i|
temp_trace = trace.clone << @board[cell_i].index
find_surrounding(@board[cell_i], temp_trace, length) if !trace.include?(cell_i)
temp_trace = []
end
else
puts "\e[H\e[2J"
puts show_with_trace(trace)
new_word = ""
([email protected]).each do |i|
new_word.concat(@board[trace[i]].value)
end
sleep if @word == new_word
sleep 0.2
end
end
def show_with_trace(trace)
puts
puts "Target word: #{@word}"
puts
acc = 1
@board.each do |cell|
if trace[0] == (cell.index)
print "\033[92m" + cell.value + " " + "\033[0m"
elsif !trace[1].nil? && trace[1] == (cell.index)
print "\033[91m" + cell.value + " " + "\033[0m"
elsif !trace[2].nil? && trace[2] == (cell.index)
print "\033[93m" + cell.value + " " + "\033[0m"
elsif !trace[3].nil? && trace[3] == (cell.index)
print "\033[94m" + cell.value + " " + "\033[0m"
elsif !trace[4].nil? && trace[4] == (cell.index)
print "\033[95m" + cell.value + " " + "\033[0m" if trace[4]
else
print cell.value + " "
end
if acc == 4 || acc == 8 || acc == 12
puts
end
acc += 1
end
new_word = ""
([email protected]).each do |i|
new_word.concat(@board[trace[i]].value)
end
puts
puts "Current word: #{new_word}"
puts
puts "SOLVED!" if @word == new_word
puts
puts "\033[92m" + "Green = First letter" + "\033[0m"
puts "\033[91m" + "Red = Second letter" + "\033[0m"
puts "\033[93m" + "Yellow = Third letter" + "\033[0m"
puts "\033[94m" + "Blue = Fourth letter" + "\033[0m" if !trace[3].nil?
puts "\033[95m" + "Pink = Fifth letter" + "\033[0m" if !trace[4].nil?
end
end
example = BoggleBoard.new
example.shake!
puts example
query = ""
until query.length >= 3 && query.length <= 5
puts "Type a 3-5 letter word:"
query = gets.chomp
end
example.include?(query)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment