Forked from dbc-challenges/0.2.1-boggle_class_from_methods.rb
Last active
August 29, 2015 13:56
-
-
Save alexanderfrankel/9199307 to your computer and use it in GitHub Desktop.
phase 0 unit 2 week 1boggle class challenge
This file contains 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(grid) | |
@grid = grid | |
end | |
def create_word(*coords) | |
coords.map { |coord| @grid[coord.first][coord.last]}.join("") | |
end | |
def get_row(row) | |
@grid[row] | |
end | |
def get_col(col) | |
@grid.map { |row| row[col] } | |
end | |
def access_coord(coord) | |
@grid[coord.first][coord.last] | |
end | |
# we will need coord1 as a starting point. | |
# by adding 1 row and 1 column, we will be able to get the next letter. | |
# the row will be increased using iteration, while the column will be increased | |
# using a counter. the stopping point will be coord2 inclusive. | |
def get_diagonal(coord) # how can we make this dynamic????? | |
diagonal = [] | |
col = 0 | |
if coord == [0,0] | |
0.upto(3) do |row| | |
diagonal << @grid[row][col] | |
col += 1 | |
end | |
elsif coord == [3,0] | |
3.downto(0) do |row| | |
diagonal << @grid[row][col] | |
col += 1 | |
end | |
else | |
raise ArgumentError | |
end | |
diagonal | |
end | |
end | |
dice_grid = [["b", "r", "a", "e"], | |
["i", "o", "d", "t"], | |
["e", "c", "l", "r"], | |
["t", "a", "k", "e"]] | |
# the boggle_board object holds the dice_grid as an instance variable | |
boggle_board = BoggleBoard.new(dice_grid) | |
# implement tests for each of the methods here: | |
p boggle_board.create_word([2,1], [1,1], [1,2], [0,3]) # => "code" | |
p boggle_board.create_word([0,1], [0,2], [1,2]) # => "rad" | |
p boggle_board.create_word([3,0], [3,1], [3,2], [3,3]) # => "take" | |
p boggle_board.create_word([1,3], [0,3], [0,2], [0,1]) # => "tear" | |
p boggle_board.create_word([1,2], [1,1], [2,1], [3,2]) # => "dock" | |
puts | |
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"] ---real word. | |
puts # better way to insert a new line?????? | |
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"] | |
puts | |
#OUTPUT: | |
"code" | |
"rad" | |
"take" | |
"tear" | |
"dock" | |
["b", "r", "a", "e"] # row 0 | |
["i", "o", "d", "t"] # row 1 | |
["e", "c", "l", "r"] # row 2 | |
["t", "a", "k", "e"] # row 3 | |
["b", "i", "e", "t"] # col 0 | |
["r", "o", "c", "a"] # col 1 | |
["a", "d", "l", "k"] # col 2 | |
["e", "t", "r", "e"] # col 3 | |
p boggle_board.access_coord([3,2]) # => "k" | |
p boggle_board.access_coord([0,0]) # => "b" | |
p boggle_board.access_coord([3,3]) # => "e" | |
p boggle_board.access_coord([2,2]) # => 'l' | |
p boggle_board.get_diagonal([0,0]) # => ["b", "o", "l", "e"] | |
p boggle_board.get_diagonal([3,0]) # => ["t", "c", "d", "e"] | |
p boggle_board.get_diagonal([1,0]) # => "Argument Error" | |
#REFLECTION: | |
# Again, I am really into this boggle board stuff aka. array of arrays. I really like | |
# the problem solving that goes along with these exercises. I feel like I have a pretty | |
# good understanding of classes, instance methods, and instance variables; although, | |
# I am sure there is more to learn. I am quite confused on the mehthod to return a | |
# diagonal. I understand the column must be increased by 1 every time, so I have created a | |
# working method, but I would like to spend some time refactoring to make it dynamic and to | |
# be able to return a partial diagonal. I like the idea of object-oriented programming. | |
# It makes sense in my mind as I work with newly created objects i.e. boggle boards. | |
# The main difference in the implementation is variable scope. In my opinion, working with | |
# objects (classes) actually makes things a little bit easier and encourages NO REPETITION! | |
# It is a little bit more code that is being written out, but the code is more organized, | |
# easier to read, and in that case that I have a lot of instances, will be very helpful. | |
# Essentially, we are using different procedures within defined classes to create class methods | |
# that can be used over and over again. COOL STUFF!!!! | |
# I would like to become a class master... |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment