Forked from dbc-challenges/0.2.1-boggle_class_from_methods.rb
Last active
August 29, 2015 13:55
-
-
Save rbenapfl/8779700 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
#input:board, coordinates for making a word,row number, column number | |
#ouput:instance varible,created word,array for row, array for column | |
#We need to make an initalize method and set the board as a parameter then create and instance variable for the other methods to use | |
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 | |
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]) #=> "rick" | |
boggle_board.get_row(1) #=> ["i","o","d","t"] | |
boggle_board.get_col(1) #=> ["r","o","c","a"] | |
# create driver test code to retrieve a value at a coordinate here: | |
boggle_board.create_word[3,2] #=> "k" | |
#this is the one I wanted all along | |
boggle_board.board[3][2] #=>"k" | |
#The object orientated approach is implementing a class to store all the methods we would need to model a boggle board in this case | |
#The main difference in this problem was being able to store the board as part of a new object and then call the methods with the board already stored | |
#I was a bit confused on the last bit of driver code I made. I though that if my intialize created an attr_accessor :board then i would be able to | |
#call that as a method "boggle_board.board[3,2]" except I was a bit unclear on the syntax for that since we were passing a parameter to it | |
#I didn't do a refactor on this one because I believe me methods were already clear and concise however I would have liked using the attr in order to do that last driver code. | |
#That would have made a lot more since being able to access the board that I initialized in that way instead of using the method that was designed for a slightly different purpose. | |
#OKAY I FOUND OUT HOW TO DO IT WAS TRYING TO PASS IT INSIDE THE INITIALIZE METHOD NOW I FIXED IT WOW |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment