Forked from dbc-challenges/0.2.1-boggle_class_from_methods.rb
Last active
January 3, 2016 04:09
-
-
Save michaelanthonymain/8407060 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 | |
attr_reader :dice_grid | |
def initialize(dice_grid) | |
@dice_grid = dice_grid | |
end | |
def create_word(*coords) | |
coords.map { |coord| @dice_grid[coord.first][coord.last]}.join("") | |
end | |
def get_row(row) | |
return @dice_grid[row][0..3] | |
end | |
def get_col(col) | |
return @dice_grid.transpose[0..3][col] | |
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.get_row(0).to_s #=> returns "brae" | |
p boggle_board.get_row(1).to_s #=> returns "iodt" | |
p boggle_board.get_row(2).to_s #=> returns "eclr" | |
p boggle_board.get_row(3).to_s #=> returns "take" | |
p boggle_board.get_col(0).to_s #=> returns "biet" | |
p boggle_board.get_col(1).to_s #=> returns "roca" | |
p boggle_board.get_col(2).to_s #=> returns "adlk" | |
p boggle_board.get_col(3).to_s #=> returns "etre" | |
# create driver test code to retrieve a value at a coordinate here: | |
p boggle_board.dice_grid[3][2] == "k" #=> returns true | |
# REFLECTION | |
# The object-oriented version of this code is much more readable to me. | |
# Instead of each method taking board and row/column variables, I can just work with | |
# dice_grid the whole way through. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment