Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save blooies/9116890 to your computer and use it in GitHub Desktop.
Save blooies/9116890 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_coord(single_coord)
@board[single_coord.first][single_coord.last]
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([1,2],[1,1],[2,1],[3,2]) #=> dock
# expected output
#["b","r","a","e"]
#["i","o","d","t"]
#["e","c","l","r"]
#["t","a","k","e"]
#["b","i","e","t"]
#["r","o","c","a"]
#["a","d","l","k"]
#["e","t","r","e"]
# real words:
# take
p boggle_board.get_row(0)
p boggle_board.get_row(1)
p boggle_board.get_row(2)
p boggle_board.get_row(3)
p boggle_board.get_col(0)
p boggle_board.get_col(1)
p boggle_board.get_col(2)
p boggle_board.get_col(3)
# create driver test code to retrieve a value at a coordinate here:
p boggle_board.get_coord([3,2])
#reflect
# for object oriented programming, you have to create a class, and create an initialize method to
# instantiate variables you pass through when creating an instance of that class.
# you create an instance of the class by doing ClassName.new and assigning it to a variable.
# you can tell call the methods in that class on that newly created object.
# the benefits as you can see from the previous boggle_board challenge,
# is that you dont have to include the actual boggle_board in each individual method.
# with object oriented programming, you can pass the board as an argument when creating
# the instance, and since it will be initialized, it can then be accessed in all the methods.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment