Forked from dbc-challenges/0.2.1-boggle_class_from_methods.rb
Last active
August 29, 2015 13:55
-
-
Save teedang19/8713711 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 :nested_board | |
def initialize(nested_board) | |
@nested_board = nested_board | |
end | |
def create_word(*coords) | |
coords.map{|coord| @nested_board[coord.first][coord.last]}.join | |
end | |
def get_row(row) | |
@nested_board[row] | |
end | |
def get_col(col) | |
column = [] | |
@nested_board.each {|layer| column << layer[col]} | |
column | |
end | |
def get_coord(letter) | |
coords = [] | |
@nested_board.each {|layer| | |
if layer.include?(letter) | |
coord = [] | |
coord << @nested_board.index(layer) | |
coord << layer.index(letter) | |
coords << coord | |
end | |
} | |
return coords[0] if coords.count == 1 | |
coords | |
end | |
=begin | |
def get_diagonal(coord1, coord2) | |
raise ArgumentError.new("You must input two coordinates on a diagonal.") if coord1[0]-coord2[0] != coord1[1] - coord2[1] | |
a = coord1[0] | |
b = coord1[1] | |
c = coord2[0] | |
d = coord2[1] | |
end | |
=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) # The boggle_board object, which is an instance of the BoggleBoard class, holds the dice_grid as an attribute that is passed in as a parameter when boggle_board is initialized | |
puts boggle_board.create_word([1,2], [1,1], [2,1], [3,2]) # => "dock" | |
puts boggle_board.create_word([2,1], [1,1], [1,2], [0,3]) # => "code" | |
puts boggle_board.create_word([0,1], [0,2], [1,2]) # => "rad" | |
puts boggle_board.create_word([2,1], [2,2], [1,1], [1,2]) #=> "clod" | |
puts boggle_board.create_word([0,0], [1,0], [2,1], [3,2], [3,3], [2,3]) # => "bicker" | |
p boggle_board.get_row(1) == ["i", "o", "d", "t"] # => true | |
p boggle_board.get_row(3) == ["t", "a", "k", "e"] # => true | |
p boggle_board.get_col(0) == ["b", "i", "e", "t"] # => true | |
p boggle_board.get_col(1) == ["r", "o", "c", "a"] # => true | |
for x in 0...boggle_board.nested_board.count # => "brae", "iodt", "eclr", "take" | |
p boggle_board.get_row(x).join("") # "take" is a word. | |
end | |
for x in 0...boggle_board.nested_board.count # => "biet", "roca", "adlk", "etre" | |
p boggle_board.get_col(x).join("") # "etre" is the verb "to be" in French | |
end | |
p boggle_board.get_coord("k") == [3,2] # true | |
p boggle_board.nested_board[3][2] == "k" # true. we can access the @nested_board property bc of the attr_accesor :nested_board in the beginning of the code | |
p boggle_board.get_coord("e") == [[0,3], [2,0], [3,3]] # true | |
p boggle_board.get_coord("d") == [1,2] # true | |
p boggle_board.nested_board[1][2] == "d" # true | |
=begin | |
p boggle_board.get_diagonal([0,0], [3,3]) | |
p boggle_board.get_diagonal([3,2], [1,0]) | |
p boggle_board.get_diagonal([2,0], [0,3]) | |
p boggle_board.get_diagonal([2,2], [1,2]) | |
=end | |
=begin | |
REFLECTION | |
Object oriented programming is different from procedural programming in that it creates non-instance specific, reusable code | |
in which you can instantiate objects that will have behaviors and attributes similar to that reusable code block | |
while still being capable of holding its own unique traits, if desired. In this way, we can create many "BoggleBoard" objects | |
with their own unique dice grids, that all behave the same way -- they can return words with coordinates, can return rows and columns - | |
but are different in that they all hold different letters in their grids. A benefit or object-oriented programming: We can do this without having | |
to duplicate the procedural code, because that procedural code is stored within an object called a Class, from which new objects can be made. YES. | |
BUMMER that I wasn't able to create the get_diagonal method, but it's time to move on. Will probably come back to it .... | |
=end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment