Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save natekandler/8774937 to your computer and use it in GitHub Desktop.
Save natekandler/8774937 to your computer and use it in GitHub Desktop.
Creating a Boggle Board Class
dice_grid = [["b", "r", "a", "e"],
["i", "o", "d", "t"],
["e", "c", "l", "r"],
["t", "a", "k", "e"]]
class BoggleBoard
def initialize(board)
@board = board
end
def create_word(*coords)
coords.map { |coord| @board[coord.first][coord.last]}.join("")
end
def get_row(row)
@board[row]
end
def get_col(col)
@board.transpose[col]
end
=begin
Apologies,
Still working on this mess of a diagonal method
if the the first number of the coordinate is greater than the first number of the second,
choose subtraction
if the first of the coordinate is less the first number of the second,
choose addition
for subtraction take the coordinate (ex. [2,0]) and subtract [1,1] and put the absolute value
of each in an array until the absolute value == the second parameter
for addition add [1,1] and put each value in an array until the first value == the second
def diagonal(a,b)
counter = []
if a[0]>b[0]
puts a[0]
puts b[0]
a = a.map {|i| i-1}
counter << a
b = a.map {|i| i-1}
counter << b
c = b.map {|i| i-1}
counter << c
puts counter.inspect
elsif a[0]<b[0]
puts a[0]
puts b[0]
a = a.map {|i| i+1}
counter << a
b = a.map {|i| i+1}
counter << b
c = b.map {|i| i+1}
counter << c
puts counter.inspect
end
end
puts diagonal([0,0],[3,3])
puts diagonal([2,0],[1,1])
=end
end
end
boggle_board = BoggleBoard.new(dice_grid)
puts boggle_board.create_word([2,1], [1,1], [1,2], [0,3]) #=> returns "code"
puts boggle_board.create_word([0,1], [0,2], [1,2]) #=> creates what california slang word?
puts boggle_board.get_row(1) #=> ["i", "o", "d", "t"]
puts boggle_board.get_col(1) #=> ["r", "o", "c", "a"]
puts boggle_board.diagonal([0,0], [3,3])
# implement tests for each of the methods here:
# create driver test code to retrieve a value at a coordinate here:
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment