Forked from dbc-challenges/0.2.1-boggle_class_from_methods.rb
Last active
December 29, 2015 14:59
-
-
Save ireneyiu/7687320 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) | |
@dice_grid = dice_grid | |
end | |
def create_word(*coordinates) | |
coordinates.map { |coordinate| @dice_grid[coordinate.first][coordinate.last] }.join("") | |
end | |
def get_row(row) | |
@dice_grid[row] | |
end | |
def get_col(col) | |
@dice_grid.map { |row| row[col] } | |
end | |
def get_diagonal(coordinate1, coordinate2) | |
row_difference, col_difference = coordinate1.first - coordinate2.first, coordinate1.last - coordinate2.last | |
raise ArgumentError.new("Coordinates are not diagonals") unless row_difference.abs == col_difference.abs # absolute value of the difference between the row and col coordinates should be the same | |
row_index, col_index = coordinate1.first, coordinate1.last | |
row_counter, col_counter = (row_difference < 0) ? 1 : -1, (col_difference < 0) ? 1 : -1 # determine if we are incrementing or decrementing the row and column coordinates | |
diagonals = [] | |
until row_index == coordinate2.first # generate the diagonal coordinates | |
diagonals << @dice_grid[row_index][col_index] | |
row_index += row_counter | |
col_index += col_counter | |
end | |
diagonals << @dice_grid[row_index][col_index] # push the final coordinate | |
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: | |
boggle_board = BoggleBoard.new(dice_grid) | |
p boggle_board.create_word([1,2], [1,1], [2,1], [3,2]) == "dock" | |
p boggle_board.get_row(0) == ["b", "r", "a", "e"] | |
p boggle_board.get_row(1) == ["i", "o", "d", "t"] | |
p boggle_board.get_row(2) == ["e", "c", "l", "r"] | |
p boggle_board.get_row(3) == ["t", "a", "k", "e"] | |
p boggle_board.get_col(0) == ["b", "i", "e", "t"] | |
p boggle_board.get_col(1) == ["r", "o", "c", "a"] | |
p boggle_board.get_col(2) == ["a", "d", "l", "k"] | |
p boggle_board.get_col(3) == ["e", "t", "r", "e"] | |
# create driver test code to retrieve a value at a coordinate here: | |
p boggle_board.create_word([3,2]) == "k" | |
p boggle_board.get_row(3)[2] == "k" | |
p boggle_board.get_col(2)[3] == "k" | |
p boggle_board.get_diagonal([0,0], [3,3]) == ["b", "o", "l", "e"] | |
p boggle_board.get_diagonal([0,3], [3,0]) == ["e", "d", "c", "t"] | |
p boggle_board.get_diagonal([3,0], [0,3]) == ["t", "c", "d", "e"] | |
# REFLECTION | |
# I worked on this challenge during a pairing session with Ivan Sued. | |
# Most of the methods were fairly easy to figure out. However, we were struggling with the #get_diagonal method | |
# since the coordinates of the diagonal can either increment or decrement, depending on the difference between | |
# the row and column coordinates. For example, the diagonal coordinates [0,0] and [3,3] means incrementing the row and column values, | |
# but [0,3] and [3,0] means incrementing the row value while decrementing the column value. | |
# Since there's no way we can do decreasing ranges (e.g. 3..0) to generalize the solution, I initially suggested making two separate arrays | |
# and then merging the two together via the array #zip method to get the coordinates. The first array would be the range of increasing values | |
# say (0..3). The second array would be the array of decreasing values (3.downto(0)). The #zip method would pair everything together. | |
# This method works, but is slightly clunky performance-wise since it means creating two separate arrays and merging the results together. | |
# I was able to come up with something later that doesn't involve merging two separate arrays. | |
# The object-oriented approach to modeling the boggle board makes things a lot more managable. | |
# In the procedural approach, I had to make the boggle board a global variable in order to access it via | |
# my different methods, and each time I had to call #create_word with $boggle_board plus the coordinates. | |
# Putting the methods inside the boggle board class means delegation of the logic to each boggle board instance. | |
# Instead of calling a method to manipulate a certain object, the object contains that method itself. | |
# If there were ever only one boggle board, then the procedural approach may make sense. But if we're dealing with | |
# different boggle boards, it seems to make more sense to have the methods defined as part of the class. | |
# This also prevents us from polluting the global scope with methods that only pertain to a certain object. | |
# Furthermore, if we ever wanted a #get_row method for something completely different (say, a seating chart), then we would | |
# have a namespacing nightmare. So putting the methods inside the objects means cleaner, separated code. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment