Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save chhedarcheese/9122210 to your computer and use it in GitHub Desktop.
Save chhedarcheese/9122210 to your computer and use it in GitHub Desktop.
phase 0 unit 2 week 1boggle class challenge
class BoggleBoard
def initialize(grid)
@grid = grid
end
def create_word(*coords)
coords.map { |coord| @grid[coord.first][coord.last]}.join("")
end
def get_row(row)
@grid[row]
end
def get_col(col)
@grid.map {|row| row[col]}
end
def access_coordinate(coord)
@grid[coord.first][coord.last]
end
# Pseudocode: first figure out if it's a diagonal - if the absolute value of the difference between
# each coordinate spot (first and last) is the same, then it's a diagonal. If it's not a diagonal,
# say so and end it, otherwise continue on to get the diagonal coordinates. Do this by determining
# if the first coordinate's row is greater or less than the second coordinates row. If greater,
# go through the coordinates up one and right one unitl the second coordinate is reached. If less,
# go throught eh coordinate down one and right one until the second coordinate is reached. Make sure
# to save each coordinate by iterating and putting each one in an array.
def get_diagonal(coord1, coord2)
if (coord2.first - coord1.first).abs == (coord2.last - coord1.last).abs
diagonal = []
if coord1.first < coord2.first
while coord1.first <= coord2.first
diagonal << @grid[coord1.first][coord1.last]
coord1 = [coord1.first + 1, coord1.last + 1]
end
elsif coord1.first > coord2.first
while coord1.first >= coord2.first
diagonal << @grid[coord1.first][coord1.last]
coord1 = [coord1.first - 1, coord1.last - 1]
end
else
diagonal << @grid[coord1]
end
return diagonal
else
return "That's not a diagonal."
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:
puts boggle_board.create_word([0,0], [1,1], [0,2], [0,1], [1,2]) #=> returns "board"
puts boggle_board.create_word([2,1], [3,1], [3,2], [3,3]) #=> returns "cake"
puts boggle_board.create_word([1,2], [1,1], [2,1], [3,2]) #=> returns "dock"
puts boggle_board.get_row(0).join("") #=> "brae"
puts boggle_board.get_row(1).join("") #=> "iodt"
puts boggle_board.get_row(2).join("") #=> "eclr"
puts boggle_board.get_row(3).join("") #=> "take" => real word
puts boggle_board.get_col(0).join("") #=> "biet"
puts boggle_board.get_col(1).join("") #=> "roca"
puts boggle_board.get_col(2).join("") #=> "adlek"
puts boggle_board.get_col(3).join("") #=> "etre"
# create driver test code to retrieve a value at a coordinate here:
puts boggle_board.access_coordinate([3,2]) #=> "k"
puts boggle_board.access_coordinate([0,2]) #=> "a"
puts boggle_board.access_coordinate([1,1]) #=> "o"
# bonus
puts boggle_board.get_diagonal([1,1], [3,3])
# Refelction
# Aside from the bonus, these exercises were fairly simply after having done the previous boggle
# exercise. In order to print the rows and columns as string, as the objective 3 asked, I learned
# that I needed to remove the .inspect method in order to use the .join method.
# My diagonal method originally didn't't work. I've went through trouble trying to figure out why
# I was getting the error: rb:34:in `[]': no implicit conversion of Array into Integer (TypeError).
# After finally consulting with a DBC coach, I got that it was because I was doing
# "coord1 = [coord1.first + 1], [coord1.last + 1]" instead of "coord1 = [coord1.first + 1, coord1.last + 1]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment