Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save cdelauder/9044634 to your computer and use it in GitHub Desktop.
Save cdelauder/9044634 to your computer and use it in GitHub Desktop.
phase 0 unit 2 week 1boggle class challenge
class BoggleBoard
def initialize(board)
@board = board
end
def create_word(*coords)
coords.collect { |coord| @board[coord.first][coord.last]}.join("")
end
def get_row(row) # added board as a parameter so the method would know what it was supposed to work on
@board[row].join("") # basic array method
end
def get_col(col) #this requires us to select the item at index(col) for each row. requires iteration
#get_row(board,0..3).map {|row| row[col]} # iterates through each index of get_row and returns an array of the column values
@board.collect.with_index {|row| row[col]} # same idea, but this makes it a stand-alone method
end
def get_diagonal(*coords)
if coords.flatten.bsearch {|value| value >=4} # check to make sure coordinates are on the board
raise ArgumentError.new("Please use valid coordinates")
else
diag_coords = coords[0] #new variable to hold our calculations
until diag_coords == coords.last #iterate until we have reached the upper limit
diag_coords = diag_coords.map {|num| num += 1} #increment our coordinates
coords.insert(-2, diag_coords) # insert into our array of coordinates
end
coords.pop # the until loop repeats the final variable
coords.collect { |coord| @board[coord.first][coord.last]}.join("") #output a string of the letters corresponding to the coordinates
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.create_word([0,0..3]) #=> 0th array as a string
p boggle_board.create_word([1,0..3]) #=> 1st array as a string
p boggle_board.create_word([2,0..3]) #=> 2nd array as a string
p boggle_board.create_word([3,0..3]) #=> 3rd array as a string
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(0) #=> ["b", "r", "a", "e"]
p boggle_board.get_col(1).join("") #=> 1st column as a string
p boggle_board.get_col(0).join("") #=> 0th column as a string
p boggle_board.get_col(3).join("") #=> 3rd column as a string
p boggle_board.get_col(2).join("") #=> 2nd column as a string
# The words that exist in the boggle board's rows and columns include: brae, take
p boggle_board.get_diagonal([0,0], [3,3]) #=> "bole"
p boggle_board.get_diagonal([0,1], [1,2]) #=> "rd"
p boggle_board.get_diagonal([0,4], [3,7]) #=> ArgumentError
# create driver test code to retrieve a value at a coordinate here:
p boggle_board.create_word([3,2]) #=> "k"
# Reflection
# The biggest difference that I saw when we transitioned to object oriented programming is that now we are able to store objects
# and the methods that go with them. It enables us to group things that go together. In practice this meant that the methods are
# are called on objects, instead of objects being a parameter of a method. The emphasis is on the thing instead of the way. It
# gives objects a presence which they previously lacked and improves the overall clarity of what we build because now it is more
# relatable to real-world objects.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment