Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save brendanscarano/8772169 to your computer and use it in GitHub Desktop.
Save brendanscarano/8772169 to your computer and use it in GitHub Desktop.
phase 0 unit 2 week 1boggle class challenge
class BoggleBoard
attr_reader :grid
def initialize(grid)
@grid = grid
end
def create_word(*coord)
puts coord.map { |coord| grid[coord.first][coord.last]}.join("")
end
def get_row(row)
print grid[row]
end
def get_col(col)
row = 0
new_board = []
raise ArgumentError.new("There is no column #{col}!") if col > (grid.size - 1)
until row >= grid.size
new_board << grid[row][col]
row += 1
end
print new_board
end
def get_diagonal(coord1, coord2)
row_len = grid.length #4
flatten_len = grid.flatten.length #16
#acceptable coordinates
top_left_corner = [0,0]
bottom_right_corner = [(row_len - 1),(row_len - 1)]
top_right_corner = [0,(row_len - 1)]
bottom_left_corner = [(row_len - 1), 0]
#Check to make sure the grid is a perfect square, and can therefore have a diagonal
puts "This grid is not a perfect square, no diagonal can be made!" if row_len ** 2 != flatten_len
#Check to make sure the coordinates are in a diagonal with one another
if (coord1 == top_left_corner && coord2 == bottom_right_corner) || (coord1 == bottom_right_corner && coord2 == top_left_corner)
#b/c we know top_left_corner is being included here
#ie: [0,0] are in this set of coords
#automatically set the row(y) and column(x) to 0
row = 0
col = 0
new_board = []
until row >= grid.size
new_board << grid[row][col]
row += 1
col += 1
end
print new_board
elsif (coord1 == top_right_corner && coord2 == bottom_left_corner) || (coord1 == bottom_left_corner && coord2 == top_right_corner)
row = 0 #b/c we know top_left_corner ie: [0,0] are in this set of coords automatically set the row(y) and column(x) to 0
col = (grid.length - 1)
new_board = []
until row >= grid.size
new_board << grid[row][col]
row += 1
col -= 1
end
print new_board
else
puts "These are not symetrical coordinates for a diagonal in this grid."
end
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.get_row(1) #=> ["i", "o", "d", "t"]
boggle_board.get_col(2) #=> ["a", "d", "l", "k"]
boggle_board.get_diagonal([3,3],[0,0]) #=> ["b", "o", "l", "e"]
boggle_board.get_diagonal([0,3],[3,0]) #=> ["e", "d", "c", "t"]
# create driver test code to retrieve a value at a coordinate here:
boggle_board.create_word([3,2]) #=> k
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment