Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save Spitigala/8771715 to your computer and use it in GitHub Desktop.
Save Spitigala/8771715 to your computer and use it in GitHub Desktop.
phase 0 unit 2 week 1boggle class challenge. SOLO. https://socrates.devbootcamp.com/challenges/440
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)
# Alternative Solution: [row].map {|row| board[row]}
@board[row].map # maps the specified row of the board
end
def get_col(col)
# Alternative Solution: board.transpose[col..col].transpose
@board.map {|x| x[col]}
end
def get_all_rows
@board.map {|row| row.join}
end
def get_all_cols
bl = @board[0].length
i = 0
while i < bl
puts @board.map {|x| x[i] }.join
i += 1 # Increment i
end
end
#def get_diagonal(c1, c2)
# @board[c1].map
#end
end
dice_grid = [["b", "r", "a", "e"],
["i", "o", "d", "t"],
["e", "c", "l", "r"],
["t", "a", "k", "e"]]
# implement tests for each of the methods here:
# create driver test code to retrieve a value at a coordinate here:
boggle_board = BoggleBoard.new(dice_grid)
puts boggle_board.create_word([2,1], [1,1], [1,2], [0,3]) #=> "code"
puts boggle_board.create_word([1,2], [1,1], [2,1], [3,2]) #=> "dock"
puts boggle_board.get_row(1) #=> ["i", "o", "d", "t"]
puts boggle_board.get_row(2) #=> ["e", "c", "l", "r"]
puts boggle_board.get_row(3) #=> ["t", "a", "k", "e"]
puts boggle_board.get_col(1) #=> ["r", "o", "c", "a"]
puts boggle_board.get_col(2) #=> ["a", "d", "l", "k"]
puts boggle_board.get_col(3) #=> ["e", "t", "r", "e"]
puts boggle_board.create_word([3,2]) #=> 'k'
puts boggle_board.get_all_rows #=> brae,iodt,eclr,take
puts boggle_board.get_all_cols #=> biet,roca,adlk,etre
#puts boggle_board.get_diagonal([])
#REFLECTION
=begin
I had accidentally done most of the work for this exercise while working on the
previous one, so it was a matter of expansing upon existing code.
I knew that the get_all_rows method needed a join method to create separate words,
but it took me a couple of tries to figure out exactly where to put it.
What I had most trouble with was getting all the cols to print.
I came up with a crude solution that may not the best, but it got the job done.
I've yet to implement the get_diagonal method.
=end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment