Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save trekkie4life/7670647 to your computer and use it in GitHub Desktop.
Save trekkie4life/7670647 to your computer and use it in GitHub Desktop.
phase 0 unit 2 week 1 boggle class challenge
#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)
#4.1 - Rewrite definition of "first"
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(:+) }
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] ]
# diagonal_pts[0] = coord1 = [0,0]
# diagonal_pts[0][0] = 0
# Tip : #between?(val1,val2)
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],[3,3]) == ["b", "o", "l", "e"].to_s ? true : "false, the returned value was #{boggle_board.get_diagonal([0,0],[1,1])}")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment