Skip to content

Instantly share code, notes, and snippets.

@leishman
Created January 31, 2014 04:45
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 leishman/8726741 to your computer and use it in GitHub Desktop.
Save leishman/8726741 to your computer and use it in GitHub Desktop.
# require 'pry'
# You should re-use and modify your old BoggleBoard class
# to support the new requirements
class Cell
attr_accessor :letter, :index, :row, :column, :neighbors, :neighbor_letters, :used
def initialize(letter, index)
@letter = letter
@index = index
@column = index % 4
@row = index/ 4
@neighbors = []
@neighbor_letters = []
@used = false
end
end
class BoggleBoard
def initialize
@board = ['A', 'B', 'C', 'D', 'H', 'U', 'T', 'H', 'L','Q','R','E','A', 'B', 'C', 'D'].each_with_index.map{ |x, i| Cell.new(x, i)}
@i = 0
# @board = Array.new(16, "_").each_with_index.map{ |x, i| Cell.new(x, i) }
@dice = "AAEEGN
ELRTTY
AOOTTW
ABBJOO
EHRTVW
CIMOTU
DISTTY
EIOSST
DELRVY
ACHOPS
HIMNQU
EEINSU
EEGHNW
AFFKPS
HLNNRZ
DEILRX".split.map{ |x| x.split('') }
find_neighbors
end
def find_neighbors
@board.each do |cell|
(@board - [cell]).each do |potential_neighbor|
if (cell.column - potential_neighbor.column).abs <= 1 && (cell.row - potential_neighbor.row).abs <= 1 && #([cell.row, cell.column] != [potential_neighbor.row, potential_neighbor.column])
cell.neighbors << potential_neighbor
cell.neighbor_letters << potential_neighbor.letter
end
end
end
end
def shake!
dice_temp = @dice.map{ |x| x }
@board = @board.each_with_index.map{ |space, index| Cell.new(dice_temp.delete(dice_temp.sample).sample, index) }
find_neighbors
end
def to_s
board_string = ""
@board.each_slice(4).to_a.each do |row|
row.map!{|x| x.letter.gsub("Q","Qu").ljust(3) }
board_string << "#{row.join('')}\n"
end
"#{board_string}\n"
end
def include?(word, index_of_word = 0, index_of_board = 0)
word.gsub!(/qu/,'q')
word_array = word.upcase.split('')
current_letter = word_array[index_of_word]
cell = @board[index_of_board]
# puts "word: #{word} | #{index_of_word} | #{index_of_board} | cell letter: #{cell.letter}| word letter: #{current_letter} | neighbors: #{cell.neighbor_letters}"
# If we have checked all cells or the cell has already been used, return false
if cell == nil || cell.used == true
return false
# Else if the checker has found all letters, return true
elsif index_of_word == word_array.length - 1
return true
# Else
else
# If the cell letter is the current letter to be searched for
if cell.letter == current_letter
@i += 1
cell.used = true
# For each of its neighbors
@board[index_of_board].neighbors.each do |neighbor|
# If the neighbor letters includes the next search letter
if neighbor.letter == word_array[index_of_word + 1]
# Return true if the next cell returns true on include?
if include?(word, index_of_word + 1, @board.index(neighbor))
return true
end
end
end
end
# If the current letter is not the one we are looking for, go to the next cell and reset the current cell to false
cell.used = false
if @i <= 1
return include?(word, index_of_word, index_of_board + 1)
end
end
# If none of the above conditions are met, return false
false
end
end # End of BoggleBoard class
board = BoggleBoard.new
puts board
# board.shake!
# puts board
# An unshaken board prints out something sensible, like:
# ____
# ____
# ____
# ____
# Shake (and modify) the board
p board.include?("hut") == true
board = BoggleBoard.new
p board.include?("abcdt") == true
board = BoggleBoard.new
p board.include?("aba") == false
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment