Skip to content

Instantly share code, notes, and snippets.

@joelevering
Last active December 23, 2015 11:49
Show Gist options
  • Save joelevering/6630726 to your computer and use it in GitHub Desktop.
Save joelevering/6630726 to your computer and use it in GitHub Desktop.
require 'pry'
PRINTING_BOARD = <<-STRING.chomp
*| *| *| *
---|---|---|---
*| *| *| *
---|---|---|---
*| *| *| *
---|---|---|---
*| *| *| *
STRING
BOGGLEBOARD = [[' ',' ',' ',' '],
[' ',' ',' ',' '],
[' ',' ',' ',' '],
[' ',' ',' ',' ']]
DICE = ['AAEEGN', 'ELRTTY', 'AOOTTW', 'ABBJOO', 'EHRTVW', 'CIMOTU', 'DISTTY', 'EIOSST',
'DELRVY', 'ACHOPS', 'HIMNQU', 'EEINSU', 'EEGHNW', 'AFFKPS', 'HLNNRZ', 'DEILRX']
class BoggleBoard
def initialize
@board = BOGGLEBOARD
end
def shake!
dice = DICE.dup
@board.each do |row|
row.map! do |cell|
random_dice = dice.sample
dice.delete(random_dice)
rolled_die = random_dice.split('').sample
if rolled_die == 'Q'
'Qu'
else
rolled_die.ljust(2)
end
end
end
end
def print
temp_board = PRINTING_BOARD.dup
@board.flatten.each do |letter|
temp_board.sub!('*', letter)
end
puts temp_board
end
def include?(word)
@board.each_with_index do |row, row_index|
row.each_with_index do |char, column_index|
#binding.pry
#puts "#{row_index}, #{column_index}" if word[0] == char
return true if word.length == 1 && char.strip == word
if word[0] == char.strip
return true if crawl(row_index-1, column_index, word[1..-1]) == true
return true if crawl(row_index+1, column_index, word[1..-1]) == true
return true if crawl(row_index-1, column_index-1, word[1..-1]) == true
return true if crawl(row_index-1, column_index+1, word[1..-1]) == true
return true if crawl(row_index+1, column_index-1, word[1..-1]) == true
return true if crawl(row_index+1, column_index+1, word[1..-1]) == true
return true if crawl(row_index, column_index-1, word[1..-1]) == true
return true if crawl(row_index, column_index+1, word[1..-1]) == true
end
end
end
return false
end
def crawl(row, column, word, board = @board.dup)
if board[row] == nil || board[row][column] == nil
return false
elsif word.length == 1 && board[row][column].strip == word
return true
end
if board[row][column].strip == word[0]
board[row][column] = " "
return true if crawl(row-1, column, word[1..-1], board) == true
return true if crawl(row+1, column, word[1..-1], board) == true
return true if crawl(row-1, column-1, word[1..-1], board) == true
return true if crawl(row-1, column+1, word[1..-1], board) == true
return true if crawl(row+1, column-1, word[1..-1], board) == true
return true if crawl(row+1, column+1, word[1..-1], board) == true
return true if crawl(row, column-1, word[1..-1], board) == true
return true if crawl(row, column+1, word[1..-1], board) == true
end
return false
end
end
board = BoggleBoard.new
board.shake!
board.print
puts "Enter a word to check"
check_word = gets.chomp.upcase
if board.include?(check_word) == true
puts "IT'S DARE"
else
puts ":("
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment