Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save yoguido77/8376602 to your computer and use it in GitHub Desktop.
Save yoguido77/8376602 to your computer and use it in GitHub Desktop.
Create a boggle board class
class BoggleBoard
# 1. Instantiate new board object
attr_reader :board
def initialize(board)
@board = board
end
def print_board
@board.each { |row| p row} #prints 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
# Refresh board
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]) # => ["t","a", "k", "e"]
p boggle_board.create_word([2,1], [1,1], [3,2], [0,3]) #=> ["c","o","k","e"]
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[1][3]
# Test the method to get a new board
boggle_board.new_board
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment