Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save awertman/9356438 to your computer and use it in GitHub Desktop.

Select an option

Save awertman/9356438 to your computer and use it in GitHub Desktop.
phase 0 unit 2 week 1boggle class challenge
class BoggleBoard
#your code here
class BoggleBoard
def initialize(dice_grid)
@dice_grid = dice_grid
end
def create_word(*grid)
grid.map { |letter| @dice_grid[letter.first][letter.last] }.join("")
end
def get_row(row)
@dice_grid[row-1].join("")
end
def get_col(column)
@dice_grid.map { |row| row.slice(column-1) }.join("")
end
def get_coordinate(row,column)
@dice_grid[row-1][column-1]
end
def get_diagonal(row,column)
counter = 0
diagonal = Array.new
while row + counter <= 4 && column + counter <= 4
diagonal << @dice_grid[row-1+counter][column-1+counter]
counter +=1
end
return diagonal
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.get_row(1)
puts boggle_board.get_col(1)
puts boggle_board.get_row(2)
puts boggle_board.get_col(2)
puts boggle_board.get_row(3)
puts boggle_board.get_col(3)
puts boggle_board.get_row(4)
puts boggle_board.get_col(4)
# brae
# biet
# iodt
# roca
# eclr
# adlk
# take
# create driver test code to retrieve a value at a coordinate here:
puts boggle_board.create_word([1,2],[1,1],[2,1],[3,2]) == "dock"
puts boggle_board.get_row(2) == "iodt"
puts boggle_board.get_col(2) == "roca"
puts boggle_board.get_coordinate(4,3) == "k"
puts boggle_board.get_diagonal(1,2) == ["r","d","r"]
# Reflection
# good exercise in working with classes and objects. these are very comfortable topics for me. more importantly, this exercise
# for me put some focus on nested arrays and how to access different elements. in reading about related topics,
# it was interesting to learn that this is how many video games use positioning on a map, based on these types of
# array coordinates.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment