Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
SOLO CHALLENGE: Create A Boggle Board Class Challenge
#1) Need to make the boggle board code from the previous challenge into a class
#2) Need to call the class's methods from outside the class (using the dice_grid given)
#3) Need to print out all row and columns as strings
#4) Need to create a method, individual coordinates, that returns the letter at a given
#spot when the user inputs the coordinates
#5) Bonus: need to create a get_diagonal method
##A diagonal starts at a given point, then goes either up or down a row, then goes
#left or right one space. For the dice_board array, if I input the letters "b" and "e", the
#pattern will go like this: "b", down one row and one column to the right to get "o", down
#one row and one column to the right to get "l", and down one row and one column to the right
#to get "e". Numbers: (0,0) - (1,1) - (2,2) - (3,3)
#To get the "rdr" diagonal: (0,1) - (1,2) - (2,3)
#In both cases, the row and column numbers each increase by one
class BoggleBoard
def initialize(board)
@board = board
def create_word(*coords) { |coord| @board[coord.first][coord.last]}.join("")
def get_row(row)
def get_col(col)
new_column = ""
@board.each do |row|
new_column << row[col]
def get_diagonal(coord1, coord2)
row_start = coord1.first
row_end = coord2.first
col_start = coord1.last
col_end = coord2.last
if (row_end - row_start).abs !=
(col_end - col_start).abs
raise"Not a valid diagonal.")
row_end < row_start ? row_i = -1 : row_i = 1
col_end < col_start ? col_i = -1 : col_i = 1
diagonal = []
x = row_start
y = col_start
until x == row_end + row_i
letter = @board[x][y]
diagonal << letter
x += row_i
y += col_i
dice_grid = [["b", "r", "a", "e"],
["i", "o", "d", "t"],
["e", "c", "l", "r"],
["t", "a", "k", "e"]]
boggle_board =
puts boggle_board.create_word([1,2], [1,1], [2,1], [3,2]) #=>returns "dock"
p boggle_board.get_row(0).to_s #=>returns "brae"
p boggle_board.get_row(1).to_s #=>returns "iodt"
p boggle_board.get_row(2).to_s #=>returns "eclr"
p boggle_board.get_row(3).to_s #=>returns "take"
p boggle_board.get_col(0).to_s #=>returns "biet"
p boggle_board.get_col(1).to_s #=>returns "roca"
p boggle_board.get_col(2).to_s #=>returns "adlk"
p boggle_board.get_col(3).to_s #=>returns "etre"
p boggle_board.get_row(3)[2] #=>returns "k"
p boggle_board.get_col(2)[3] #=>returns "k"
p boggle_board.get_diagonal([0,0], [3,3]) #=>returns "bole"
p boggle_board.get_diagonal([0,2], [1,3]) #=>returns "at"
p boggle_board.get_diagonal([1,1], [1,3]) #=>invalid diagonal, raises argument error
#Review and Reflect
#You just made a transition from procedural programming to object-oriented programming!
#How is the implementation different? What are the benefits to using the Object Oriented
#approach (even if it is a bit more code?)
###You call the methods by typing the object name + . + the method name
###By creating a new instance, the object has access to all of the class' methods
###without having to retype them
###Using the OO approach is nice because you can store all of the potential methods you
#would use for a certain type of object in one place
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.