Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save nadiakg/9163341 to your computer and use it in GitHub Desktop.
Save nadiakg/9163341 to your computer and use it in GitHub Desktop.
phase 0 unit 2 week 1boggle class challenge
# In the previous challenge I had to pass an additional parameter for each method - board.
# In this case I am working with classes so I removed the board parameter and created the instance variable
# @board in the initialize method so I can access it anywhere within the BoggleBoard class.
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
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) # this initializes a class BoggleBoard with a nested array dice_grid
# implement tests for each of the methods here:
# I also had to change the test code syntax since we are now working with methods within the class (instance methods)
puts boggle_board.create_word([1,2], [1,1], [2,1], [3,2]) #=> "dock"
puts boggle_board.create_word([0,1], [1,1], [2,1], [3,2]) #=> "rock"
puts boggle_board.create_word([0,1], [0,2], [1,2], [0,2], [0,1]) #=> "radar"
p boggle_board.get_row(0) #=> ["b", "r", "a", "e"]
p boggle_board.get_row(1) #=> ["i", "o", "d", "t"]
p boggle_board.get_row(2) #=> ["e", "c", "l", "r"]
p boggle_board.get_row(3) #=> ["t", "a", "k", "e"]
p boggle_board.get_col(0) #=> ["b", "i", "e", "t"]
p boggle_board.get_col(1) #=> ["r", "o", "c", "a"]
p boggle_board.get_col(2) #=> ["a", "d", "l", "k"]
p boggle_board.get_col(3) #=> ["e", "t", "r", "e"]
# create driver test code to retrieve a value at a coordinate here:
puts boggle_board.create_word([3,2]) #=> k
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment