Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save awillborn/7777151 to your computer and use it in GitHub Desktop.
Save awillborn/7777151 to your computer and use it in GitHub Desktop.
phase 0 unit 2 week 1 boggle class challenge
class BoggleBoard
attr_reader :board
def initialize(board)
@board = board
puts board
end
def create_word(*coords)
coords.map { |coord| @board[coord.first][coord.last]}.join("")
end
def get_row(row)
puts @board[row]
end
def get_col(col)
for num in (0..3) do puts @board[num][col] end
end
def get_diag(coords1, coords2)
#Either the difference of the elements of each set of coordinates should equal each other
#Or the sum of the elements of each set of coordinates should equal each other
word = []
c = 0
if coords1[0]-coords1[1] == coords2[0]-coords2[1] # e.g. [0,0], [2,2]
if coords1[0] > coords2[0]
coords1, coords2 = coords2, coords1
end
while coords1[0] + c <= coords2[0]
word << @board[coords1[0] + c][coords1[1] + c]
c += 1
end
elsif coords1[0] + coords1[1] == coords2[0] + coords2[1] #e.g. [0,3], [2,1]
if coords1[0] < coords2[0]
coords1, coords2 = coords2, coords1
end
while coords1[0] - c >= coords2[0]
word << @board[coords1[0] - c][coords1[1] + c]
c += 1
end
else
raise ArgumentError.new("Your coordinates do not make a diagonal")
end
return word
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)
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([3,0], [3,1], [2,2], [3,2]) #=> returns "talk"
puts boggle_board.create_word([1,3], [2,3], [3,3], [3,2]) #=> returns "trek"
boggle_board.get_row(1) #=> ["i", "o", "d", "t"]
boggle_board.get_row(2) #=> ["e", "c", "l", "r"]
boggle_board.get_row(3) #=> ["t", "a", "k", "e"]
boggle_board.get_col(1) #=> ["r", "o", "c", "a"]
boggle_board.get_col(2) #=> ["a", "d", "l", "k"]
boggle_board.get_col(3) #=> ["e", "t", "r", "e"]
p boggle_board.get_diag([0,0], [2,2])
p boggle_board.get_diag([0,3], [2,1])
p boggle_board.get_diag([2,1], [0,3])
p boggle_board.get_diag([0,0], [2,1])
# create driver test code to retrieve a value at a coordinate here:
p coordinate = boggle_board.board[3][2]
=begin
This was all reasonably simple except for the #get_diag method, which I paired with Phil on.
I wrote down coordinates that did create diagonals and found that for all of them, either the sum of each set of coordinates or the
difference of each set of coordinates were equal. The steps you have to take to create the word afterward is different
in each case.
Phil and I learned how to switch variables quickly, which is pretty cool: x, y = y, x for example. (see line 29 and line 37)
=end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment