Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save Keopaseutb/8393637 to your computer and use it in GitHub Desktop.
Save Keopaseutb/8393637 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)
p @board[row]
end
def get_col(col)
p @board.transpose[col]
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:
boggle_board.get_row(1) #=> ["i", "o", "d", "t"]
boggle_board.get_row(0) #=> ["b", "r", "a", "e"]
boggle_board.get_row(3) #=> ["t", "a", "k", "e"]
boggle_board.get_col(1) #=> ["r", "o", "c", "a"]
boggle_board.get_col(3) #=> ["e", "t", "r", "e"]
boggle_board.get_col(0) #=> ["b", "i", "e", "t"]
# create driver test code to retrieve a value at a coordinate here:
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]) #=> returns "rad"
# create two more calls to create_word
puts boggle_board.create_word([0,1], [1,1], [2,1], [3,2], [3,3], [2,3]) #=> returns "rocker"
puts boggle_board.create_word([3,0], [3,1], [3,2], [3,3]) #=> returns "take"
# Review/Reflection
# This challenge further help me to understand variable scope. It was important in understanding instnace variable and how
# using them in the class make the method call easier. I found this challenge easier than the "...Nested....Array" challenge.
# Using transpose instead of collect I thought made the column code easier to implement.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment