Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save ameliapadua/8392544 to your computer and use it in GitHub Desktop.
Save ameliapadua/8392544 to your computer and use it in GitHub Desktop.
phase 0 unit 2 week 1 boggle 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_letter(row, col)
@board[row][col]
end
def get_row(row)
@board[row]
end
def get_col(col)
@board.map { |row| row[col] }
end
def print_board
@board.map { |row| row.to_s }
end
def get_diagonal(coord1, coord2)
x1, y1 = coord1
x2, y2 = coord2
unless (x2 - x1).abs == (y2 - y1).abs
raise ArgumentError.new("The coordinates must be on a diagonal")
end
diagonal_arr = Array.new
row = x1
col = y1
if x2 - x1 != y2 - y1 # if positive slope
until row == 3 || col == 0
row += 1
col -= 1
end
until row < 0 || col > 3
diagonal_arr << @board[row][col]
row -= 1
col += 1
end
else
until row == 0 || col == 0
row -= 1
col -= 1
end
until row > 3 || col > 3
diagonal_arr << @board[row][col]
row += 1
col += 1
end
end
diagonal_arr.join
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:
# create_word tests:
puts boggle_board.create_word([2,1], [1,1], [1,2], [0,3]) #=> returns "code"
puts boggle_board.create_word( [0,1], [0,2], [1,2]) #=> returns "rad"
puts boggle_board.create_word([1,3], [2,3], [3,3], [3,2]) #=> returns "trek"
puts boggle_board.create_word([3,0], [3,1], [2,1], [1,1]) #=> returns "taco"
# get_row tests:
p boggle_board.get_row(0) #=> returns ["b", "r", "a", "e"]
p boggle_board.get_row(1) #=> returns ["i", "o", "d", "t"]
p boggle_board.get_row(2) #=> returns ["e", "c", "l", "r"]
p boggle_board.get_row(3) #=> returns ["t", "a", "k", "e"] A real word!
# get_col tests:
p boggle_board.get_col(0) #=> returns ["b", "i", "e", "t"]
p boggle_board.get_col(1) #=> returns ["r", "o", "c", "a"]
p boggle_board.get_col(2) #=> returns ["a", "d", "l", "k"]
p boggle_board.get_col(3) #=> returns ["e", "t", "r", "e"]
# create driver test code to retrieve a value at a coordinate here:
# using a method we already had
puts boggle_board.create_word([3,2]) #=> returns "k"
# created a new method to get a letter
puts boggle_board.get_letter(3,2) #=> returns "k"
# driver test code for #get_diagonal
puts boggle_board.get_diagonal([1,1], [3,3]) #=> returns "bole"
puts boggle_board.get_diagonal([2,1], [0,3]) #=> returns "tcde"
# Reflection
# The nice thing about object-oriented programming is that you can create an object and call custom
# methods on that object instead of passing the same object as a parameter to all of your methods.
# Then you can create ways to see what is in that object so you don't have to remember all the changes
# you've made to it. I'm looking forward to seeing how others have solved the #get_diagonal method! I
# wasn't sure if the points that would be passed to the method would be the end points of the diagonal,
# or any point on the diagonal.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment