Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save alvarezmelissa87/9127419 to your computer and use it in GitHub Desktop.
Save alvarezmelissa87/9127419 to your computer and use it in GitHub Desktop.
phase 0 unit 2 week 1boggle class challenge
# OBJECTIVE 1: Instantiate new BobbleBoard class which will have methods from last challenge (#create_word, #get_row, #get_column)
class BoggleBoard
attr_reader :board # I removed one of the parameters that the methods took before they were part of
# the BoggleBoard class- the board parameter -where you had to pass a nested array as one of the
def initialize(board) # arguments so that the method knew what to do its method to. In this case, you're initializing
@board = board # an instance of the BoggleBoard class with a nested array (dice_grid, in this case) so you don't
end # have to specify a board parameter for each method within the class. You create the instance variable
# that will stand for it and then can just use it in the methods within the class.
def create_word(*coords)
coords.map{ |coord| @board[coord.first][coord.last]}.join("")
end
def get_row(row)
board[row]
end
def get_column(column) # This method was able to be refactored since we only take one parameter and don't have to worry
board.map{ |row| row[column] } # about specifying the board/nested array- now we can use #map method which gives output as array
end
def get_diagonal(coord1, coord2)
unless (coord1[1] - coord2[1]).abs == (coord1[0] - coord2[0]).abs && coord1[1] - coord2[1] != 0
raise ArgumentError.new("Coordinates do not define a diagonal.")
else
diagonal = [] # We define the method and give it two parameters for the
until coord1[1] == coord2[1] && coord1[0] == coord2[0] # coordinates that will be the start and end of the diagonal.
diagonal << @board[coord1[0]][coord1[1]] # Raise argument error unless the coordinates meet the pattern
if coord1[0] < coord2[0] # defined (which is a pattern diagonals would have). Create a
coord1[0] += 1 # diagonal array to push our coordinates in diagonal into until
if coord1[1] < coord2[1] # coord1 has reached coord2. Next we just determine the direction
coord1[1] += 1 # of diagonal (get from start coordinates) and then increment
else # accordingly. If coord1[0] is < coord2[0] we know coord1[0] will
coord1[1] -= 1 # be incrementing by +1 (until reach coord2[0]). Then, if coord1[1]
end # is < coord2[1] we increment coord1[1] by +1 but if it's greater
else # coord1[1] increments by -1.
coord1[0] -= 1 # If coord1[0] was greater than coord2[0] then coord1[0] increments
if coord1[1] < coord2[1] # by -1 (until reaches coord2[0]). And then if coord1[1] is less
coord1[1] += 1 # than coord2[1], coord1[1] increments by +1 (but if coord1[1] is
else # greater than coord2[1], coord1[1] increments by -1 until reaches
coord1[1] -= 1 # coord2[1].
end # Make sure I end all of the if/else staements
end # Push in the end coordinates (coord2) to the diagonal array. Now
end # will contain all of the coordinates starting with coord1 until
diagonal << @board[coord2[0]][coord2[1]] # coord2.
end
end
end
dice_grid = [["b", "r", "a", "e"],
["i", "o", "d", "t"],
["e", "c", "l", "r"],
["t", "a", "k", "e"]]
# The driver code below also had to be changed to not include the board parameter as it did in the previous challenge.
# Also we're calling the methods on boggle_board now (the instance of BoggleBoard class initialized with the nested array dice_grid)
# I carried over the driver code from the previous challenge (and added what was asked for in this challenge) because it should
# still work as good driver code since we're putting in the same methods into our BoggleBoard class- although I did have to adjust
# driver code.
boggle_board = BoggleBoard.new(dice_grid) # Initialize boggle_board instance of BoggleBoard class with dice_grid nested array.
puts boggle_board.create_word([2,1], [1,1], [1,2], [0,3]) #=> returns "code"
puts boggle_board.create_word([0,1], [0,2], [1,2]) #=> returns "rad"
# Tried out these coordinates as per OBJECTIVE 2
puts boggle_board.create_word([1,2], [1,1], [2,1], [3,2]) #=> "dock"
# OBJECTIVE 3 : Write some driver code to access an individual coordinate in your boggle_board object.
# Access the "k" character at row 3 column 2 - I can do this using the #create_word method and choosing the
# corresponding coordinates for the letter.
puts boggle_board.create_word([3,2]) #=> "k"
# OBJECTIVE 2: write code to test methods (from last challenge but modified) with class.
# print out all the rows and columns of the board as strings.
print boggle_board.get_row(0) #=> ["b", "r", "a", "e"]
print boggle_board.get_row(1) #=> ["i", "o", "d", "t"]
print boggle_board.get_row(2) #=> ["e", "c", "l", "r"]
print boggle_board.get_row(3) #=> ["t", "a", "k", "e"]
print boggle_board.get_column(0) #=> [ "b", "i", "e", "t" ]
print boggle_board.get_column(1) #=> [ "r", "o", "c", "a" ]
print boggle_board.get_column(2) #=> [ "a", "d", "l", "k" ]
print boggle_board.get_column(3) #=> [ "e", "t", "r", "e" ]
#This code is same as above but will return "true" if they
#actually match up like they should. It's easier to look at and see if it's running correctly.
puts boggle_board.get_row(0) == ["b", "r", "a", "e"] # => true
puts boggle_board.get_row(1) == ["i", "o", "d", "t"] # => true
puts boggle_board.get_row(2) == ["e", "c", "l", "r"] # => true
puts boggle_board.get_row(3) == ["t", "a", "k", "e"] # => true
puts boggle_board.get_column(0) == [ "b", "i", "e", "t" ] # => true
puts boggle_board.get_column(1) == [ "r", "o", "c", "a" ] # => true
puts boggle_board.get_column(2) == [ "a", "d", "l", "k" ] # => true
puts boggle_board.get_column(3) == [ "e", "t", "r", "e" ] # => true
#OBJECTIVE 4 : create bonus #get_diagonal method
# INPUT : two sets of index numbers (two coordinates) (for example, ([0,0], [1,1]))
# OUTPUT : an array of values that are a diagonal defined by the two input coordinates.
# PSEUDOCODE:
# We take a starting coordinate and an ending coordinate as parameters. These coordinates are the start
# and end point of our diagonal. If they are diagonal we return the array with each of the letters corresponding
# to the coordinates in the diagonal and if they are not diagonal we return nothing (or we could raise an ArguementError
# saying the coordinates are not diagonal).
# A diagonal in a nested array will simply be a change of 1 (+ or -) in rise over run. For example, [0,0] [1,1] [2,2], etc or
# if going down from right to left- [3,3], [2,2], etc. Even when not going from corner to corner, the coordinates that begin
# and end our diagonal will have a relationship in which (if we say x,y represent the coordinates for a particlar place in the
# nested array) y1 - y2 is equal to x1 - x2 and y1 - y2 will not be equal to 0. To represent this we would be comparing the
# absolute values of the differences (since to be a diagonal the change is either - or + 1 depending on direction (when you
# take rise over run)).
# Raise ArgumentError ("this is not a diagonal", or something like this) if coordinates passed as arguments don't match the
# pattern we just defined that diagonal coordinates would have. If they are diagonals, then we have to start from the
# first coordinate and, depending on the direction of the diagonal (taken from second coordinates) we increment either up or
# down by 1 until coord1[0] reaches/is equal to coord2[0] and coord1[1] reaches/is equal to coord2[1].
# We take these coordinates (obtained by incrementing by 1 or negative 1) and push them into an empty array we create (called diagonal)
# which will be the array we return after the code has run (it will contain all of the coordinates making up the diagonal).
# If coord1[0] is less than coord2[0] and coord1[1] is less than coord2[1]- this tells us the diagonal is going
# downward and to the right thus incrementing x,y of coord1 by +1 until reaching x,y of coord2. We push the coordinates
# in between into the diagonal array.
# If coord1[0] is greater than coord2[0] and coord1[1] is greater than coord2[1]- this tells us the diagonal is going
# upward and to the left thus -1 increments until reaching x,y of coord2.
# If coord1[0] is less than coord2[0] (do +=1) and if coord1[1] is greater than coord2[1] (do -=1) - this is a diagonal going
# down and left so we do the appropriate incrementation until x,y of coord1 reach/equal x,y of coord2.
# Conversely, if coord1[0] is greater than coord2[0] (do -= 1) and if coord1[1] is less than coord2[1] (do += 1) - this is
# a diagonal going up and to the right so we do the appropriate incrementation until x,y of coord1 reach/equal x,y of coord2.
# That takes care of every diagonal possibility and will return the diagonal array with the coordinates for each
# element that is in the diagonal from beginning coordinate to end coordinate.
# Driver code to test get_diagonal method with expected output
p boggle_board.get_diagonal([0,3],[3,0]) #=> ["e", "d", "c", "t"]
p boggle_board.get_diagonal([0,0],[3,3]) #=> ["b", "o", "l", "e"]
p boggle_board.get_diagonal([0,1],[2,3]) #=> ["r", "d", "r"]
p boggle_board.get_diagonal([0,3],[0,2]) #=> "Coordinates do not define a diagonal."
# REFLECTION
# Whew. This challenge was awesome. I had not too bad of a time using the nested array boggleboard challenge to model my
# methods I had already created. They required some modifications since they would now be used in a class but I was
# able to implement them pretty easily. The most challenging part was the bonus #get_diagonal method. I had to
# whiteboard out a solution- it was difficult to explain it as pseudocode but I tried to be as detailed as possible in all
# the necessary steps. I feel that there must be a shorter and less repetitive way of making this method work. Perhaps
# using the #map method which returns results in an array. I wasn't able to figure out a shorter way of making this method
# work (not yet, anyway).
# Now that we've transitioned to object oriented programming, I can see the utility of doing this in this way. Procedural
# programming is much more about the particular steps you're taking whereas OOP is more about the object and the objects'
# attributes and the methods and messages that can be passed to it. When working with OOP we are working with the object's
# methods and it seems like it is a lot more flexible as when you change something it doesn't affect everything else in
# your program as it would with procedural programming because each step usually depends on some other step. OOP would be
# more flexible and more like the real world than procedural. Even though OOP might result in more code, it is much easier
# to update and maintain.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment