Skip to content

Instantly share code, notes, and snippets.

@andmcgregor
Created June 8, 2013 16:25
Show Gist options
  • Save andmcgregor/5735726 to your computer and use it in GitHub Desktop.
Save andmcgregor/5735726 to your computer and use it in GitHub Desktop.
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']]
# [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 = []
get_surrounding_cells
end
def get_surrounding_cells
(-5..5).to_a.each do |x|
@surrounding << @index + x if (@index + x).between?(0,15) unless x == -2 || x == 0 || x == 2
end
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 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
example = BoggleBoard.new
example.shake!
puts example
example.inspect
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment