#TODO #1 - Instanciate the BoggleBoard object Check #2 - Implement the methods we already had Check #3 - Create driver code to access a coordinate Check #4 - Create a #get_diagonal (Bonus) Check class BoggleBoard attr_reader :board def initialize(board) @board = board end def create_word(*coords) # Removed the board argument, use the instance variable instead coords.map { |coord| @board[coord.first][coord.last] }.join("") end def get_row(row) # We just return boggle_board[row] @board[row].to_s end def get_col(column) # First we get the length of the array. It will be the maximum of our first coordinate. # Then we take iterate from 0 to that and add to our result the value of [current_rank_of_the_iteration,argument] # Then we return this new array. @board.map {|row| row[column]}.to_s end def get_diagonal(coord1, coord2) # First, we check that coord 1 and 2 are not on the same line or column, and separated by a multiple of one row one column # Then, we find the first value of the diagonal, the step it takes from each value to the next, and build the diagonal with that. # And we return it. diagonal_pts = [coord1, coord2].sort # sorting coordinates ensures a positive step/slope between 2 our coordinates # array.inject(:+) => returns the sum of all the values of the array. sum = diagonal_pts.map { |coord| coord = coord.inject(:+) } # Check if the difference between coordinates is the same for both x and y, or if it is the opposite (- and +) raise ArgumentError.new("Please use coordinates that are in a diagonal") if (sum[-1] - sum[0]) % 2 != 0 if sum[-1] - sum[0] == 0 step = [1, -1] else step = [1, 1] end diagonal_pts.pop while(true) # creates an infinite loop new_pt = [ diagonal_pts[0][0] - step[0], diagonal_pts[0][-1] - step[-1] ] if new_pt[0].between?(0, @board.length - 1) && new_pt[1].between?(0, @board.length - 1) diagonal_pts = diagonal_pts.push(new_pt).sort # array << value => #push else break end end while(true) # creates an infinite loop new_pt = [ diagonal_pts[-1][0] + step[0], diagonal_pts[-1][-1] + step[-1] ] if new_pt[0].between?(0, @board.length - 1) && new_pt[1].between?(0, @board.length - 1) diagonal_pts = diagonal_pts.push(new_pt).sort # array << value => #push else break end end diagonal_pts.map { |coordinate| @board[coordinate[0]][coordinate[1]].to_s }.to_s 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 "##TESTS##" puts "Testing coordinates" puts (boggle_board.board[0][0] == "b" ? true : "false, the returned value was #{boggle_board.board[0][0]}") puts (boggle_board.board[2][1] == "c" ? true : "false, the returned value was #{boggle_board.board[2][1]}") puts (boggle_board.board[1][2] == "d" ? true : "false, the returned value was #{boggle_board.board[2][1]}") puts "Testing diagonal" puts (boggle_board.get_diagonal([0,0],[1,1]) == ["b","o","l","e"].to_s ? true : "false, the returned value was #{boggle_board.get_diagonal([0,0],[1,1])}") ## OUTPUT =begin ##TESTS## Testing coordinates true true true Testing diagonal false, the returned value was ["b", "o", "l", "e"] =end #REFLEXION =begin The get_diagonal may not be the most straightforward thing we did. I am pretty sure there are better and/or simpler ways to do it, I just don't know about them yet. I let the tips in the revision before last, in comments... everywhere. While(true) is not something that sounds very... practical, but I guess I will learn how to simplify that while learning about other ruby methods I don't know. =end