Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save gpuglia/9214272 to your computer and use it in GitHub Desktop.
Save gpuglia/9214272 to your computer and use it in GitHub Desktop.
phase 0 unit 2 week 1boggle class challenge
class Boggle
attr_reader :board
def initialize(board)
@board = board
end
def create_word(*coords)
coords.map { |coord| @board[coord.first][coord.last]}.join("")
end
def get_row(row)
@board[row]
end
def get_col(col)
@board.transpose[col]
end
def get_diagonal(x, y)
raise ArgumentError.new('Invalid diagonal') unless x == y or x + y == 3
diagonal = []
if x == y
4.times { |i| diagonal << @board[i][i] }
else
4.times { |i| diagonal << @board[i][3 - i] }
end
return diagonal.inspect
end
end
game = Boggle.new( [["b", "r", "a", "e"],
["i", "o", "d", "t"],
["e", "c", "l", "r"],
["t", "a", "k", "e"]] )
puts game.create_word([2,1], [1,1], [1,2], [0,3]) #=> returns "code"
puts game.create_word([0,1], [0,2], [1,2]) #=> returns "rad"
puts game.create_word([3,0], [3,1], [2,1], [3,2], [2,2], [3,3]) #=> returns "tackle"
puts game.create_word([1,2], [0,2], [1,3], [0,3] ) #=> returns "date"
puts game.get_row(1) #=> ["i", "o", "d", "t"]
puts game.get_col(1) #=> ["r", "o", "c", "a"]
puts game.get_diagonal(0,0) #=> ["b", "o", "l", "e"]
puts game.get_diagonal(0,3) #=> ["e", "d", "c", "t"]
puts game.get_diagonal(0,1) #=> ArgumentError: "Invalid diagonal"
#----REFLECT----#
# In this challenge we transitioned from procedural programming to Object Oriented Programming. The advantage of OOP is that
# this code provides a blueprint from which many objects can be made, this means that out of the same Boggle class I can
# create multiple different Boggle Boards, on which I can operate whithout affecting the others. In PP the program is only
# usable in the environment in which it is defined. While it takes more time to get used to OOP, its benefits outweigh this
# period of adaptation.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment