Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shkurkin/7297041 to your computer and use it in GitHub Desktop.
Save shkurkin/7297041 to your computer and use it in GitHub Desktop.
phase 0 unit 2 week 1 boggle 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-1]
end
def get_col(col)
@board.transpose[col-1]
end
def get_coor(row,col)
@board[row][col]
end
def get_diagonal(start_row, start_col, end_row, end_col)
raise IndexError, "Not a diagonal" if (start_row - end_row).abs != (start_col - end_col).abs
if start_col > end_col
start_row, end_row = end_row, start_row
start_col, end_col = end_col, start_col
end
diagonal = []
(end_col - start_col).times do
diagonal << @board[start_row][start_col]
start_col += 1
start_row < end_row ? start_row += 1 : start_row -= 1
end
diagonal << @board[start_row][start_col]
end
def print
0..@board.length.times do |i|
puts get_row(i+1).join
puts get_col(i+1).join
end
end
end
dice_grid = [["b", "r", "a", "e"],
["i", "o", "d", "t"],
["e", "c", "l", "r"],
["t", "a", "k", "e"]]
boggle = BoggleBoard.new(dice_grid)
puts boggle.create_word([2,1], [1,1], [1,2], [0,3]) == "code"
puts boggle.create_word([0,1], [0,2], [1,2]) == "rad"
puts boggle.create_word([3,0], [3,1], [2,1], [3,2], [2,2], [3,3]) == "tackle"
puts boggle.create_word([0,0], [1,1], [2,2], [1,2]) == "bold"
puts boggle.create_word([1,2], [1,1], [2,1], [3,2]) == "dock"
puts boggle.get_row(2) == %w[i o d t]
puts boggle.get_row(3) == %w[e c l r]
puts boggle.get_row(4) == %w[t a k e]
puts boggle.get_col(1) == %w[b i e t]
puts boggle.get_col(2) == %w[r o c a]
puts boggle.get_col(3) == %w[a d l k]
boggle.print #=>
#brae
#biet
#iodt
#roca
#eclr
#adlk
#take Woo! Real word!
#etre
puts boggle.get_coor(3,2) == "k"
puts "---------------------------"
puts boggle.get_diagonal(0,0,3,3).join == "bole"
puts boggle.get_diagonal(1,0,0,1).join == "ir"
puts boggle.get_diagonal(1,0,3,2).join == "ick"
puts boggle.get_diagonal(3,1,1,3).join == "alt"
puts boggle.get_diagonal(3,3,0,0).join == "bole"
puts boggle.get_diagonal(0,1,1,0).join == "ir"
puts boggle.get_diagonal(3,2,1,0).join == "ick"
puts boggle.get_diagonal(1,3,3,1).join == "alt"
@shkurkin
Copy link
Author

shkurkin commented Nov 5, 2013

The real challenge here was the diagonal class. The current implementation works, but it seems hackish to me, which usually means that there's a better way that I'm not seeing.
One big take-away from the diagonal function was that most of my problems come from not fully understanding the thing I'm working on. Ruby is expressive, easy and predictable. Real world problems aren't. For the diagonal, I spent a good amount of time coming up with a mental model of accessing diagonals in the array that the computer would understand. I eventually came up with:

  1. read the diagonal left to right (convert to make this happen if necessary), this way the next cell will always be col+1
  2. compare rows in the start position and end position the see whether the diagonal goes up or down.
  3. end_col - start_col to see how long the diagonal is

If you have a different method, please post! I would love to improve this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment