Forked from dbc-challenges/0.2.1-boggle_class_from_methods.rb
Last active
December 30, 2015 02:49
-
-
Save dustinfox-code/7765485 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
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_number) | |
@board[row_number] | |
end | |
def get_col(col_number) | |
holder_array = [] | |
@board.each_index {|index| holder_array << @board[index][col_number] } | |
holder_array | |
end | |
def get_diagonal(coord1, coord2) | |
if (coord2.last - coord1.last == coord2.first - coord1.first) | |
holder_array = [] | |
(coord2.last - coord1.last + 1).times do |count| | |
holder_array << @board[coord1.first + count][coord1.last + count] | |
end | |
holder_array | |
else | |
holder_array = ["This is not a diagonal"] | |
end | |
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.get_row(0).join("") #=> brae | |
puts boggle_board.get_row(1).join("") #=> iodt | |
puts boggle_board.get_row(2).join("") #=> eclr | |
puts boggle_board.get_row(3).join("") #=> take | |
puts boggle_board.get_col(0).join("") #=> biet | |
puts boggle_board.get_col(1).join("") #=> roca | |
puts boggle_board.get_col(2).join("") #=> adlk | |
puts boggle_board.get_col(3).join("") #=> etre | |
puts boggle_board.get_diagonal([1,0],[3,2]).join("") #=> ick | |
puts boggle_board.get_diagonal([1,0],[3,3]).join("") #=> This is not a diagonal | |
# create driver test code to retrieve a value at a coordinate here: | |
puts boggle_board.create_word([3,2]) #=> k | |
# Reflection: | |
# This new code had a very different implementation. In this code we are always calling the methods on an instance of the class, | |
# whereas in the last code we could just call the method and pass in the board variable to manipulate. The object oriented | |
# version is much more versatile. We can easily create multiple boggle boards and run all of the code just by calling the | |
# methods. We can also use this class in other programs and create subclasses as well. | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment