Forked from dbc-challenges/0.2.1-boggle_class_from_methods.rb
Last active
January 2, 2016 13:48
-
-
Save RyanVerhey/8312059 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 :grid | |
def initialize(grid) | |
@grid = grid | |
end | |
def access(*coords) | |
coords.map { |coord| @grid[coord.first][coord.last]}.join("") | |
end | |
def create_word(*coords) | |
coords.map { |coord| @grid[coord.first][coord.last]}.join("") | |
end | |
def get_row(row) | |
@grid[row] | |
end | |
def get_col(col) | |
column = [] | |
@grid.each { |x| column << x[col] } | |
column | |
end | |
def get_diagonal(coord_1, coord_2) | |
if coord_1 == [0,0] and coord_2 == [3,3] | |
[@grid[0][0], @grid[1][1], @grid[2][2], @grid[3][3]] | |
elsif coord_1 == [0,3] and coord_2 = [3,0] | |
[@grid[0][3], @grid[1][2], @grid[2][1], @grid[3][0]] | |
else | |
puts "Sorry, those coordinates aren't diagonal" | |
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: | |
p boggle_board.create_word([1,2], [1,1], [2,1], [3,2]) #=> returns "dock" | |
p boggle_board.get_row(1) #=> returns ["i", "o", "d", "t"] | |
p boggle_board.get_col(1) #=> returns ["r", "o", "c", "a"] | |
p boggle_board.get_diagonal([0,0], [3,3]) #=> returns ["b", "o", "l", "e"] | |
p boggle_board.get_diagonal([0,3], [3,0]) #=> returns ["e", "d", "c", "t"] | |
p boggle_board.get_diagonal([2,2], [3,2]) #=> returns "Sorry, those coordinates aren't diagonal" | |
# create driver test code to retrieve a value at a coordinate here: | |
p boggle_board.access([3,2]) #=> returns "k" | |
#REFLECTION! | |
# | |
# Wooo, I'm an object-oriented programmer! The implimentation is a bit | |
# less direct, because, in this case, I'm not dealing with dice_grid | |
# directly, I'm dealing with an object I created using it. I think a | |
# huge benefit to object-oriented programming is that it makes things | |
# reusable. For example, in the last challenge, I'd have to rewrite | |
# all the code just to add a new board. But, using object-oriented | |
# programming, I could just create a new object with a different | |
# grid and use the BoggleBoard class all over again without having | |
# to rewrite everything. It makes things tons simpler. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment