Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save mh120888/7403403 to your computer and use it in GitHub Desktop.
Save mh120888/7403403 to your computer and use it in GitHub Desktop.
phase 0 unit 2 week 1 boggle class challenge
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
def show_all_rows
row = 0
@board.length.times do
puts "Row #{row}: " + get_row(row).join("")
row += 1
end
end
def show_all_cols
col = 0
@board.transpose.length.times do
puts "Column #{col}: " + get_col(col).join("")
col += 1
end
end
def show_all
show_all_rows
show_all_cols
end
def get_letter(row, column)
@board[row][column]
end
end
dice_grid = [["b", "r", "a", "e"],
["i", "o", "d", "t"],
["e", "c", "l", "r"],
["t", "a", "k", "e"]]
boggle = BoggleBoard.new(dice_grid)
=begin
boggle.show_all =
Row 0: brae
Row 1: iodt
Row 2: eclr
Row 3: take
Column 0: biet
Column 1: roca
Column 2: adlk
Column 3: etre
=end
# implement tests for each of the methods here:
boggle = BoggleBoard.new(dice_grid)
p boggle.create_word([1,2],[1,1],[2,1],[3,2]) == "dock"
p boggle.get_row(0) == ["b", "r", "a", "e"]
p boggle.get_row(3) == ["t", "a", "k", "e"]
p boggle.get_col(0) == ["b", "i", "e", "t"]
p boggle.get_col(2) == ["a", "d", "l", "k"]
# create driver test code to retrieve a value at a coordinate here:
p boggle.get_letter(3, 2) == "k"
p boggle.get_letter(0, 0) == "b"
=begin
Creating a BoggleBoard class obviously involves more code, but it is somehow easier to manage conceptually, and I imagine it's
much easier to actually manage the code as well. I'm still unsure as to when it's best to create a separate method for a task
versus including it within another method, or writing the method call in such a way as to get the behavior I want, but I imagine
that understanding will come with time.
I wrote the "show-all" method(s) so that each row and column includes a "row #:" or "column #" string so that it is easier to read
the output.
=end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment