Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save mejarc/8011980 to your computer and use it in GitHub Desktop.
Save mejarc/8011980 to your computer and use it in GitHub Desktop.
phase 0 unit 2 week 1 boggle class challenge
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)
@grid[row]
end
def get_col(col)
@grid.map { |row| row[col] }
end
def get_diagonal(start = 0, direction = "forward")
raise ArgumentError, "You must start at 0 or 3" unless start == 0 || start == 3
result = []
i = 0
j = start
4.times do
if start == 3 # upper-right corner
result << self.create_word([i, j])
i += 1
j -= 1
else # default will be upper-left corner
result << self.create_word([i, j])
i = j += 1
end
end
direction == "forward"? result : result.reverse
end
end
# 1) Instantiate a new board object.
# Transform your driver code so that
# it creates a new board object. You'll need to pass the original 2D
# array as an argument (let's call that dice_grid because boggle_board
# is going to be an object now.)
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:
puts boggle_board.create_word([1, 2], [1, 1], [2, 1], [3, 2]) # => returns "dock"
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]) # => creates what california slang word?
puts boggle_board.create_word([3, 0], [3, 1], [3, 2], [3, 3]) # => returns "take"
puts boggle_board.create_word([1, 3], [0, 3], [0, 2], [0, 1]) # => returns "tear"
puts boggle_board.get_row(1) # => ["i", "o", "d", "t"]
puts boggle_board.get_row(3) # => ["t", "a", "k", "e"]
puts boggle_board.get_row(-1) # => ["t", "a", "k", "e"]
puts boggle_board.get_col(1) # => ["r", "o", "c", "a"]
puts boggle_board.get_col(2) # => ["a", "d", "l", "k"]
puts boggle_board.get_col(-2) # => ["a", "d", "l", "k"]
# Now print out
# all the rows and columns of the board as strings. You should end up
# with 8 four letter words. Are there any real words shown? Add your
# total output as a comment in your gist.
i = 0
while i < 4
puts boggle_board.get_row(i).join('')
puts boggle_board.get_col(i).join('')
i += 1
end
# Output:
=begin
brae
biet
iodt
roca
eclr
adlk
take
etre
=end
# 3) Access a coordinate Now write some driver code to access an
# individual coordinate in your boggle_board object. Make this as simple
# as possible.
# create driver test code to retrieve a value at a coordinate here:
puts boggle_board.get_row(0)[1] # => returns "r"
# Can you access the "k" character at row 3 column 2?
puts boggle_board.get_row(3)[2]
# 4) Bonus: Create a #get_diagonal method Just like the #get_col or
# #get_row method, the #get_diagonal method should return an array of
# values, but it will need 2 coordinates entered to define the diagonal.
# Error checking to make sure the coordinates are actually a diagonal
# would probably be a good idea.
puts boggle_board.get_diagonal # => ["b", "o", "l", "e"]
puts boggle_board.get_diagonal(0, "backward") # => ["e", "l", "o", "b"]
puts boggle_board.get_diagonal(3) # => ["e", "d", "c", "t"]
puts boggle_board.get_diagonal(3, "backward") # => ["t", "c", "d", "e"]
puts boggle_board.get_diagonal(4) # => ArgumentError
# 5) Review and Reflect You just made a transition from procedural
# programming to object-oriented programming! How is the implementation
# different? What are the benefits to using the Object Oriented approach
# (even if it is a bit more code?)
# * can see potential to create different-size grids
# * can see potential to reuse methods within other methods
# * transform `return false` into ArgumentError
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment