Forked from dbc-challenges/0.2.1-boggle_class_from_methods.rb
Last active
December 29, 2015 12:29
-
-
Save hwhelchel/7670887 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
# Overview | |
# Create a class BoggleBoard that includes the functionality of your methods from the previous challenge. | |
# To do this, take a look at the methods you've created. How can they be integrated into your BoggleBoard class? What needs to change? | |
# 1) Instantiate a new board object | |
# Transform your driver code so that it creates a new board object. You'll need to pass the original 2D array as an argument | |
# (let's call that dice_grid because boggle_board is going to be an object now.) | |
# | |
# 2) Implement your methods | |
# One method at a time, create a test to access your new boggle_board object. The first method should be #create_word. | |
# (Don't get thrown off with the #method_name syntax, using # before a method name is a ruby convention.) Write out a test with | |
# it's expectation in a comment, and then create the method in the BoggleBoard class. Try these coordinates: (1,2), (1,1), (2,1), (3,2). | |
# Then, write methods for #get_row and #get_col. Can you interact with the boggle_board object and get the values you expect? | |
# Now print out all the rows and columns of the board as strings. You should end up with 8 four letter words. | |
# Are there any real words shown? Add your total output as a comment in your gist. | |
# 3) Access a coordinate | |
# Now write some driver code to access an individual coordinate in your boggle_board object. Make this as simple as possible. | |
# Can you access the "k" character at row 3 column 2? | |
# 4) Bonus: Create a #get_diagonal method | |
# Just like the #get_col or #get_row method, the #get_diagonal method should return an array of values, but it will need 2 coordinates | |
# entered to define the diagonal. Error checking to make sure the coordinates are actually a diagonal would probably be a good idea. | |
# 5) Review and Reflect | |
# You just made a transition from procedural programming to object-oriented programming! How is the implementation different? | |
# What are the benefits to using the Object Oriented approach (even if it is a bit more code?) | |
# 6) Submit your solution | |
# Take a moment to look at other solutions after you submit. Were there any interesting implementations? | |
class BoggleBoard | |
attr_reader :board | |
def initialize(board) | |
@board = board | |
end | |
def get_row(row) | |
print @board[row] | |
end | |
def create_word(*coords) | |
coords.map { |coord| @board[coord.first][coord.last]}.join("") | |
end | |
def get_col(col) | |
print @board.map {|row| row[col]} | |
end | |
# Get diagonal takes two integers. | |
def get_diagonal(initial_row, initial_col) | |
# # mod 4 | |
# # add one | |
# # 4 times | |
# # check initial row > or < initial col. Use to chop off unneeded strings. | |
# diagonal = [] | |
# diagonal << [initial_row,initial_col] | |
# 3.times do | |
# diagonal << [(initial_row += 1) % 4,(initial_col += 1) % 4] | |
# end | |
# if initial_row == initial_col | |
# diagonal | |
# # convert to letters | |
# elsif initial_row < initial_col | |
# diagonal.map do | |
# # Select | |
(0..3).map {|index| @board[index][index]} | |
end | |
# (0,0)(1,1)(2,2)(3,3) | |
# (0,1)(1,2)(2,3) | |
# (0,2)(1,3) | |
# (2,0)(3,1) | |
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) | |
# How does the boggle_board object hold the dice_grid? As an initialized instance variable. | |
# Step 1 | |
boggle_board.get_row(1) # returns ["i", "o", "d", "t"] | |
boggle_board.get_col(0) # returns ["b","i","e","t"] | |
puts boggle_board.create_word([3,0],[3,1],[3,2],[3,3]) # Returns "take" | |
puts boggle_board.create_word([1,2], [1,1], [2,1], [3,2]) # Returns "dock" | |
# Step 2 | |
boggle_board.get_row(0) | |
boggle_board.get_row(1) | |
boggle_board.get_row(2) | |
boggle_board.get_row(3) | |
boggle_board.get_col(0) | |
boggle_board.get_col(1) | |
boggle_board.get_col(2) | |
boggle_board.get_col(3) | |
# Step 3 | |
puts boggle_board.board[3][2] #returns k | |
# Step 4 | |
# print boggle_board.get_diagonal(1,1) # Could not get to work. | |
# Step 5 | |
# object oriented programming requires the creation of objects to run the methods on. It links the object with the | |
# procedure.In procedural programming the emphasis is more on the procedure itself and its input and output. There | |
# is no need for objects. OOP has more code but maps more easily to many types of human conception of systems and | |
# processes in the world. | |
# From Wikipedia: PP tries to break down a programming task into a collection of | |
# variables, data structures, and subroutines whereas OOP tries to break down programming | |
# tasks into objects that expose behavior (methods) and data (members or attributes) using | |
# interfaces. PP uses procedures to operate on data structures, OOP bundles methods and | |
# data structures together so an "object" operates on its "own" data structure. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment