Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save zlc2013/9110470 to your computer and use it in GitHub Desktop.
Save zlc2013/9110470 to your computer and use it in GitHub Desktop.
phase 0 unit 2 week 1boggle class challenge
class BoggleBoard
attr_reader :board
def initialize(board)
@board = board
end
def create_word(*coords)
coords.map{|coord| self.board[coord.first][coord.last]}.join("")
end
def get_row(row)
return self.board[row]
end
def get_column(col)
column = []
self.board.each_index{|row| column.push(self.board[row][col])}
return column
end
def get_diagonal(coord1,coord2)
start_row = coord1[0]
start_col = coord1[1]
finish_row = coord2[0]
finish_col = coord2[1]
diagonal = start_row != finish_row && start_col != finish_col && (finish_col - start_col).abs == (finish_row - start_row).abs
unless diagonal then raise ArgumentError.new('Womp womp...') end
result = []
current_row = start_row
current_col = start_col
result.push(self.board[current_row][current_col])
while current_row != finish_row && current_col != finish_col do
current_row < finish_row ? current_row += 1 : current_row -= 1
current_col < finish_col ? current_col += 1 : current_col -= 1
result.push(self.board[current_row][current_col])
end
return result.join
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) #instantiates board object
# implement tests for each of the methods here:
puts boggle_board.create_word([2,1], [1,1], [0,1], [1,2]) #=> 'cord'
puts boggle_board.create_word([1,2], [1,1], [2,1], [3,2]) #=> 'dock'
puts boggle_board.get_row(0).join #=> "brae"
puts boggle_board.get_row(1).join #=> "iodt"
puts boggle_board.get_row(2).join #=> "eclr"
puts boggle_board.get_row(3).join #=> "take"
puts boggle_board.get_column(0).join #=> "biet"
puts boggle_board.get_column(1).join #=> "roca"
puts boggle_board.get_column(2).join #=> "adlk"
puts boggle_board.get_column(3).join #=> "etre"
puts boggle_board.get_diagonal([0,0],[3,3]).join #=> "bole"
puts boggle_board.get_diagonal([3,0],[0,3]).join #=> "tcde"
#take seems to be the only real word printed - all outputs were as expected
# create driver test code to retrieve a value at a coordinate here:
boggle_board.board[3][2] #=> "k" .... works as expected
# ... original and refactored implementations of get_diag
=begin
def get_diagonal(coord1,coord2)
start_row = coord1[0]
start_col = coord1[1]
finish_row = coord2[0]
finish_col = coord2[1]
diagonal = (finish_col - start_col).abs == (finish_row - start_row).abs
unless diagonal then raise ArgumentError.new('Bullshit!') end
result = []
current_row = start_row
current_col = start_col
result.push(self.board[current_row][current_col]) #pushes the first letter to the result array
while current_row != finish_row && current_col != finish_col do #iterates up to and including the last letter, pushing to result array
if current_row < finish_row
current_row += 1
elsif current_row > finish_row
current_row -= 1
end
if current_col < finish_col
current_col += 1
elsif current_col > finish_col
current_col -= 1
end
result.push(self.board[current_row][current_col])
end
return result.join
end
#Refactored...
def get_diagonal(coord1,coord2)
start_row = coord1[0]
start_col = coord1[1]
finish_row = coord2[0]
finish_col = coord2[1]
diagonal = start_row != finish_row && start_col != finish_col && (finish_col - start_col).abs == (finish_row - start_row).abs
unless diagonal then raise ArgumentError.new('Womp womp...') end
result = []
current_row = start_row
current_col = start_col
result.push(self.board[current_row][current_col])
while current_row != finish_row && current_col != finish_col do
current_row < finish_row ? current_row += 1 : current_row -= 1
current_col < finish_col ? current_col += 1 : current_col -= 1
result.push(self.board[current_row][current_col])
end
return result.join
end
=end
#Reflection
#Two major advantages strike me about OOP.
#a) It provides a way to group methods with the data they are intended to manipulate and gives the code structure.
#b) It provides a useful conceptual framework for modeling real world objects and how they interact (or how you would like them to...)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment