Forked from dbc-challenges/0.2.1-boggle_class_from_methods.rb
Last active
December 29, 2015 13:39
-
-
Save thefenry/7678801 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
# PSUEDOCODE | |
# initialize: instantiate the dice_grid | |
# create_word: | |
# iterate through the input coordinates | |
# for each coordinate, get the element at the first index (row) and the element at the second index (column) and append it to a string | |
# return the joined string | |
# get_row: get the row number of the dice_grid | |
# get_col: map the board to its rows and get the specified column on the row | |
# get_diagonal: | |
# check if the two coordinates are diagonals by comparing the differences within each coordinate pair | |
# raise an error if the differences are not the same | |
# determine if the first coordinate specified is smaller or greater than the second | |
# if it's smaller, then we increment the coordinate by 1 until we reach the second coordinate | |
# if it's greater, then we decrement the coordinate by 1 until we reach the second coordinate | |
# return the values within the range of coordinates | |
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 | |
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) | |
boggle_board.create_word([1,2], [1,1], [2,1], [3,2]) #=> results in "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" | |
#Reflection | |
# The hardest part of this problem was trying to figure out how to pass the array to every method | |
# without changing the parameters already defined. I decided to make it an instance variable so that | |
# every method can now access the array. This problem was simple overall exept the column method because | |
# i was hinted to use the map method but I was not sure how to use it. Once a cohort explained it to | |
# it seemed easy to use and very useful for multidimensional arrays. | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment