Forked from dbc-challenges/0.2.1-boggle_class_from_methods.rb
Last active
August 29, 2015 13:56
-
-
Save betsyrhodes/9107910 to your computer and use it in GitHub Desktop.
phase 0 unit 2 week 1boggle class challenge
This file contains hidden or 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
# 1) Instantiate a new board object. How does the boggle_board object hold the dice_grid? | |
#Psudocode | |
# Input: BoggleBoard class takes in a 2-dimensional array of letters as a parameter, | |
# #create_word takes in a series of coordinates as a parameter, #get_row & #get_col take | |
# in a row/column number respectively as a parameter. | |
# Output: #initialize returns the dice_grid, #create_word returns a string made up of the | |
# letters at the respective coordinates, #get_row returns a string made up of the letters in | |
# the row that was passed in as a parameter, #get_col returns a string made up of the letters | |
# in the column that was passed in as a parameter. | |
# Most of the methods here are from 'A Nested Array to Model a Boggle Board', so I'm not | |
# going to step through the problem solving again here. | |
# In the instance being created for test purposes dice_grid is the array being passed in as a | |
# parameter and boggle_board is the variable that this instance of BoggleBoard is assigned to. | |
class BoggleBoard | |
def initialize(dice_grid) # dice_grid should be a 2D array of letters | |
@boggle_board = dice_grid | |
end | |
def create_word(*word_coords) # word_coords should be a list of arrays containing the indexes - row then column - of the desired letters | |
word_coords.map { |letter_coords| @boggle_board[letter_coords.first][letter_coords.last]}.join("") | |
end | |
def get_row(row) # row should be the index number of the row that is to be returned | |
if row < @boggle_board.length | |
return @boggle_board[row].join('') | |
else | |
return "No such row exists." | |
end | |
end | |
def get_col(col) # col should be the index number of the column that is to be returned | |
if col < @boggle_board.transpose.length | |
return @boggle_board.transpose[col].join('') | |
else | |
return "No such column exists." | |
end | |
end | |
# this method will return only the diagonals that span from corner to corner in a square dice_grid if the | |
# coordinates passed in are on one of those 2 diagonals. | |
def get_diagonal(coordinates) | |
diagonal = [] | |
if coordinates.first < @boggle_board.length && coordinates.last < @boggle_board.length | |
if coordinates.first == coordinates.last | |
@boggle_board.each_index { |index_num| diagonal << @boggle_board[index_num.to_i][index_num.to_i] } | |
elsif coordinates.first + coordinates.last == @boggle_board.length - 1 | |
@boggle_board.each_index { |index_num| diagonal << @boggle_board[index_num.to_i][(@boggle_board.length - 1) - index_num.to_i] } | |
else | |
return "This coordinate pair is not on a diagonal." | |
end | |
else | |
return "This coordinate pair is not in the Boggle Board." | |
end | |
return diagonal | |
end | |
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) | |
# 2) Implement your methods - #create_word, #get_row, #get_col. | |
# implement tests for each of the methods here: | |
puts boggle_board.create_word([1,2], [1,1], [2,1], [3,2]) == 'dock' #=> should be true | |
puts boggle_board.create_word([0,0], [1,1], [0,2], [0,1], [1,2]) == 'board' #=> should be true | |
puts boggle_board.create_word([3,0], [3,1], [2,2], [3,2]) == 'talk' #=> should be true | |
puts boggle_board.create_word([3,0], [3,1], [2,1], [3,2], [2,2], [3,3]) == 'tackle' #=> should be true | |
puts boggle_board.get_row(0) == "brae" #=> should be true | |
puts boggle_board.get_row(1) == "iodt" #=> should be true | |
puts boggle_board.get_row(2) == "eclr" #=> should be true | |
puts boggle_board.get_row(3) == "take" #=> should be true, this is also a real word | |
puts boggle_board.get_row(0) != "biet" #=> should be true | |
puts boggle_board.get_row(4) == "No such row exists." #=> should be true | |
puts boggle_board.get_row(5) == "No such row exists." #=> should be true | |
puts boggle_board.get_col(0) == "biet" #=> should be true | |
puts boggle_board.get_col(1) == "roca" #=> should be true | |
puts boggle_board.get_col(2) == "adlk" #=> should be true | |
puts boggle_board.get_col(3) == "etre" #=> should be true | |
puts boggle_board.get_col(0) != "brae" #=> should be true | |
puts boggle_board.get_col(4) == "No such column exists." #=> should be true | |
puts boggle_board.get_col(15) == "No such column exists." #=> should be true | |
# 3) Access a coordinate. | |
# create driver test code to retrieve a value at a coordinate here: | |
puts boggle_board.create_word([3,2]) == 'k' #=> should be true (using boggle_board object) | |
puts dice_grid[3][2] == 'k' #=> should be true (not using boggle_board object) | |
puts boggle_board.create_word([0,2]) != 'k' #=> should be true | |
# 4) Bonus: Create a #get_diagonal method | |
puts boggle_board.get_diagonal([1,1]) == ['b', 'o', 'l', 'e'] #=> should be true | |
puts boggle_board.get_diagonal([0,3]) == ['e', 'd', 'c', 't'] #=> should be true | |
puts boggle_board.get_diagonal([0,1]) == "This coordinate pair is not on a diagonal." #=> should be true | |
puts boggle_board.get_diagonal([3,3]) == ['b', 'o', 'l', 'e'] #=> should be true | |
puts boggle_board.get_diagonal([1,2]) == ['e', 'd', 'c', 't'] #=> should be true | |
puts boggle_board.get_diagonal([4,1]) == "This coordinate pair is not in the Boggle Board." #=> should be true | |
# currently this method returns the letters in an array, to return as a string could add #join('') to the | |
# end of the code on lines 51 and 53. | |
# 5) Review & 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?) | |
# So far it doesn't feel much different. From day one all I've been hearing is that everything in | |
# Ruby is an object. In the early exercises we were writing methods that worked to solve a problem | |
# on a given object (an array, a string, etc). Each exercise basically had a single problem to | |
# solve. Now that we are writing classes we are still writing methods that serve a particular | |
# purpose, but now we are writing multiple methods to solve various problems. The key difference | |
# is that all the methods relate to a single object that we have created and defined. | |
# The benefits seem to be a more focused set of problem solving tools geared at addressing | |
# the nuances of a particular object. This is all very new to me and it feels like we are now | |
# starting to use the builing blocks we have just learned about. At this point I'm seeing more | |
# similarities than differences. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment