Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save mjafshar/8329252 to your computer and use it in GitHub Desktop.
Save mjafshar/8329252 to your computer and use it in GitHub Desktop.
phase 0 unit 2 week 1boggle class challenge
class BoggleBoard
def initialize(board)
@board = board
end
def create_word(*coords)
coords.map { |coord| @board[coord.first][coord.last]}.join('')
end
def get_row(row)
@board[row]
end
def get_col(col)
@board.map { |row| row[col] }
end
def get_diagonal(coord1, coord2)
# finds dist btwn first pnts and dist btwn last pnts, if equal, coords are diag
row_dist = (coord1.first - coord2.first)
col_dist = (coord1.last - coord2.last)
if row_dist.abs == col_dist.abx
# builds row and col arrays, if first coord is larger builds array backwards. Allows for multi-directional words.
coord1.first < coord2.first ? row_ary = coord1.first.upto(coord2.first).to_a : row_ary = coord1.first.downto(coord2.first).to_a
coord1.last < coord2.last ? col_ary = coord1.last.upto(coord2.last).to_a : col_ary = coord1.last.downto(coord2.last).to_a
# builds coord array by "zipping" the row array and the column array
coords = row_ary.zip(col_ary)
# enums over coord array and passes each coord into the #create_word method
coords.map { |coord| create_word(coord) }.join('')
else
raise ArgumentError, "Your coords are not diagonal"
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([2,1], [1,1], [1,2], [0,3]) == "code" #=> true
p boggle_board.create_word([0,1], [0,2], [1,2]) == "rad" #=> true
p boggle_board.create_word([1,2], [1,1], [2,1], [3,2]) == "dock" #=> true
puts
p boggle_board.get_row(0) == ["b", "r", "a", "e"] #=> true
p boggle_board.get_row(1) == ["i", "o", "d", "t"] #=> true
p boggle_board.get_row(2) == ["e", "c", "l", "r"] #=> true
p boggle_board.get_row(3) == ["t", "a", "k", "e"] #=> true
puts
p boggle_board.get_col(0) == ["b", "i", "e", "t"] #=> true
p boggle_board.get_col(1) == ["r", "o", "c", "a"] #=> true
p boggle_board.get_col(2) == ["a", "d", "l", "k"] #=> true
p boggle_board.get_col(3) == ["e", "t", "r", "e"] #=> true
puts
p boggle_board.get_diagonal([0,0], [3,3]) == "bole" #=> true
p boggle_board.get_diagonal([3,3], [1,1]) == "elo" #=> true
p boggle_board.get_diagonal([1,0], [3,2]) == "ick" #=> true
p boggle_board.get_diagonal([0,1], [3,3])
# create driver test code to retrieve a value at a coordinate here:
p boggle_board.create_word([3,2]) == "k" #=> true
# Review and Reflect!
# This was a very challenging exercise. I got too ambitious in the beginning and ended up with jumbled and incoherent code
# that wouldn't pass. It was such a mess I couldn't find where to fix it, so I ended up having to rewrite the #get_diagonal
# method. This time I built it step by step, testing as I went. I found that after fumbling through it the first time
# I was better able to understand the challenge and what was needed to solve it.
# One of my issues in the beginning was trying to build separate methods for validating diagonal coords and building a
# coord array. I think I need a better explanation of scope and method referencing in order to make my code as lean
# DRY as possible.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment