Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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)
column = []
i = 0
while i < @board.length
column << @board[i][col]
i +=1
end
return column
end
def get_diagonal(coord1, coord2)
if ((coord1[0]-coord2[0]).abs==@board.length-1)&&((coord1[1]-coord2[1]).abs==@board.length-1)
diag = []
i = 0
if coord1[0] == 0 && coord1[1] == 0
while i < @board.length
diag << @board[i][i]
i +=1
end
elsif coord1[0] == 0 && coord1[1] == @board.length - 1
while i < @board.length
diag << @board[i][@board.length - 1 - i]
i +=1
end
elsif coord1[0] == @board.length - 1 && coord1[1] == 0
while i < @board.length
diag << @board[@board.length - 1 - i][i]
i +=1
end
else coord1[0] == @board.length - 1 && coord1[1] == @board.length - 1
while i < @board.length
diag << @board[@board.length - 1 - i][@board.length - 1 - i]
i +=1
end
end
return diag
else
puts "You did not enter diagonal coordinates."
end
end
end
dice_grid = [["b", "r", "a", "e"],
["i", "o", "d", "t"],
["e", "c", "l", "r"],
["t", "a", "k", "e"]]
boggle_board1 = BoggleBoard.new(dice_grid)
# implement tests for each of the methods here:
puts boggle_board1.create_word([1,2], [1,1], [2,1], [3,2]) # => returns dock
puts boggle_board1.create_word([2,1], [1,1], [1,2], [0,3]) == "code"
puts boggle_board1.create_word([0,1], [0,2], [1,2]) == "rad"
puts boggle_board1.create_word([3,3], [2,2], [3,2]) == "elk"
puts boggle_board1.create_word([0,1], [1,1], [2,1], [3, 2]) == "rock"
puts boggle_board1.create_word([2,1], [3, 1], [3,0]) == "cat"
puts boggle_board1.get_row(0) == ["b", "r", "a", "e"]
puts boggle_board1.get_row(1) == ["i", "o", "d", "t"]
puts boggle_board1.get_row(2) == ["e", "c", "l", "r"]
puts boggle_board1.get_row(3) == ["t", "a", "k", "e"]
puts boggle_board1.get_col(0) == ["b", "i", "e", "t"]
puts boggle_board1.get_col(1) == ["r", "o", "c", "a"]
puts boggle_board1.get_col(2) == ["a", "d", "l", "k"]
puts boggle_board1.get_col(3) == ["e", "t", "r", "e"]
puts boggle_board1.get_row(0).join("") # => returns brae
puts boggle_board1.get_row(1).join("") # => returns iodt
puts boggle_board1.get_row(2).join("") # => returns eclr
puts boggle_board1.get_row(3).join("") # => returns take
puts boggle_board1.get_col(0).join("") # => returns biet
puts boggle_board1.get_col(1).join("") # => returns roca
puts boggle_board1.get_col(2).join("") # => returns adlk
puts boggle_board1.get_col(3).join("") # => returns etre
puts boggle_board1.get_diagonal([0,0], [3,3]) == ["b", "o", "l", "e"]
puts boggle_board1.get_diagonal([3,3], [0,0]) == ["e", "l", "o", "b"]
puts boggle_board1.get_diagonal([0,3], [3,0]) == ["e", "d", "c", "t"]
puts boggle_board1.get_diagonal([3,0], [0,3]) == ["t", "c", "d", "e"]
# create driver test code to retrieve a value at a coordinate here:
puts boggle_board1.board[3][2] # => returns "k"
puts boggle_board1.board[0][1] # => returns "r"
puts boggle_board1.board[1][3] # => returns "t"
puts boggle_board1.board[2][0] # => returns "e"
# REFLECTION
# this was a really good challenge. i got a lot out of moving from the last exercise to this exercise--
# that is, transforming the boggle board to object oriented. also--getting the diagonal to print
# out was a good task. i created if/else statements for the four starting points--not sure if there
# is a more efficient way to do that.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment