Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save euwest/9181603 to your computer and use it in GitHub Desktop.
Save euwest/9181603 to your computer and use it in GitHub Desktop.
phase 0 unit 2 week 1boggle class challenge
class BoggleBoard
attr_reader :board
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
#BONUS METHOD: get_diagonal
def get_diagonal(coord1, coord2)
if coord1[0] - coord2[0] == coord1[1] - coord2[1]
diagonal = []
i = 0
@board.each do |row|
if coord1[0] < coord1[1]
diagonal << row[coord1[1] + i - coord1[0]] unless coord1[1] + i >= row.count
elsif coord1[0] > coord1[1]
diagonal << row[coord1[1] + i - coord1[0]] unless coord1[1] + i < coord1[0]
else
diagonal << row[i]
end
i += 1
end
diagonal
elsif coord1[0] + coord1[1] == coord2[0] + coord2[1]
case coord1[0] + coord1[1]
when 5
[@board[3][2],@board[2][3]]
when 4
[@board[3][1],@board[2][2],@board[1][3]]
when 3
[@board[3][0],@board[2][1],@board[1][2],@board[0][3]]
when 2
[@board[2][0],@board[1][1],@board[0][2]]
else
[@board[1][0],@board[0][1]]
end
else
puts "Those coordinates aren't part of 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:
4.times do |i|
print boggle_board.get_row(i).join + " " + boggle_board.get_col(i).join + " " #=> brae biet iodt roca eclr adlk take etre
end
p boggle_board.get_row(3) #=> ["t", "a", "k", "e"]
p boggle_board.get_col(0) #=> ["b", "i", "e", "t"]
p boggle_board.get_col(6) #=> [nil, nil, nil, nil]
p boggle_board.create_word([1,2], [1,1], [2,1], [3,2]) #=> "dock"
# create driver test code to retrieve a value at a coordinate here:
p boggle_board.board[0][3] == "e"
p boggle_board.board[3][2] == "k"
p boggle_board.board[0][5] == nil
p boggle_board.board[1][2] == "d"
#BONUS PROBLEM: method is included above.
p boggle_board.get_diagonal([0,2],[1,3]) #=> ["a", "t"]
p boggle_board.get_diagonal([0,1],[1,2]) #=> ["r", "d", "r"]
p boggle_board.get_diagonal([2,2],[3,3]) #=> ["b", "o", "l", "e"]
p boggle_board.get_diagonal([2,1],[3,2]) #=> ["i", "c", "k"]
p boggle_board.get_diagonal([2,0],[3,1]) #=> ["e", "a"]
p boggle_board.get_diagonal([1,0],[0,1]) #=> ["i", "r"]
p boggle_board.get_diagonal([1,1],[0,2]) #=> ["e","o","a"]
p boggle_board.get_diagonal([3,1],[2,2]) #=> ["a", "l", "t"]
p boggle_board.get_diagonal([3,0],[1,2]) #=> ["t", "c", "d", "e"]
p boggle_board.get_diagonal([3,1],[2,2]) #=> ["a", "l", "t"]
p boggle_board.get_diagonal([3,2],[2,3]) #=> ["k", "r"]
p boggle_board.get_diagonal([3,3],[2,1]) #=> "Those coordinates aren't part of a diagonal."
#REFLECTION:
# This was the hardest challenge yet, for me. Well, to be clear, the bonus problem was the hardest challenge yet. The base
# challenge was actually super easy, but figuring out how to get the diagonal array was super difficult for some reason. I
# think it was the fact that both types of diagonals(incline and decline) needed their own type of solution. I can't help but
# feel like my solution for incline diagonals was kind of a cop-out, but after a good two hours thinking and trying out ways
# to generate diagonals given any size boggle board and any two coordinates of the diagonal desired, I settled for this.
# The transition from procedural to object-oriented programming was smooth and painless. The difficulty of the bonus problem
# wasn't increased or decreased by the switch, and everything else was improved, so I'm quite happy about it. I can really see
# now how when you're dealing with manipulating large sets of data, being able to treat the set as an object is quite handy. It's
# great to see how I can extend what I've learned about object creation to greater scopes than just inside methods.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment