Forked from dbc-challenges/0.2.1-boggle_class_from_methods.rb
Last active
December 31, 2015 16:39
-
-
Save gsantikian/8015145 to your computer and use it in GitHub Desktop.
phase 0 unit 2 week 1
boggle 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 | |
attr_reader :board | |
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.map {|row| row[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: | |
puts boggle_board.create_word([1,2], [1,1], [2,1], [3,2]) #=> returns "dock" | |
p boggle_board.get_row(1) #=> returns ["i", "o", "d", "t"] | |
p boggle_board.get_col(2) #=> returns ["a", "d", "l", "k"] | |
puts boggle_board.get_row(0).join #=> "brae" | |
puts boggle_board.get_row(1).join #=> "iodt" | |
puts boggle_board.get_row(2).join #=> "eclr" | |
puts boggle_board.get_row(3).join #=> "take" a real word! | |
puts boggle_board.get_col(0).join #=> "biet" | |
puts boggle_board.get_col(1).join #=> "roca" | |
puts boggle_board.get_col(2).join #=> "adlk" | |
puts boggle_board.get_col(3).join #=> "etre" | |
# create driver test code to retrieve a value at a coordinate here: | |
puts boggle_board.board[3][2] #=> "k" | |
# This OO implementation is, I think, more easy to follow than the procedural version. Since, the object carries along | |
# its state and methods, I think calling the methods seems more intuitive. Also, we don't have to include the local variable | |
# thats addressed to the 2d array within each method. With the classs we initialize the BoggleBoard object with the array and | |
# the array just carries along that data which all the methods can access. I'm still kind of touch and go with classes (along | |
# with everything else) but I'm starting to get a little more comfortable with looking at them and understanding what's going on. | |
# I think I'll try to implement the diagonal method a little later on. I was thinking about how to implement it but it was | |
# starting to make my head hurt. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment