Forked from dbc-challenges/0.2.1-boggle_class_from_methods.rb
Last active
August 29, 2015 13:56
-
-
Save nullet/9223160 to your computer and use it in GitHub Desktop.
phase 0 unit 2 week 1boggle class challenge
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class BoggleBoard | |
def initialize(grid) | |
@grid = grid | |
end | |
def create_word(*coords) | |
coords.map { |coord| @grid[coord.first][coord.last]}.join("") | |
end | |
def get_row(row) | |
@grid[row].join | |
end | |
def get_col(col) | |
@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) | |
p boggle_board.create_word([1,2], [1,1], [2,1], [3,2]) # => "dock" | |
p boggle_board.create_word([2,1], [1,1], [1,2], [0,3]) # => "code" | |
p boggle_board.get_row(3) # => "take" | |
p boggle_board.get_row(0) # => "brae" | |
p boggle_board.get_col(1) # => "roca" | |
p boggle_board.get_col(3) # => "etre" | |
#puts boggle_board[1][1] == 'o' <-- class object? this would work for dice_grid | |
puts boggle_board.get_row(3)[2] == "k" #true | |
puts boggle_board.get_row(1)[3] == 't' #true | |
puts boggle_board.get_col(2)[3] == 'k' #true | |
puts boggle_board.get_col(0)[0] == 'd' #false | |
# I find myself making dumb mistakes. In this exercise, I couldn't overcome an error | |
# for the better part of an hour, and the cause was calling create_word without | |
# boggle_board preceding it. Duh. | |
# OOP seems to make sense in that I can define methods within a strict | |
# context in which I might need them, which seems like it should prove to be | |
# beneficial, but I definitely need more practice to fully grasp its strengths. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment