Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dstavis/8290087 to your computer and use it in GitHub Desktop.
Save dstavis/8290087 to your computer and use it in GitHub Desktop.
phase 0 unit 2 week 1 boggle class challenge
class BoggleBoard
def initialize(board)
@board = board
end
# method that maps through each
def create_word(*coords)
coords.map { |coord| @board[coord.first][coord.last] }.join("") #@board is a 2D array
end
#[[1,1], [0,1]]
#puts create_word(boggle_board, [2,1], [1,1], [1,2], [0,3]) #=> returns "code"
#[2,1] => "c" "o" "d" "e" ==> "code"
def get_row(row)
@board[row]
end
def get_col(col)
@board.transpose[col]
end
#this implementation of get_diagonal only works if the letter on which the diagonal ends is unique in the diagonal,
#and only if the start_char is at lower indexes than the end_char. Paolo created a universally functional solution in his attempt.
def get_diagonal(start_char, end_char) #start_char and end_char are arrays. Imagine: start_char = [0, 0] end_char = [2, 2]
diagonal = []
diagonal << @board[start_char.first][start_char.last] #==> [1][1] instead of [1,1]
until diagonal.last == @board[end_char.first][end_char.last] do
start_char.each do |index|
index += 1
end
diagonal << @board[start_char.first][start_char.last]
end
return diagonal
end
#Paolo Version #get_diagonal(0,0) => "b" "o" "l" "e"
#Charles Version #get_diagonal(2,1) => "t" "k" "d" "i"
#Paolo Version #get_diagonal(2,1) => "c" "k"
#David Version #get_diagonal([0,1],[2,3]) => "r" "d" "r"
#Psuedocode
#takes two coordinates to define the diagonal
#error check to see if the coordinates actually make a diagonal
#find and return the elements of the diagonal (between the coordinates)
#start at the element determined by the first argument. put it in an array.
#add one to row and one to column and also push the element at that position- continue until you reach the second argument.
#return that array. Done!
#+1 row and +1 column == next element in diagonal
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)
# implement tests for each of the methods here:
boggle_board.get_diagonal([0,1],[2,3]) #=> "r" "d" "r"
# create driver test code to retrieve a value at a coordinate here:
=begin
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?
Remember to Enter the collaboration hangout. Please turn off your video and sound. Use the chat function.
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?
=end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment