Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save nickmeehan/8769961 to your computer and use it in GitHub Desktop.
Save nickmeehan/8769961 to your computer and use it in GitHub Desktop.
phase 0 unit 2 week 1boggle class challenge
class BoggleBoard
def initialize(dice_grid)
@board = dice_grid
end
def create_word(*coords)
coords.map { |coord| @board[coord.first][coord.last] }.join("")
end
def get_row(row)
@board.at(row).join("")
end
def get_col(col)
@board.transpose[col].join("")
end
def get_diagonal(starting_coord, ending_coord)
diagonal_output = []
if starting_coord == [0,0] && ending_coord == [3,3]
start = 0
@board.each do |i|
diagonal_output << i.at(start)
start += 1
end
return diagonal_output
elsif starting_coord == [3,3] && ending_coord == [0,0]
get_diagonal([0,0], [3,3]).reverse
elsif starting_coord == [0,3] && ending_coord == [3,0]
start = -1
@board.each do |i|
diagonal_output << i.at(start)
start -= 1
end
return diagonal_output
elsif starting_coord == [3,0] && ending_coord == [0,3]
get_diagonal([0,3], [3,0]).reverse
else
"Please input two valid diagonals."
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)
# implement tests for each of the methods here:
p boggle_board.get_row(0) == "brae" #=> true
p boggle_board.get_row(1) == "iodt" #=> true
p boggle_board.get_row(2) == "eclr" #=> true
p boggle_board.get_row(3) == "take" #=> true
puts
p boggle_board.get_col(0) == "biet" #=> true
p boggle_board.get_col(1) == "roca" #=> true
p boggle_board.get_col(2) == "adlk" #=> true
p boggle_board.get_col(3) == "etre" #=> true
puts
p boggle_board.get_diagonal([0,0], [3,3]) == ["b", "o", "l", "e"] #=> true
p boggle_board.get_diagonal([3,3], [0,0]) == ["e", "l", "o", "b"] #=> true
p boggle_board.get_diagonal([0,3], [3,0]) == ["e", "d", "c", "t"] #=> true
p boggle_board.get_diagonal([3,0], [0,3]) == ["t", "c", "d", "e"] #=> true
# create driver test code to retrieve a value at a coordinate here:
p boggle_board.create_word([3,2]) == "k" #=> true
@nickmeehan
Copy link
Author

My output for getting columns and rows:

"brae"
"iodt"
"eclr"
"take"

"biet"
"roca"
"adlk"
"etre"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment