Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save jcohen10/9163611 to your computer and use it in GitHub Desktop.
Save jcohen10/9163611 to your computer and use it in GitHub Desktop.
phase 0 unit 2 week 1boggle class challenge
class BoggleBoard
def initialize(dice_grid)
@dice_grid = dice_grid
end
def create_word(board, *coords)
coords.map { |coord| board[coord.first][coord.last]}.join("")
end
def get_row(row)
print @dice_grid[row].join("") # 'join' to return as a string.
end
def get_col(col)
print @dice_grid.transpose[col].join("")
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(dice_grid, [1, 2], [1, 1], [2, 1], [3, 2]) == "dock" # returns true
puts boggle_board.get_row(0)
puts boggle_board.get_row(1)
puts boggle_board.get_row(2)
puts boggle_board.get_row(3) # returns 'take' - the only real word here
puts boggle_board.get_col(0)
puts boggle_board.get_col(1)
puts boggle_board.get_col(2)
puts boggle_board.get_col(3)
# create driver test code to retrieve a value at a coordinate here:
puts boggle_board.create_word(dice_grid, [3,2]) # returns 'k'
# Reflection
# Object oriented programming feels a bit more organized to me than procedural
# programming - more structured and easier to edit.
# The prior practice with setting up classes and using driver tests helped make this
# one feel like a pretty straightforward exercise.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment