Forked from dbc-challenges/0.2.1-boggle_class_from_methods.rb
Last active
December 30, 2015 02:39
-
-
Save thejessleigh/7763839 to your computer and use it in GitHub Desktop.
phase 0 unit 2 week 1
boggle class challenge
This file contains 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(grid) | |
@grid = grid | |
end | |
def create_word(*coords) | |
coords.map { |coord| @grid[coord.first][coord.last]}.join("") | |
end | |
def get_row(row) | |
return @grid[row] | |
end | |
def get_col(col) | |
return @grid.map {|row| row[col]} | |
end | |
def get_coord(coord1, coord2) | |
return @grid[coord1][coord2] | |
end | |
def is_diagonal?(coord1, coord2) | |
(coord1.first - coord2.first).abs == (coord1.last - coord2.last).abs ? true : false # is the distance between coordinates equqal? | |
end # do they define a square from which to take the diagonal? | |
def get_diagonal(coord1, coord2) | |
unless is_diagonal?(coord1, coord2) # use a helper method to determine whether to raise an ArgumentError | |
raise ArgumentError.new("That is not a diagonal") | |
end | |
diagonal = [] | |
row_count = coord1.first # assumes that the coordinates were input left coord to right coord | |
column_count = coord1.last | |
if coord1.first < coord2.first # if the first coordinate is the top coordinate | |
until row_count > coord2.first # until the counters have reached the bottom | |
diagonal << @grid[row_count][column_count] # grab the current coordinate in the diagonal | |
row_count += 1 | |
column_count += 1 # increment row downwards and column to the right | |
end | |
else # if the first coordinate is the bottom coordinate | |
until row_count < coord2.first # until the counters have reached the top | |
diagonal << @grid[row_count][column_count] # grab the current coordinate in the diagonal | |
row_count -= 1 | |
column_count += 1 # increment the row upwards and the column to the right | |
end | |
end | |
return diagonal # return letters from the diagonal line | |
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) | |
# DRIVER CODE: | |
puts boggle_board.get_col(0) #=> ["b", "i", "e", "t"] | |
puts boggle_board.get_row(2) #=> ["e", "c", "l", "r"] | |
puts boggle_board.create_word([0,0],[1,1],[0,2],[0,1],[1,2]) #=> "board" | |
# The boggle_board object holds the dice_grid array as an instance variable that | |
# is initialzed as part of the BoggleBoard class. | |
puts boggle_board.create_word([1,2], [1,1], [2,1], [3,2]) #=> "dock" | |
puts boggle_board.get_coord(3,2) == "k" | |
puts boggle_board.get_coord(1,3) == "t" | |
puts boggle_board.get_coord(3,3) == "e" | |
puts boggle_board.is_diagonal?([0,0], [3,3]) == true | |
puts boggle_board.is_diagonal?([2,2], [3,0]) == false | |
# puts boggle_board.get_diagonal([2,2], [3,0]) => "That is not a diagonal"(ArgumentError) | |
puts boggle_board.get_diagonal([0,0], [3,3]) == ["b", "o", "l", "e"] | |
puts boggle_board.get_diagonal([3,0], [0,3]) == ["t", "c", "d", "e"] | |
puts boggle_board.get_diagonal([2,1], [3,2]) == ["c", "k"] | |
# REFLECTION: | |
# Creating a class implementing the methods from the previous exercize was pretty easy. Last time, rather than defining a class, | |
# I defined boggle_board as a global variable, but this was a much easier way to do it. The object oriented approach allows | |
# me to create multiple instances of the Boggle game, and to share methods and characterisitcs without redifining them for every | |
# instance. The global variable is something that I would rather avoid because it is too easily accessed and edited by multiple | |
# sources and does not allow for enough flexibility. | |
# For creating the diagonal methods, I decided that it would be a little bit cleaner to read if I created a helper method | |
# to determine whether or not the input coordinates were in fact diagonal to one another. I checked to see if the | |
# coordinates were corners of a square by determining if the absolute value of the difference between the rows and columns | |
# was equal. If this is true, then the coordinates could be part of the diagonal. | |
# Figuring out how to move the coordinates was a little bit trickier. I understood the concept, but it took a lot of | |
# tweaking the code to understand how to move through the array to get the right coordinates. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Great solution to the the diagonal challenge. I particularly like your helper method.