Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save paigecrum/8774869 to your computer and use it in GitHub Desktop.
Save paigecrum/8774869 to your computer and use it in GitHub Desktop.
phase 0 unit 2 week 1boggle class challenge
# Challenge completed - still working on #get_diagonal method. Mine is not currently functioning.
class BoggleBoard
attr_reader :board
def initialize(board)
@board = board
end
def create_word(*coords)
coords.map { |coord| @board[coord.first][coord.last]}.join("")
end
def get_row(row)
@board[row]
end
def get_col(col)
@board.transpose[col]
end
=begin #get_diagonal not yet working
def get_diagonal(start, finish)
diagonal = []
if start.first < finish.first && start.last < finish.last
diagonal << @board[start.first][start.last]
until diagonal.last == @board[finish.first][finish.last]
start.each { |coord| coord +=1 }
diagonal << @board[start.first][start.last]
end
#elsif start.first > finish.first && start.last < finish.last
# until diagonal.last == @board[finish.first][finish.last]
# start.each { |coord| coord -=1 }
# diagonal << @board[start.first][start.last] <--This wouldn't work because we need to be subtracting 1 from the row coordinate & adding 1 to the column coordinate
else
puts "Error"
end
diagonal
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:
# Use #create_word method to spell words with coordinates:
puts boggle_board.create_word([1,2], [1,1], [2,1], [3,2]) # => "dock"
puts boggle_board.create_word([1,2], [0,2], [1,3], [0,3]) # => "date"
puts boggle_board.create_word([2,1], [1,1], [2,2], [1,2]) # => "cold"
puts boggle_board.create_word([0,0], [0,1], [1,0], [2,1], [3,2]) # => "brick"
# 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.
# Print out all the rows as strings:
puts boggle_board.get_row(0).join("") # => "brae"
puts boggle_board.get_row(1).join("") # => "iodt"
puts boggle_board.get_row(2).join("") # => "eclr"
puts boggle_board.get_row(3).join("") # => "take" <-- "take" is a real word
# Print out all the columns as strings:
# None of the columns form a real 4-letter word
puts boggle_board.get_col(0).join("") # => "biet"
puts boggle_board.get_col(1).join("") # => "roca"
puts boggle_board.get_col(2).join("") # => "adlk"
puts boggle_board.get_col(3).join("") # => "etre"
# How does the boggle_board object hold the dice_grid?
# The dice_grid is the array that is input as the board in the above example.
# boggle_board is the variable name given to the new BoggleBoard object that was
# just generated.
# Create driver test code to retrieve a value at a coordinate here:
# Can you access the "k" character at row 3 column 2?
puts boggle_board.board[3][2] # => "k"
# More Driver Test Code
puts boggle_board.board[3][2] == "k" # => "true"
puts boggle_board.board[1][0] == "i" # => "true"
puts boggle_board.board[1][3] == "t" # => "true"
# 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?)
#
# In Object Oriented Programming (OOP), you create an instance of a class (an
# object) and work is done through the object. OOP allows us to simulate
# real-world concepts more effectively and allows you to write code that is
# less redundant and easier to reuse and maintain.
#
# I learned how to use the #transpose array method in Ruby and can go back and
# refactor my #get_col method in the other Boggle Board challenge using this
# new method.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment