Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save menor/7704203 to your computer and use it in GitHub Desktop.
Save menor/7704203 to your computer and use it in GitHub Desktop.
phase 0 unit 2 week 1 boggle class challenge
class BoggleBoard
attr_reader :board
def initialize(board)
@board = board
end
def print_board
@board.each { |row| p row}
end
def create_word(*coords)
coords.map { |coord| @board[coord.first][coord.last]}.join("")
end
def get_row(row_number)
@board[row_number]
end
def get_col(col)
@board.map {|row| row[col] }
end
def new_board(rows=4, cols=4)
@board = Array.new(rows) { Array.new(cols) { ('a'..'z').to_a.sample } }
print_board
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:
boggle_board.print_board
p boggle_board.create_word([3,0..3]) # => take
p boggle_board.create_word([1,2], [1,1], [2,1], [3,2]) #=>dock
p boggle_board.get_row(1).join
p boggle_board.get_col(0).join
# create driver test code to retrieve a value at a coordinate here:
p boggle_board.board[1][1]
p boggle_board.board[1][2]
p boggle_board.board[3][2]
# Test the method to get a new board
boggle_board.new_board
# Worked with Eric on this one, we basically recreated previous exercise encapsulating
# everything in a class, I already did this on last exercise, so not much to reflect,
# Added a method to refresh the board, this could be done by creating sixteen dice objects
# and throwing them to make it more realistic, but I found it overkill for this challenge.
# I've tried to get the diagonal method working but I couldn't understand what they were
# asking for in the challenge and time was running out so I didn't insisted much.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment