Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save whypry/9183094 to your computer and use it in GitHub Desktop.
Save whypry/9183094 to your computer and use it in GitHub Desktop.
phase 0 unit 2 week 1boggle class challenge
class BoggleBoard
#1 INSTANTIATE A NEW BOARD OBJECT
def initialize board
@board = board
end
#2 IMPLEMENT YOUR METHODS
def create_word *coords
coords.map { |coord| @board[coord.first][coord.last]}.join("")
end
def get_row row
print @board[row]
end
def get_column col
@board.flatten.each_with_index {|obj, ind| print obj if ind % 4 == col}
end
def get_diagonal c1, c2
if (c1.first - c2.first).abs != (c1.last - c2.last).abs
raise ArgumentError, 'Coordinates are not diagonals'
elsif c1.first < c2.first
#write some sort of loop that, based on the number of coordinates in between
#c2 and c1, calls the appropriate amount of coordinates in between
puts c1 + @board[c1.first+1, c1.last+1] + c2
else
#same as above but for the opposite direction
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)
=begin
# implement tests for each of the methods here:
methodman1 = boggle_board.create_word([1,2],[1,1],[2,1],[3,2]) #=> dock
puts methodman1
#get_row test
gr0 = boggle_board.get_row(0).to_s
gr1 = boggle_board.get_row(1).to_s
gr2 = boggle_board.get_row(2).to_s
gr3 = boggle_board.get_row(3).to_s
#get_column test
gc0 = boggle_board.get_column(0).to_s
gc1 = boggle_board.get_column(1).to_s
gc2 = boggle_board.get_column(2).to_s
gc3 = boggle_board.get_column(3).to_s
#gc = [gc0, gc1, gc2, gc3]
#total output => braeiodteclrtakebietrocaadlketre
#Yes, take is a real word
#3 ACCESS A COORDINATE
#create driver test code to retrieve a value at a coordinate here:
#character "k"
a = "\n" + boggle_board.create_word([3,2])
puts a
#4 BONUS
=end
puts boggle_board.get_diagonal([0,0],[2,2])
#5 REVIEW AND REFLECT
#I didn't quite realize that we made a transition. After pondering the reflection
#prompt I did notice how taking an object and defining many ways to alter and
#use the data. Looking up procedural programming, I can say that our OO code doesn't
#provide a set of instructions for what to do step by step but rather a way to
#manipulate the object without having to rely on a linear, procedural approach.
#OO gives greater flexibility and I can see its advantage in promoting a clearer
#understanding of code.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment