Forked from dbc-challenges/0.2.1-boggle_class_from_methods.rb
Last active
December 30, 2015 13:09
-
-
Save darrendahl/7834192 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 {|x| x[col]} | |
end | |
def get_diag(first,second) | |
raise ArgumentError.new("Parameter too big") if first > 3 || second > 3 | |
diag_array = [] | |
while first < 4 | |
diag_array << @board[first][second] | |
first = first + 1 | |
second = second - 1 | |
end | |
return diag_array | |
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) | |
puts boggle_board.create_word([2,1], [1,1], [1,2], [0,3]) | |
puts boggle_board.get_row(0).join("") | |
puts boggle_board.get_row(1).join("") | |
puts boggle_board.get_row(2).join("") | |
puts boggle_board.get_row(3).join("") | |
puts boggle_board.get_col(0).join("") | |
puts boggle_board.get_col(1).join("") | |
puts boggle_board.get_col(2).join("") | |
puts boggle_board.get_col(3).join("") | |
puts boggle_board.board[3][2] | |
puts boggle_board.get_diag(0,3) | |
puts boggle_board.get_diag(0,6) | |
#The object oriented approach is easier to read and different, because you dont have to pass boggle_board as an argument every time. | |
#Its more code to initialize and have the getter attribute method, but it looks way more contained. in the prodecural approach everything looks more scattered, | |
#In the object approach, its more categorized. theres the boggleboard class, and then the implementations of that class afterwards, instead of mixed together. | |
#The main advantage is that the object oriented approach is more versatile. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment