Forked from dbc-challenges/0.2.1-boggle_class_from_methods.rb
Last active
August 29, 2015 13:55
-
-
Save moshi-kibu/8778499 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(dice_grid) | |
@dice_grid = dice_grid | |
end | |
def create_word(*coords) | |
coords.map { |coord| dice_grid[coord.first][coord.last]}.join("") | |
end | |
def get_row(row) | |
dice_grid[row] | |
end | |
def get_col(col) | |
dice_grid.map do |nested_array| | |
nested_array[col] | |
end | |
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 create_word((1,2), (1,1), (2,1), (3,2)) == "dock" | |
puts get_row(0) == ["b", "r", "a", "e"] | |
puts get_row(1) == ["i", "o", "d", "t"] | |
puts get_row(2) == ["e", "c", "l", "r"] | |
puts get_row(3) == ["t", "a", "k", "e"] | |
puts get_col(0) == ["b", "i", "e", "t"] | |
puts get_col(1) == ["r", "o", "c", "a"] | |
puts get_col(2) == ["a", "d", "l", "k"] | |
puts get_col(3) == ["e", "t", "r", "e"] | |
# create driver test code to retrieve a value at a coordinate here: | |
puts create_word((3,2)) #=> "k" | |
#You just made a transition from procedural programming to object-oriented programming! | |
#How is the implementation different? | |
#What are the benefits to using the Object Oriented approach (even if it is a bit more code?) | |
# The implementation is very different - it is centered around the class and its instance variables rather than around passing | |
# the results of different methods to one another(although of course that happens in OOP too). | |
# based on my (admittedly) limited experience, it seems to me that OOP is beneficial in that it allows you to maintain and | |
# modify your code more easily - all of the methods pertaining to or affecting this class are here so modifying it would be | |
# relatively simple. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment