Forked from dbc-challenges/0.2.1-boggle_class_from_methods.rb
Last active
August 29, 2015 13:57
-
-
Save kendallflutey/9755170 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 | |
attr_accessor :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].to_s | |
end | |
def get_col(col) | |
@board.transpose[col].to_s | |
end | |
def get_diagonal(x,y) | |
diag = [] | |
@board.size.times do |x| | |
diag << @board[x][y] | |
x += 1 | |
y += 1 | |
end | |
diag.to_s | |
end #Still not 100% with this method as it also returns the a boolean before the array. It's a WIP. | |
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) #=> ["b", "r", "a", "e"] | |
puts boggle_board.get_row(1) #=> ["i", "o", "d", "t"] | |
puts boggle_board.get_row(2) #=> ["e", "c", "l", "r"] | |
puts boggle_board.get_row(3) #=> ["t", "a", "k", "e"] | |
puts boggle_board.get_col(0) #=> ["b", "i", "e", "t"] | |
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(3) #=> ["e", "t", "r", "e"] | |
# create driver test code to retrieve a value at a coordinate here: | |
puts boggle_board.board[3][2] == true | |
# create driver test code to retrieve a diagonal from specified coordinates: | |
puts boggle_board.get_diagonal(0,0) | |
# REFLECTION | |
# It was great building on the code from the previous challenge as it really cemented how a class is constructed | |
# It also made a few things click for me around variables and scope. | |
# As always I was introduced to a few new sneaky methods that aided a lot. | |
# I was really tested with the bonus challenged (get_diagonal) and am not 100% happy with my effort but will | |
# return and update maybe in a pairing session or once I have a pair wave! | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment