Forked from dbc-challenges/0.2.1-boggle_class_from_methods.rb
Last active
December 29, 2015 11:59
-
-
Save papapoison/7667893 to your computer and use it in GitHub Desktop.
phase 0 unit 2 week 1
boggle class challenge
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#Boggle Board class | |
class BoggleBoard | |
def initialize(board) | |
@board = board | |
end | |
def access(point1, point2) | |
puts @board[point1][point2] | |
end | |
def create_word(*coords) | |
puts coords.map { |coord| @board[coord.first][coord.last]}.join("") | |
end | |
def get_row(row) | |
puts @board[row].inspect | |
end | |
def get_col(col) | |
puts @board.transpose[col].inspect | |
end | |
def get_diag(row, col) | |
diag_arr = [] | |
diag_arr << @board[row - 1][col - 1] unless @board[row][col] == nil | |
diag_arr << @board[row + 1][col + 1] unless @board[row][col] == nil | |
diag_arr << @board[row + 1][col - 1] unless @board[row][col] == nil | |
diag_arr << @board[row - 1][col + 1] unless @board[row][col] == nil | |
puts diag_arr.inspect | |
end | |
end | |
##########Calls############## | |
dice_grid = [["b", "r", "a", "e"], | |
["i", "o", "d", "t"], | |
["e", "c", "l", "r"], | |
["t", "a", "k", "e"]] | |
boggle_board = BoggleBoard.new(dice_grid) | |
###############TESTS################ | |
boggle_board.create_word([2, 1], [1, 1], [1, 2], [0, 3]) | |
boggle_board.create_word([0, 1], [0, 2], [1, 2]) | |
boggle_board.create_word([2, 1], [3, 1], [3, 2], [3, 3]) | |
boggle_board.create_word([3, 0], [3, 1], [2, 1], [3, 2], [2, 2], [3, 3]) | |
boggle_board.get_row(1) | |
boggle_board.get_row(0) | |
boggle_board.get_col(3) | |
boggle_board.get_col(2) | |
boggle_board.get_diag(2, 2) | |
boggle_board.get_diag(2, 1) | |
###############DRIVER################## | |
boggle_board.access(2, 1) | |
boggle_board.access(3, 1) | |
boggle_board.access(3, 2) | |
##############REVIEW################### | |
#Coding using OOP methodology makes things a great deal easier. Conceptually, it just makes more sense. | |
#Thing 1 can do a, b, and c. d, e, and f are private, and cannot be acceced from without | |
#It also makes the code more versatile, allowing me to pass in any values at the initialization of my code | |
# that will work regardless. It's good stuff. In the long run it makes your code more dry because you can reuse | |
# methods without retyping the shit each time. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment