Forked from dbc-challenges/0.2.1-boggle_class_from_methods.rb
Last active
August 29, 2015 13:55
-
-
Save drewbs/8726257 to your computer and use it in GitHub Desktop.
phase 0 unit 2 week 1boggle class challenge
This file contains hidden or 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(boggle_board) | |
@boggle_board = boggle_board | |
end | |
def create_word(board, *coords) | |
coords.map { |coord| board[coord.first][coord.last]}.join("") | |
end | |
def get_row(row) | |
@boggle_board[row] | |
end | |
def get_col(col) | |
@boggle_board.transpose[col] | |
end | |
def get_diagonal | |
diag = [] | |
@boggle_board.each_index do |d| | |
diag << @boggle_board[d][d] | |
end | |
puts diag.inspect | |
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) # boggle_board holds dice_grid as an array. | |
# implement tests for each of the methods here: | |
puts boggle_board.create_word(dice_grid, [1,2], [1,1], [2,1], [3,2]) #=> should return "dock" | |
puts boggle_board.get_row(1).inspect #=> should return ["i", "o", "d", "t"] | |
puts boggle_board.get_col(2).inspect #=> should return ["a", "d", "l", "k"] | |
dice_grid.each_index do |x| #=> should return: brae | |
puts boggle_board.get_row(x).join("") #=> iodt | |
end #=> eclr | |
#=> take (real word) | |
dice_grid.each_index do |x| #=> should return: biet | |
puts boggle_board.get_col(x).join("") #=> roca | |
end #=> adlk | |
#=> etre (French word :-) | |
puts boggle_board.get_diagonal #=> should return ["b", "o", "l", "e"] | |
# create driver test code to retrieve a value at a coordinate here: | |
puts boggle_board.create_word(dice_grid, [3,2]) #=> should return k | |
# REFLECTIONS | |
# Although creating a class did take more code than in the previous exercise, it did seem | |
# easier to add additional methods on the fly. The methods could be called easily from | |
# outside the class. The methods can also be reused easily. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment