Forked from dbc-challenges/0.2.1-boggle_class_from_methods.rb
Last active
August 29, 2015 13:56
-
-
Save ETNOL/9180256 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(grid) | |
@grid = grid | |
end | |
def create_word(*coords) | |
coords.map { |coord| @grid[coord.first][coord.last] }.join("") | |
end | |
def get_row(row) | |
@grid[row].join(" ") | |
end | |
def get_col(col) | |
@grid.map {|row| row[col]}.join(" ") | |
end | |
def get_diagonal(coord1, coord2) | |
return "Not a diagonal" unless is_diagonal? | |
end | |
def is_diagonal? | |
coord1.first - coord2.first == coord1.last - coord2.last | |
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([0,0],[0,1],[1,1]) == "bro" | |
p boggle_board.create_word([2,1], [3,1], [3,2], [3,3]) == "cake" | |
p boggle_board.create_word([2,1], [3,1], [2,2], [1,1], [0,1], [1,0], [2,0]) == "calorie" | |
p boggle_board.get_row(1) == "i o d t" | |
p boggle_board.get_row(2) == "e c l r" | |
p boggle_board.get_col(2) == "a d l k" | |
p boggle_board.get_col(1) == "r o c a" | |
# Reflection | |
# Classes are definately a great way of organizing data. I hear a lot about their validity in future applications vs. | |
# functional programming due to concurrency issues. I don't completely grasp this yet. I imagine it'll make a great | |
# read one day. I started to attempt the get_diagonal method. I originally had a good grasp of how to tackle it. | |
# However, once I realized diagonals are not isolated to downward sloping left to right conditions, I realized my | |
# strategy was flawed. I did delete that code in preparation of going at it again but I've ended up skipping it. | |
# | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment