Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save bengolden/9122402 to your computer and use it in GitHub Desktop.
Save bengolden/9122402 to your computer and use it in GitHub Desktop.
phase 0 unit 2 week 1boggle class challenge
class Boggle_board
def initialize(board)
@board = board.clone
end
def create_word(*coords)
coords.map { |coord| @board[coord.first][coord.last]}.join("") # returns a word built from coordinates
end
def get_row(row)
@board[row] # returns a row from a boggle board
end
def get_col(col)
@board.collect { |row| row[col]} # returns a column from a boggle board
end
def get_coord(y, x)
@board[y][x]
end
def valid_coord(x, y)
x * y >= 0 && x < @board.length && y < @board.length
end
def get_diag(x, y, dir = 1)
output = []
if not valid_coord(x,y)
raise "Error - coordinate must be inside the board x = #{x} y = #{y}"
elsif y == 0 || (x == 0 && dir == 1) || (x == @board.length-1 && dir == -1)
while valid_coord(x,y)
output.push get_coord(y,x)
x += dir
y += 1
end
return output
else
get_diag(x-dir,y-1,dir)
end
end
end
dice_grid = [["b", "r", "a", "e"],
["i", "o", "d", "t"],
["e", "c", "l", "r"],
["t", "a", "k", "e"]]
bog_board = Boggle_board.new(dice_grid)
puts bog_board.get_row(1) == ["i", "o", "d", "t"]
puts bog_board.get_row(2) == ["e", "c", "l", "r"]
puts bog_board.get_row(3) == ["t", "a", "k", "e"]
puts bog_board.get_col(0) == ["b", "i", "e", "t"]
puts bog_board.get_col(1) == ["r", "o", "c", "a"]
puts bog_board.get_col(2) == ["a", "d", "l", "k"]
puts bog_board.create_word([2,1], [1,1], [1,2], [0,3]) == "code"
puts bog_board.create_word([0,1], [1,1], [2,1], [3,2] ) == "rock"
puts bog_board.create_word([1,0], [2,1], [2,0]) == "ice"
puts bog_board.create_word([1,2], [1,1], [2,1], [3,2]) == "dock"
puts bog_board.get_coord(3,2)
puts bog_board.get_diag(1,3)
puts bog_board.get_diag(1,3,-1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment