Forked from dbc-challenges/0.2.1-boggle_class_from_methods.rb
Last active
December 29, 2015 11:59
-
-
Save papapoison/7667144 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#################### | |
#Doing this OOP style is way easier. I don't have to putz about with hard coding the board into everything | |
#instead of simply making it an instance variable. It's also more versatile because I can change board | |
#values to whatever I want and just pass that bizz into my code and I'll know it will work. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment