Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save mbech/8085680 to your computer and use it in GitHub Desktop.
Save mbech/8085680 to your computer and use it in GitHub Desktop.
phase 0 unit 2 week 1 boggle class challenge
class BoggleBoard
attr_reader :board
def initialize(dice_grid)
@board = dice_grid
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(column)
@board.map{|row| row[column]}
end
def get_diagonal(coord)
diagonal = []
if coord[0] == coord[1]
index_counter = 0
while !get_row(index_counter).nil?
diagonal << @board[index_counter][index_counter]
index_counter += 1
end
return diagonal
elsif coord[0] + coord[1] == (board.size - 1)
board.each_with_index do |row, row_index|
row.each_with_index do |col, col_index|
if row_index + col_index == (board.size - 1 )
diagonal << col
end
end
end
return diagonal
else
puts "That coordinate is not on a diagonal."
return diagonal
end
end
end
dice_grid = [["b", "r", "a", "e"],
["i", "o", "d", "t"],
["e", "c", "l", "r"],
["t", "a", "k", "e"]]
boggle_board = BoggleBoard.new(dice_grid)
# implement tests for each of the methods here:
puts boggle_board.create_word([1,2], [1,1], [2,1], [3,2]) == "dock"
puts boggle_board.create_word([2,1], [1,1], [1,2], [0,3]) == "code"
puts boggle_board.create_word([0,1], [0,2], [1,2]) == "rad"
puts boggle_board.create_word([0,0], [1,1], [2,2], [1,2]) == "bold"
puts boggle_board.create_word([1,3], [0,3], [0,2], [0,1]) == "tear"
puts boggle_board.get_row(1) == ["i", "o", "d", "t"]
puts boggle_board.get_row(2) == ["e", "c", "l", "r"]
puts boggle_board.get_col(2) == ["a", "d", "l", "k"]
puts boggle_board.get_col(3) == ["e", "t", "r", "e"]
puts boggle_board.get_diagonal([1,1]) == ["b", "o", "l", "e"]
puts boggle_board.get_diagonal([2,1]) == ["e", "d", "c", "t"]
puts boggle_board.get_diagonal([2,0]) == []
puts "All rows:"
index_counter = 0
while !boggle_board.get_row(index_counter).nil?
puts boggle_board.get_row(index_counter).inspect
index_counter += 1
end
puts "All columns:"
index_counter = 0
while !boggle_board.get_col(index_counter)[0].nil?
puts boggle_board.get_col(index_counter).inspect
index_counter += 1
end
# create driver test code to retrieve a value at a coordinate here:
puts boggle_board.board[3][2] #=> "k"
# While it is a bit more code to create the class versus passing around a board variable,
# it's nice to keep all the board data contained within a particular instance of the class,
# rather than just have it assigned to some variable out in space. The method names
# seem more direct/clear when it's applied using the dot oeprator to the specific board you want.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment