Forked from dbc-challenges/0.2.1-boggle_class_from_methods.rb
Last active
December 28, 2015 02:09
-
-
Save henryh28/7425454 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 | |
def initialize(dice_grid) # initializes the boggle board | |
@board = dice_grid | |
end | |
# create_word will return a string of letters based upon user selected set of coordinates | |
def create_word(*coords) coords.map { |coord| @board[coord.first][coord.last]}.join("") end | |
def get_row(row) @board[row] end # Returns selected row of the boggle board | |
def get_col(col) @board.transpose[col] end # Returns selected column of the boggle board | |
def access(row, col) | |
@board[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.create_word([1,2], [1,1], [2,1], [3,2]) #=> returns "dock" | |
puts "#{boggle.get_row(2)}" #=> [e c l r] | |
puts "#{boggle.get_col(3)}" #=> [e t r e] | |
# create driver test code to retrieve a value at a coordinate here: | |
boggle.access(3,2) | |
#5) Review and Reflect | |
#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?) | |
=begin | |
The benefits of OOP programming does not really become apparent until the scale of the project increases. Inheritance, a key | |
concept in OOP is not really any better when you are only working with a handful of simple classes. But as scale and complexity | |
increases, the benefits become equally increasingly apparent. | |
In addition, encapsulation is also a hallmark of OOP, and can be very helpful in limiting the scale of errors and error | |
tracing in a program. In procedural programming, as programs expand in size and complexity it can become difficult to avoid | |
using global scope for certain variables; excessive use of this can contribute to unintentional changing of datas that cause the | |
program to behave unpredictably and would make the tracing of these logic errors more time consuming | |
=end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment