Forked from dbc-challenges/0.2.1-boggle_class_from_methods.rb
Last active
December 29, 2015 14:39
-
-
Save mathildemouw/7685765 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 | |
end | |
def get_col(col) | |
@board.collect { |row| row[col] }.join | |
end | |
# def get_diagonal(coord1, coord2) | |
# 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 | |
puts boggle_board.create_word([2,1], [1,1], [1,2], [0,3]) #=> returns "code" | |
puts boggle_board.create_word([0,1], [0,2], [1,2]) #=> creates what california slang word? | |
#two more calls: | |
puts boggle_board.create_word([3,0], [3,1], [3,2], [3,3]) #=> "take" | |
puts boggle_board.create_word([0,1], [1,1], [0,2],[1,2]) #=> "road" | |
puts boggle_board.get_row(1) == "iodt" | |
puts boggle_board.get_col(3) == "etre" | |
p boggle_board.get_row(0) #=> "brae" | |
p boggle_board.get_row(1) #=> "iodt" | |
p boggle_board.get_row(2) #=> "eclr" | |
p boggle_board.get_row(3) #=> "take", boggle! | |
p boggle_board.get_col(0) #=> "biet" | |
p boggle_board.get_col(1) #=> "roca" | |
p boggle_board.get_col(2) #=> "adlk" | |
p boggle_board.get_col(3) #=> "etre", French boggle! | |
# create driver test code to retrieve a value at a coordinate here: | |
puts boggle_board.create_word([3,2]) #=> k | |
#Review and Reflect: | |
# | |
#Ruby is my first programming language, so procedural programming feels | |
#naked to me compared to Object Oriented programming. Chermaine and I kind | |
#of delved into object oriented in our first collection of boggle methods | |
#as well, since we had to define boggle_board as an instance variable to get it to work everywhere. | |
# | |
#It is much easier to see what methods can be improved when they are wrapped | |
#into the BoggleBoard class. For instance, the create_word method can also be | |
#used to get a single value at a coordinate, so maybe it should be re-named | |
#to imply that, or changed so it can only create words. | |
# | |
# |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment