Forked from dbc-challenges/0.2.1-boggle_class_from_methods.rb
Last active
August 29, 2015 13:56
-
-
Save boneseggsbones/9163021 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(two_d_boggle_array) | |
@two_d_boggle_array = two_d_boggle_array | |
end | |
def create_word(two_d_boggle_array, *coords) | |
coords.map { |coord| two_d_boggle_array[coord.first][coord.last]}.join("") | |
end | |
def get_row(row) | |
@two_d_boggle_array[row] | |
end | |
def get_col(col) | |
row = 0 | |
column = [] | |
while row < 4 | |
column << @two_d_boggle_array[row][col] | |
row += 1 | |
end | |
column | |
end | |
def access(row,column) | |
@two_d_boggle_array[row][column] | |
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) | |
boggle_board.create_word(dice_grid, [0,0], [0,1], [1,1]) #=> "bro" | |
boggle_board.create_word(dice_grid, [1,2], [1,1], [2,1], [3,2]) #=> "dock" | |
boggle_board.get_row(1) #=> ["i", "o", "d", "t"] | |
#boggle_board.get_row(3) #=> ["t", "a", "k", "e"] | |
boggle_board.get_col(1) #=> ["r", "o", "c", "a"] | |
#boggle_board.get_col(0) #=> ["b", "i", "e", "t"] | |
#boggle_board.get_col(2) #=> ["a", "d", "l", "k"] | |
#boggle_board.get_col(5) #=> [nil, nil, nil, nil] | |
print boggle_board.get_row(0) | |
print boggle_board.get_row(1) | |
puts | |
print boggle_board.get_row(2) | |
puts | |
print boggle_board.get_row(3) | |
puts | |
print boggle_board.get_col(0) | |
puts | |
print boggle_board.get_col(1) | |
puts | |
print boggle_board.get_col(2) | |
puts | |
print boggle_board.get_col(3) | |
# total output | |
#eclr | |
#take | |
#biet | |
#roca | |
#adlk | |
#etre | |
#only "take" is an english word | |
boggle_board.access(3,2) | |
#Reflection | |
#doing things in an object-oriented way has encouraged me to think both a bit more abstractly and in terms of | |
#modular code. encapsulation promotes clarity and dryness, and is a useful approach for breaking down problems | |
#that are complex. | |
#will revisit bonus tomorrow. ran out of time this week. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment