Forked from dbc-challenges/0.2.1-boggle_class_from_methods.rb
Created
February 20, 2014 22:16
-
-
Save RJNY/9124431 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
# Solo Challenge (Unit 2, Week 4) | |
# Take your time to see what concepts you feel comfortable and confident with. You are welcome to conduct research online, but make sure your code represents your own work. Please refrain from asking others for help. | |
# 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.) | |
class BoggleBoard | |
def initialize(board) | |
@board = board | |
end | |
def create_word(*coords) | |
coords.map {|coord| @board[coord.first][coord.last]}.join("") | |
end | |
def get_row(row) | |
output = @board[row].join("") | |
end | |
def get_col(col) | |
arr = [] | |
@board.each {|i| arr << i[col]} | |
arr.join("") | |
end | |
end | |
dice_grid = [["b", "r", "a", "e"], | |
["i", "o", "d", "t"], | |
["e", "c", "l", "r"], | |
["t", "a", "k", "e"]] | |
boggle = BoggleBoard.new(dice_grid) | |
# How does the boggle_board object hold the dice_grid? | |
#=================ANSWER=================# | |
# => BoggleBoard.new(dice_grid) is creating a new class object | |
# and boggle_board is holding that object as a variable. with this, we | |
# can now call this variable pointing to our new BoggleBoard object by | |
# simply typing 'boggle_board' | |
# 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). | |
#==========STUDENT DRIVER CODE===========# | |
puts "create_word excercise:" | |
puts boggle.create_word([1,2],[1,1],[2,1],[3,2]) #=> returns "dock" | |
puts boggle.create_word([2,1], [1,1], [1,2], [0,3]) #=> returns "code" | |
puts boggle.create_word([1,2],[0,2],[1,3],[3,3]) #=> 'date' | |
puts boggle.create_word([2,2],[0,2],[1,3],[3,3]) #=> 'late' | |
puts boggle.create_word([2,2],[1,1],[0,2],[1,2]) #=> 'load' | |
# 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. | |
puts "get_row excercise:" | |
puts boggle.get_row(0) #=> brae | |
puts boggle.get_row(1) #=> iotd | |
puts boggle.get_row(2) #=> eclr | |
puts boggle.get_row(3) #=> take (is a real word) | |
puts "get_col excercise:" | |
puts boggle.get_col(0) #=> biet | |
puts boggle.get_col(1) #=> roca | |
puts boggle.get_col(2) #=> adlk | |
puts boggle.get_col(3) #=> etre | |
#total output: | |
# brae | |
# iotd | |
# eclr | |
# take | |
# biet | |
# roca | |
# adlk | |
# etre | |
# 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?) Make sure to include your reflection in your gist. | |
# implement tests for each of the methods here: | |
# create driver test code to retrieve a value at a coordinate here: | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment