Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save cprater/7775220 to your computer and use it in GitHub Desktop.
Save cprater/7775220 to your computer and use it in GitHub Desktop.
phase 0 unit 2 week 1 boggle class challenge
class BoggleBoard
def initialize(board)
@board = board
end
def create_word(*coords)
coords.map { |coord| @board[coord.first][coord.last]}.join("")
end
def get_row(row)
puts @board[row].join
end
def get_col(col)
row = 0 #set row counter to 0
while row < @board.size #iterate through the outer arrays
print @board[row][col] #adding the columns to the new array
row += 1 #update counter
end
puts "\n" #create a newline for style
end
def get_diagonal(coord1, coord2)
if (coord1.first-coord2.first).abs != (coord1.last - coord2.last).abs #test to see if they are diagonal using absolute value
raise ArgumentError.new("Not a diagonal")
else
diagonal = []
row = coord1.first
column = coord1.last
if coord1.first < coord2.first #if the first coords are the top
until row > coord2.first #until the current row has reached the bottom
diagonal << @board[row][column] #push current item to array
row += 1
column +=1 #update row/column counter
end
else #if the first coords are at the bottom
until row < coord2.first #until the current row has reached the top
diagonal << @board[row][column] #push the current item to array
row -= 1
column +=1 #update row/column counter
end
end
print 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) #create BoggleBoard object to work with
puts boggle_board.create_word([1,2], [1,1], [2,1], [3,2]) #should spell dock, it does
boggle_board.get_row(0) #=> ["b", "r", "a", "e"]
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(0) #=> biet
boggle_board.get_col(1) #=> roca
boggle_board.get_col(2) #=> adlk
boggle_board.get_col(3) #=> etre
puts boggle_board.create_word([3,2]) # Accessing coordinate k
puts boggle_board.get_diagonal([0,0], [3,3])
puts boggle_board.get_diagonal([3,0], [0,3])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment