Forked from dbc-challenges/0.2.1-boggle_class_from_methods.rb
Last active
January 3, 2016 02:19
-
-
Save AndrieuxReabeurts/8395140 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 | |
#your code here | |
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: | |
class BoggleBoard | |
def initialize(boggle_board) | |
@boggle_board = boggle_board | |
end | |
def create_word(*coords) | |
p coords.map { |coord| @boggle_board[coord.first][coord.last]}.join("") | |
end | |
def get_row(row) | |
array = [] | |
array = @boggle_board[row] | |
p array | |
#board.map { |element| board[row] } | |
#p board[row] | |
end | |
#get_row(row) #=> ["i", "o", "d", "t"] | |
def get_col(col) | |
array = [] | |
for num in 0..3 | |
array.push(@boggle_board[num][col]) | |
end | |
p array | |
end | |
end | |
#get_col(boggle_board, 1) #=> ["r", "o", "c", "a"] | |
dice_grid = [["b", "r", "a", "e"], | |
["i", "o", "d", "t"], | |
["e", "c", "l", "r"], | |
["t", "a", "k", "e"]] | |
boggle_board = BoggleBoard.new(dice_grid) | |
boggle_board.create_word([2,1], [1,1], [1,2], [0,3]) #=> returns "code" | |
boggle_board.create_word([0,1], [0,2], [1,2]) #=> creates what california slang word? RAD | |
boggle_board.create_word([1,2], [1,1], [2,1], [3,2]) #=> "dock" | |
boggle_board.create_word([3,2]) #=> "k" | |
boggle_board.get_row(1) | |
boggle_board.get_col(1) | |
=begin | |
I would say the most striking advantage in an object-oriented implementation is | |
the fact that there is a greater interchangeability of parts, the variables "play better" | |
together in this implementation. In the method based implementation, I was forced to | |
choose between hard-coding the boggle board in every method or adding an argument to | |
the get_row and get_col methods. I thought adding the argument call to get_row and | |
get_col was advantageous, but in the object-oriented approach, it's nice to be presented | |
with an even easier and more straight-forward approach. | |
=end | |
# create driver test code to retrieve a value at a coordinate here: | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment