Forked from dbc-challenges/0.2.1-boggle_class_from_methods.rb
Last active
December 31, 2015 21:18
-
-
Save kma3a/8045490 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(board) | |
@board = board | |
end | |
def access_coord(row_num, col_num) | |
@board[row_num][col_num] | |
end | |
def create_word(*coords) | |
coords.map { |coord| @board[coord.first][coord.last]}.join("") | |
end | |
def get_row(row) | |
@board[row].inspect | |
end | |
def get_col(col) | |
@board.collect {|row_num| row_num.at(col)}.inspect | |
end | |
def get_diagonal | |
diagonal_array = [] | |
@board.each { |board_inside_array| diagonal_array << board_inside_array.values_at(@board.index(board_inside_array)).join("")} | |
diagonal_array.inspect | |
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: | |
p boggle_board.create_word([3,2]) == "k" | |
p boggle_board.create_word([1,2], [1,1], [2,1], [3,2]) == "dock" | |
puts boggle_board.get_row(0) == '["b", "r", "a", "e"]' | |
puts boggle_board.get_col(3) == '["e", "t", "r", "e"]' | |
puts boggle_board.get_diagonal == '["b", "o", "l", "e"]' | |
# create driver test code to retrieve a value at a coordinate here: | |
p boggle_board.access_coord(3, 2) == "k" | |
=begin | |
5) Review and Reflect | |
You just made a transition from procedural programming to object-oriented programming! | |
How is the implementation different? You need to have an initialize and that you can create variable that can be accessed through the whole class so you do not need to call every single thing over and over again. | |
What are the benefits to using the Object Oriented approach (even if it is a bit more code?) It makes everything easier to read and if there is an error or something isn't working right you can find it eaier than if you don't. | |
=end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment