Forked from dbc-challenges/0.2.1-boggle_class_from_methods.rb
Last active
August 29, 2015 13:56
-
-
Save nadiakg/9164340 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
# In the previous challenge I had to pass an additional parameter for each method - board. | |
# In this case I am working with classes so I removed the board parameter and created the instance variable | |
# @board in the initialize method so I can access it anywhere within the BoggleBoard class. | |
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] | |
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) # this initializes a class BoggleBoard with a nested array dice_grid | |
# implement tests for each of the methods here: | |
# I also had to change the test code syntax since we are now working with methods within the class (instance methods) | |
p boggle_board.create_word([1,2], [1,1], [2,1], [3,2]) #=> "dock" | |
p boggle_board.create_word([0,1], [1,1], [2,1], [3,2]) #=> "rock" | |
p boggle_board.create_word([0,1], [0,2], [1,2], [0,2], [0,1]) #=> "radar" | |
#========================================================== | |
p boggle_board.get_row(0) #=> ["b", "r", "a", "e"] | |
p boggle_board.get_row(1) #=> ["i", "o", "d", "t"] | |
p boggle_board.get_row(2) #=> ["e", "c", "l", "r"] | |
p boggle_board.get_row(3) #=> ["t", "a", "k", "e"] | |
p boggle_board.get_row(0) == ["b", "r", "a", "e"] #=> true | |
p boggle_board.get_row(1) == ["i", "o", "d", "t"] #=> true | |
p boggle_board.get_row(2) == ["e", "c", "l", "r"] #=> true | |
p boggle_board.get_row(3) == ["t", "a", "k", "e"] #=> true | |
#=========================================================== | |
p boggle_board.get_col(0) #=> ["b", "i", "e", "t"] | |
p boggle_board.get_col(1) #=> ["r", "o", "c", "a"] | |
p boggle_board.get_col(2) #=> ["a", "d", "l", "k"] | |
p boggle_board.get_col(3) #=> ["e", "t", "r", "e"] | |
p boggle_board.get_col(0) == ["b", "i", "e", "t"] #=> true | |
p boggle_board.get_col(1) == ["r", "o", "c", "a"] #=> true | |
p boggle_board.get_col(2) == ["a", "d", "l", "k"] #=> true | |
p boggle_board.get_col(3) == ["e", "t", "r", "e"] #=> true | |
# Driver test code to print out all the rows and columns of the board as strings | |
p boggle_board.get_row(0).join("") == "brae" #=> true | |
p boggle_board.get_row(1).join("") == "iodt" #=> true | |
p boggle_board.get_row(2).join("") == "eclr" #=> true | |
p boggle_board.get_row(3).join("") == "take" #=> true | |
p boggle_board.get_col(0).join("") == "biet" #=> true | |
p boggle_board.get_col(1).join("") == "roca" #=> true | |
p boggle_board.get_col(2).join("") == "adlk" #=> true | |
p boggle_board.get_col(3).join("") == "etre" #=> true | |
# Total output | |
=begin | |
"brae" => real word | |
"iodt" => abbreviation - http://www.linkedin.com/groups/What-is-different-between-IODT-1180727.S.53237304 | |
"eclr" => abbreviation - http://wiki.humanities.manchester.ac.uk/ECLR/index.php/Main_Page | |
"take" => real word | |
"biet" => Danish word | |
"roca" => abbreviation - http://en.wikipedia.org/wiki/Roca | |
"adlk" => abbreviation - http://www.bva.bund.de/DE/Organisation/Abteilungen/Abteilung_ZfA/Bewerbung/ADLK/adlk-inhalt.html | |
"etre" => French word | |
=end | |
# create driver test code to retrieve a value at a coordinate here: | |
puts boggle_board.create_word([3,2]) #=> k | |
puts boggle_board.create_word([0,2]) #=> a | |
puts boggle_board.create_word([2,3]) #=> r | |
puts boggle_board.create_word([1,0]) #=> i | |
# REFLECTION | |
# Working with the BoggleBoard class in this challenge wasn't very difficult. If you understand classes and how | |
# to do method calls then this excercise becomes a matter of twiking a few things to make it work. I have to say | |
# I do prefer object oriented approach to procedural approach. It makes your code so much more readable, reusable, | |
# and it becomes easier to manipulate. I am still working on the bonus section. Even if I won't complete it this | |
# week I will come back to it in the next couple of weeks and try again for myself and not to score an extra credit. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment