Forked from dbc-challenges/0.2.1-boggle_class_from_methods.rb
Last active
January 1, 2016 06:59
-
-
Save krnwonseungee/8108762 to your computer and use it in GitHub Desktop.
phase 0 unit 2 week 1
boggle 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(board) | |
@board = board | |
end | |
def create_word(*coords) | |
coords.map { |coord| @board[coord.first][coord.last]}.join("") | |
end | |
def get_row(row) | |
@board[row].join("").to_s | |
end | |
def get_col(col) | |
@board.collect {|index| index[col]}.join("").to_s | |
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: | |
# puts boggle_board.create_word([1,2], [1,1], [2,1], [3,2]) #=> returns "dock" | |
# puts boggle_board.get_row(0) #=> returns ["b", "r", "a", "e"] | |
# puts boggle_board.get_row(1) #=> returns ["i", "o", "d", "t"] | |
# puts boggle_board.get_row(2) #=> returns ["e", "c", "l", "r"] | |
# puts boggle_board.get_row(3) #=> returns ["t", "a", "k", "e"]; real word | |
# puts boggle_board.get_col(0) #=> returns ["b", "i", "e", "t"] | |
# puts boggle_board.get_col(1) #=> returns ["r", "o", "c", "a"] | |
# puts boggle_board.get_col(2) #=> returns ["a", "d", "l", "k"] | |
# puts boggle_board.get_col(3) #=> returns ["e", "t", "r", "e"]; real word in FRENCH! | |
# create driver test code to retrieve a value at a coordinate here: | |
# puts boggle_board.create_word([3,2]) #=> returns "k"; simplest solution without adding a completely new method | |
# REFLECTION | |
# Since the boggle_board we are working with is an object this time, there is no need to add | |
# an extra argument for each method, e.g. boggle_board.get_col(row) instead of get_col(boggle_board, row). | |
# The methods are stated after the object, rather than being a method that stands all by itself. | |
# Even though it requires a bit more code, the object-oriented approach to coding is a much neater | |
# way of grouping code in a more organized fashion because you are able to group methods under certain | |
# classes and ultimately use DRYer, more readable code because you are not passing the same argument | |
# again and again. | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment