Forked from dbc-challenges/0.2.1-boggle_class_from_methods.rb
Last active
August 29, 2015 13:56
-
-
Save kenjim83/9107944 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 | |
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 { |row| row[col] } | |
end | |
def diagonal?(coord1, coord2) | |
(coord1.first - coord2.first).abs == (coord1.last - coord2.last).abs ? true : false | |
end | |
def get_diagonal(coord1, coord2) | |
if diagonal?(coord1, coord2) | |
if coord1[0] < coord2[0] && coord1[1] < coord2[1] | |
(coord1[0]..coord2[0]).collect { |x| @dice_grid[ x][ x] } # 1) top left => bottom right | |
elsif coord1[0] > coord2[0] && coord1[1] > coord2[1] | |
(coord2[0]..coord1[0]).collect { |x| @dice_grid[-x-1][-x-1] } # 2) bottom right => top left | |
elsif coord1[0] > coord2[0] && coord1[1] < coord2[1] | |
(coord1[1]..coord2[1]).collect { |x| @dice_grid[-x-1][ x] } # 3) top right => bottom left | |
else | |
(coord2[1]..coord1[1]).collect { |x| @dice_grid[ x][-x-1] } # 4) bottom left -> top right | |
end | |
else | |
return "Coordinates are NOT diagonal to each other." | |
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) | |
# implement tests for each of the methods here: | |
p boggle_board.create_word([1,2], [1,1], [2,1], [3,2]) #=> Should be 'dock' | |
p boggle_board.create_word([0,0], [1,1], [2,2], [1,2]) #=> Should be 'bold' | |
p boggle_board.create_word([3,0], [3,1], [2,1], [3,2], [2,2], [3,3]) #=> Should be 'tackle' | |
p '----------' | |
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 | |
# Output: | |
# "brae" | |
# "iodt" | |
# "eclr" | |
# "take" | |
# "biet" | |
# "roca" | |
# "adlk" | |
# "etre" | |
# I see the real words "take" and "etre" (French) | |
# create driver test code to retrieve a value at a coordinate here: | |
p boggle_board.create_word([0,0], [1,1], [2,2], [1,2]) == 'bold' #=> Should be true | |
p boggle_board.create_word([3,0], [3,1], [2,1], [3,2], [2,2], [3,3]) == 'tackle' #=> Should be true | |
p boggle_board.create_word([1,1]) == 'o' #=> Should be true | |
p boggle_board.create_word([1,3]) == 't' #=> Should be true | |
p boggle_board.get_row(0) == ["b", "r", "a", "e"] #=> Should be true | |
p boggle_board.get_row(1) == ["i", "o", "d", "t"] #=> Should be true | |
p boggle_board.get_row(2) == ["e", "c", "l", "r"] #=> Should be true | |
p boggle_board.get_row(3) == ["t", "a", "k", "e"] #=> Should be true | |
p boggle_board.get_col(0) == ["b", "i", "e", "t"] #=> Should be true | |
p boggle_board.get_col(1) == ["r", "o", "c", "a"] #=> Should be true | |
p boggle_board.get_col(2) == ["a", "d", "l", "k"] #=> Should be true | |
p boggle_board.get_col(3) == ["e", "t", "r", "e"] #=> Should be true | |
p boggle_board.get_diagonal([0,0], [3,3]) == ["b", "o", "l", "e"] #=> Should be true | |
p '---------' | |
p boggle_board.diagonal?([1,1], [0,0]) # Should be true. | |
p boggle_board.diagonal?([1,1], [0,1]) # Should be false. Not diagonal coords. | |
p boggle_board.diagonal?([2,0], [3,1]) # Should be true. | |
p '---------' | |
p boggle_board.get_diagonal([0,0], [3,3]) | |
p boggle_board.get_diagonal([3,3], [0,0]) | |
p boggle_board.get_diagonal([0,3], [3,0]) | |
p boggle_board.get_diagonal([3,0], [0,3]) | |
p '---------' | |
p boggle_board.get_diagonal([1,2], [2,1]) | |
p boggle_board.get_diagonal([3,3], [0,0]) | |
p boggle_board.get_diagonal([3,1], [3,0]) # Not diagonal coords. Should throw error. | |
# Reflection | |
# It was easy to first transfer the code from the previous exercise to class methods. You just copy the methods, | |
# paste into the the class and indent 1 tab. I did have to change the instance variable names though for the code to run. | |
# The really challenging part was the bonus #diagonal method. Here, the driver code was essential to write ahead of time | |
# so I could play around with the 4 possible "directions" to form the diagonal array. They were: | |
# | |
# 1) top left => bottom right | |
# 2) bottom right => top left | |
# 3) top right => bottom left | |
# 4) bottom left -> top right | |
# I figured out the the 1st case, and then the 2nd case was the same conditions but reversed. | |
# After tweaking the 3rd case I finally got it to work. Lastly the 4th case was a reverse of the 3rd. | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment