Forked from dbc-challenges/0.2.1-boggle_class_from_methods.rb
Last active
August 29, 2015 13:55
-
-
Save MLBerns/8726708 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
class BoggleBoard | |
def initialize(board) | |
@current_board = board | |
end | |
def create_word(*coords) | |
coords.map { |coord| @current_board[coord.first][coord.last]}.join("") | |
end | |
def get_row(row) | |
print @current_board[row].join("") | |
puts | |
end | |
def get_cols(cols) | |
print @current_board.map{|i| i[cols]}.join("") | |
puts | |
end | |
def get_diagonal | |
print @current_board.map.with_index{|i, diag| @current_board[diag][diag]} | |
print @current_board.map.with_index(){|i, diag| @current_board[3-diag][diag]} | |
puts | |
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) | |
# implement tests for each of the methods here: | |
puts boggle_board.create_word([2,1], [1,1], [1,2], [0,3]) #=> returns "code" | |
puts boggle_board.create_word([0,1], [0,2], [1,2]) #=> returns "rad" | |
puts boggle_board.create_word([1,2], [1,1], [2,1], [3,2]) #=> returns "dock" | |
puts boggle_board.create_word([3,0], [3,1], [2,1], [3,2], [2,2], [3,3], [2,3]) #=> returns "tackler" | |
puts boggle_board.create_word([2,1], [1,1], [0,2], [1,3], [0,3], [1,2]) #=> returns "coated" | |
puts | |
4.times do |x| #=> returns brae, iodt, eclr, take | |
boggle_board.get_row(x) | |
end | |
puts | |
4.times do |x| #=> returns biet, roca, adlk, etre | |
boggle_board.get_cols(x) | |
end | |
boggle_board.get_diagonal | |
# create driver test code to retrieve a value at a coordinate here: | |
puts true if boggle_board.create_word([2,1]) == "c" | |
puts true if boggle_board.create_word([0,2]) == "a" | |
puts true if boggle_board.create_word([0,3]) == "e" | |
#Reflection | |
#Even though it's more code, having the board as a class makes the code easier to read and understand, in my opinion. It's | |
#more organized and has a better flow. It also made it easier to manipulate data, and there's fewer instances of repetition | |
#from my procedural code, since before I had to pass the board into each method, but now it's already availavle to each | |
#method that's called. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment