Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save kaelinbu/8756661 to your computer and use it in GitHub Desktop.
Save kaelinbu/8756661 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)
puts @board[row].inspect
end
def get_col(col)
puts @board.transpose[col].inspect
end
def get_diagonal(coord1, coord2)
word = []
word = @board[coord1.first][coord1.last]
word << @board[coord2.first][coord2.last]
coord3 = coord2.collect {|n| n =+ 2}
word << @board[coord3.first][coord3.last]
coord4 = coord3.collect {|n| n =+ 3}
word << @board[coord4.first][coord4.last]
return word
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:
puts boggle_board.create_word([1,2], [1,1], [2,1], [3,2]) #=> dock
boggle_board.get_row(0) #=> returns real word brae
boggle_board.get_col(3) #=> returns ["e", "t", "r", "e"]
boggle_board.get_diagonal([0,0],[1,1]) #=> returns bole
# create driver test code to retrieve a value at a coordinate here:
puts boggle_board.create_word([3,2]) == "k"
puts boggle_board.create_word([3,1]) == "a"
puts boggle_board.create_word([0,3]) == "e"
#Output of each row and column
#boggle_board.get_row(0) #=> returns real word brae
# boggle_board.get_row(1)
# boggle_board.get_row(2)
# boggle_board.get_row(3) #=> returns real word take
# boggle_board.get_col(0)
# boggle_board.get_col(1)
# boggle_board.get_col(2)
# boggle_board.get_col(3)
#Reflection
# I found it extremely helpful to take the singleton methods from the previous exercise and see how they transform once put in a
# method. I made a start on the diagonal method, though I know it's not at all DRY and doesn't account for backward diagonals, or
# diagonals of less than 4 cooridnates (or test to see if the two coordinates are even diagonals of each other), so it's got a
# long way to go there.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment