Skip to content

Instantly share code, notes, and snippets.

@Dawenster Dawenster/boggle_board.rb Secret forked from dbc-challenges/boggle_board.rb
Last active Dec 14, 2015

Embed
What would you like to do?
class BoggleBoard
attr_accessor :board
attr_reader :all_dice
def initialize
@board = []
@all_dice = ['AAEEGN', 'ELRTTY', 'AOOTTW', 'ABBJOO',
'EHRTVW', 'CIMOTU', 'DISTTY', 'EIOSST',
'DELRVY', 'ACHOPS', 'HIMNQU', 'EEINSU',
'EEGHNW', 'AFFKPS', 'HLNNRZ', 'DEILRX']
end
# Clear the screen
def clear_screen!
print "\e[2J"
end
# Moves cursor to the top left of the terminal
def move_to_home!
print "\e[H"
end
def clear_it_all!
clear_screen!
move_to_home!
end
def set_blank_board
clear_it_all!
4.times do |space|
self.board[space] = '_ '
end
4.times do
puts self.board.join.to_s
end
end
def shake!
clear_it_all!
dice = 0
temp_letters = []
temp_dice = self.all_dice
# uncomment the below for testing purposes - sets a defined boggle board
# self.board[0] = ["A ","B ","C ","D "]
# self.board[1] = ["E ","F ","G ","H "]
# self.board[2] = ["I ","J ","K ","L "]
# self.board[3] = ["M ","N ","O ","P "]
# comment the below for testing purposes - removes random placement of letters
4.times do |row|
self.board[row] = []
4.times do |letter|
temp_letters = temp_dice.sample.split('')
temp_dice.delete(temp_letters.join)
self.board[row][letter] = temp_letters.sample + ' '
self.board[row][letter] = self.board[row][letter].strip + 'u ' if self.board[row][letter] == 'Q '
end
end
to_s
end
def include?(word)
input_letter_array = []
input_letter_coordinates = []
temp_letter_coordinates = []
if word.length >= 3
input_into_array = word.upcase.reverse.split('')
input_into_array.uniq.each do |input_letter|
self.board.each_index do |col_location|
self.board[col_location].each_with_index do |board_letter, row_location|
if board_letter.strip == input_letter
input_letter_array << input_letter
temp_letter_coordinates << col_location
temp_letter_coordinates << row_location
input_letter_coordinates << temp_letter_coordinates
temp_letter_coordinates = []
end
end
end
end
master = {}
master = Hash[input_letter_coordinates.zip(input_letter_array)]
p input_letter_coordinates
p master
end
puts "You found #{word} in the Boggle!" if letters_link?(word, master)
end
def letters_link?(word, master)
linked_array = []
possible_arrays = []
set_of_possible_arrays = []
original_letters = word.upcase.split('')
master.keys.each_with_index do |key1, i1|
master.keys.each_with_index do |key2, i2|
if array_of_possibilities(key1).include?(key2)
possible_arrays << master.keys[i1] << master.keys[i2]
set_of_possible_arrays << possible_arrays.uniq
master.keys.each_with_index do |key3, i3|
if array_of_possibilities(key2).include?(key3)
possible_arrays << master.keys[i2] << master.keys[i3]
set_of_possible_arrays << possible_arrays.uniq
master.keys.each_with_index do |key4, i4|
if array_of_possibilities(key3).include?(key4)
possible_arrays << master.keys[i3] << master.keys[i4]
set_of_possible_arrays << possible_arrays.uniq
master.keys.each_with_index do |key5, i5|
if array_of_possibilities(key4).include?(key5)
possible_arrays << master.keys[i4] << master.keys[i5]
set_of_possible_arrays << possible_arrays.uniq
else
possible_arrays = []
end
end
else
possible_arrays = []
end
end
else
possible_arrays = []
end
end
else
possible_arrays = []
end
end
end
string_matcher = ''
good_match = [0]
set_of_possible_arrays.each do |array|
array.each do |e|
string_matcher += master[e]
end
if string_matcher == word.upcase
good_match << 1
end
p string_matcher
string_matcher = ''
end
good_match.inject { |sum, x| sum + x } > 0
end
def array_of_possibilities(key_array)
check_array = []
check_array << key_array[0] - 1 << key_array[1] - 1
check_array << key_array[0] - 1 << key_array[1]
check_array << key_array[0] - 1 << key_array[1] + 1
check_array << key_array[0] << key_array[1] - 1
check_array << key_array[0] << key_array[1] + 1
check_array << key_array[0] + 1 << key_array[1] - 1
check_array << key_array[0] + 1 << key_array[1]
check_array << key_array[0] + 1 << key_array[1] + 1
check_array = check_array.each_slice(2).to_a
end
def to_s
self.board.each do |e|
puts e.join.to_s
end
end
end
board = BoggleBoard.new
board.set_blank_board
board.shake!
board.include?('aej')
board.include?('eee')
board.include?('aaa')
@Dawenster

This comment has been minimized.

Copy link
Owner Author

Dawenster commented Feb 23, 2013

Not sure why it only works for 3-letter words...

Best guess is that probably because it's a mess...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.