Forked from dbc-challenges/0.2.1-boggle_class_from_methods.rb
Last active
August 29, 2015 13:56
-
-
Save docsmage/9181631 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
# PSEUDOCODE | |
=begin | |
- Import the necessary methods and tests from the boggle board exercise. | |
- Structure them properly and add, change or remove elements as per the error messages. | |
- Build the class around the methods, adding appropriate context within initialize. | |
- Create new method calls for the class to test each function. | |
=end | |
class BoggleBoard # defines class BoggleBoard | |
attr_reader :board, :row, :col, :coords # allows us to read board, row, col and coords without having to create the variables | |
def initialize(board) # defines initialize | |
@board = board | |
get_row = [] # defines the result of get_row as an array | |
get_col = [] # defines the result of get_col as an array | |
create_word = [] # defines the result of create_word as an array | |
end # ends initialize | |
# methods from previous exercise | |
def get_row (row) # defines get_row | |
@board.fetch(row) {|row| puts "#{row}"} | |
end # ends get_row | |
def get_col (col) # defines get_col | |
@board.map { |row| row[col]} | |
end # ends get_col | |
def create_word (board, *coords) # takes an argument of board, and coords (can hold however many coords as the user inputs) | |
coords.map { |coord| @board[coord.first][coord.last]} # returns everything in the range between the first and last coordinate | |
end # ends create_word | |
end # ends class BoggleBoard | |
# new boggle_board multidimensional array known as dice_grid | |
dice_grid = [["b", "r", "a", "e"], | |
["i", "o", "d", "t"], | |
["e", "c", "l", "r"], | |
["t", "a", "k", "e"]] | |
# call to create new boggle_board as dice_grid | |
boggle_board = BoggleBoard.new(dice_grid) | |
# similar to the knife1 example in the drawer, boggle_board becomes an object that is | |
# pulling from the information in dice_grid. | |
# method calls to get_row | |
print boggle_board.get_row(1) # ==> ["i", "o", "d", "t"] | |
puts boggle_board.get_row(2) # ==> ["e", "c", "l", "r"] | |
puts boggle_board.get_row(3) # ==> ["t", "a", "k", "e"] | |
# method calls to get_col | |
print boggle_board.get_col(1) # ==> ["r", "o", "c", "a"] | |
print boggle_board.get_col(2) # ==> ["a", "d", "l", "k"] | |
print boggle_board.get_col(3) # ==> ["e", "t", "r", "e"] | |
# method calls to create_word | |
print boggle_board.create_word([], [0,1], [1,1], [0,2], [1,2]) # ==> ["r", "o", "a", "d"] | |
print boggle_board.create_word([], [3,0], [0,1], [3,3], [0,2], [1,2]) # ==> ["t", "r", "e", "a", "d"] | |
print boggle_board.create_word([], [2,1], [3,1], [3,0]) # ==> ["c", "a", "t"] | |
# I'm having an issue here where without the empty brackets at the beginning of each | |
# set of arguments [], the method won't print the first letter... so if you remove it from | |
# the first example, it will print "o", "a", "d" instead of "r","o","a","d". Very strange! | |
# REFLECTION | |
=begin | |
This is my umpteenth time submitting this, but this is the first time I got workable code! However, | |
I am still encountering the bug indicated above, and I will continue to work on that and perhaps re-submit. | |
This was my trickiest solo challenge yet (aside of regex)! The real trick was in defining the correct variables as | |
symbols in attr_reader (:row, :col, :board, :coord) and also defining the methods as arrays. I kept getting an | |
error that fetch and map were not valid methods because I still had the .join statements and did not think about | |
the fact that they were passing through as arrays. | |
Though writing methods by themselves felt easier, I think it is good to keep them together in a class for that | |
it is more readable when you call the method (and it organizes the methods by their class.) This is also good for | |
when you are contributing a piece of code to a bigger program, as it's more logical and readable for other programmers. | |
=end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Nadia showed me that by removing board as an argument in create_word, I no longer need the placeholders in my driver code. How simple!!!!