Forked from dbc-challenges/0.2.1-boggle_class_from_methods.rb
Last active
August 29, 2015 13:55
-
-
Save raderj89/8787793 to your computer and use it in GitHub Desktop.
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(dice_grid) | |
@dice_grid = dice_grid | |
end | |
def create_word(*coords) | |
coords.map { |coord| @dice_grid[coord.first][coord.last] }.join("") | |
end | |
def get_row(row) | |
@dice_grid[row] | |
end | |
def get_col(col) | |
@dice_grid.map { |x| x[col] } | |
end | |
def is_diagonal?(coord1, coord2) | |
if (coord1.first - coord2.first).abs != (coord1.last - coord2.last).abs | |
raise ArgumentError.new("not a valid diagonal") | |
end | |
end | |
def get_diagonal(coord1, coord2) | |
is_diagonal?(coord1, coord2) | |
diag_ary = [] | |
incrementer = 0 | |
#top-bottom, left-right | |
if coord1.first < coord2.first && coord1.last < coord2.last | |
(coord1.first..coord2.first).each do | |
diag_ary << @dice_grid[coord1.first + incrementer][coord1.last + incrementer] | |
incrementer += 1 | |
end | |
#top-bottom, right-left | |
elsif coord1.first < coord2.first && coord1.last > coord2.last | |
(coord1.first..coord2.first).each do | |
diag_ary << @dice_grid[coord1.first + incrementer][coord1.last - incrementer] | |
incrementer += 1 | |
end | |
#bottom-top, left-right | |
elsif coord1.first > coord2.first && coord1.last < coord2.last | |
(coord2.first..coord1.first).each do | |
diag_ary << @dice_grid[coord1.first - incrementer][coord1.last + incrementer] | |
incrementer += 1 | |
end | |
#bottom-top, right-left | |
elsif coord1.first > coord2.first && coord1.last > coord2.last | |
(coord2.first..coord1.first).each do | |
diag_ary << @dice_grid[coord1.first - incrementer][coord1.last - incrementer] | |
incrementer += 1 | |
end | |
end | |
diag_ary | |
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) | |
p boggle_board.create_word([1,2], [1,1], [2,1], [3,2]) == "dock" | |
p boggle_board.get_row(1) == ["i", "o", "d", "t"] | |
p boggle_board.get_col(3) == ["e", "t", "r", "e"] | |
puts | |
p boggle_board.get_row(0).join("") | |
p boggle_board.get_row(1).join("") | |
p boggle_board.get_row(2).join("") | |
p boggle_board.get_row(3).join("") | |
p boggle_board.get_col(0).join("") | |
p boggle_board.get_col(1).join("") | |
p boggle_board.get_col(2).join("") | |
p boggle_board.get_col(3).join("") | |
=begin | |
"brae" | |
"iodt" | |
"eclr" | |
"take" | |
"biet" | |
"roca" | |
"adlk" | |
"etre" | |
=end | |
puts | |
p boggle_board.get_col(1)[1] #=> "o" | |
puts | |
p boggle_board.get_row(3)[2] | |
puts | |
#top-bottom, left-right | |
p boggle_board.get_diagonal([0,0], [3,3]) #=> ["b", "o", "l", "e"] | |
p boggle_board.get_diagonal([0,0], [2,2]) #=> ["b", "o", "l"] | |
p boggle_board.get_diagonal([1,1], [3,3]) #=> ["o", "l", "e"] | |
#top-bottom, right-left | |
p boggle_board.get_diagonal([0,3], [3,0]) #=> ["e", "d", "c", "t"] | |
#bottom-top, left-right | |
p boggle_board.get_diagonal([3,0], [0,3]) #=> ["t", "c", "d", "e"] | |
#bottom-top, right-left | |
p boggle_board.get_diagonal([3,3], [0,0]) #=> ["e", "l", "o", "b"] | |
p boggle_board.get_diagonal([1,1], [2,2]) #=> ["o", "l"] | |
p boggle_board.get_diagonal([3,0], [0,3]) #=> ["t", "c", "d", "e"] | |
p boggle_board.get_diagonal([2,1], [0,3]) #=> ["c", "d", "e"] | |
p boggle_board.get_diagonal([2,1], [3,2]) #=> ["c", "k"] | |
p boggle_board.get_diagonal([1,0], [2,1]) #=> ["i", "c"] | |
p boggle_board.get_diagonal([3,2], [2,1]) #=> ["k", "c"] | |
# p boggle_board.get_diagonal([5,4], [8,3]) #=> raises argument error | |
=begin | |
Object-oriented programming can help break pieces of logic out into separate methods so as to keep other methods short and | |
readable. | |
THOUGHTS ON GET DIAGONAL: | |
I don't think I've spent a longer time on another code challenge. It wasn't until I remembered the concept of absolute value | |
that I found a way I could solve this challenge. I drew out the boggle board and examined what made a diagonal value. | |
Eventually, I realized that if the absolute value of subtracting the corresponding coordinates equaled the absolute value of | |
subtracting the other coordinates, then the values were diagonal. (I believe that there are other truths associated with the | |
numbers, such as the absolute value of subtracting two coordinates must also equal the addition of the other two coordinates, | |
but I'm focusing on absolute value of subtracting right now). | |
At that point, it became fairly straightforward how to find each diagonal value between the two coordinates and push them | |
into an array. If the first coordinate's first value is less than the second coordinate's first value, AND the first | |
coordinate's second value is less than the second coordinate's second value, i.e. [0,0] and [3,3], then I knew I would have to | |
keep incrementing the first coordinate's first and second values by one, until they equaled the second coordinate's values, | |
pushing the letters of each incremented coordinate. To accomplish this, I knew I would have to iterate by lesser first | |
coordinate up to and including greater first coordinate times. I then had to account for every possible variation, as | |
described in the code. | |
I think I'll give this a rest for a day or two and come back later to refactor. I'm sure it could be made a bit DRYer. | |
=end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment