Forked from dbc-challenges/0.2.1-boggle_class_from_methods.rb
Last active
August 29, 2015 13:55
-
-
Save onweapps/8726588 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 | |
attr_accessor | |
def initialize(board) | |
@board = board | |
# boggleboard holds the dice grid as an instance variabl 'board' since you could create a boggleboard | |
# with a new board | |
end | |
def create_word(*coords) | |
coords.map { |coord| @board[coord.first][coord.last]}.join("") | |
end | |
def get_cord(row, column) | |
@board[row][column] | |
end | |
def get_row(row) | |
return @board[row] | |
end | |
def get_col(col) | |
@board.map { |x| x[col] } | |
end | |
def get_diagonal(first, second) | |
raise ArgumentError.new("Diagonals need two different points") if first == second | |
diagonal = [] | |
# identifies which coordinate is on the left to start the process of moving over column by column adding vaulues in the diagonal | |
if first[1] < second[1] | |
left = first | |
right = second | |
else | |
right = first | |
left = second | |
reverse = true | |
end | |
# holds the place of the coordinates of our most recent entry into the diagonal array | |
row = left[0] | |
column = left[1] | |
diagonal << self.get_cord(row, column) #starts the diagonal with the left most point | |
# first test if it is an upward or downward diagonal starting from the left. Then move over one column and add or subract a row | |
# (depending on if it is up or down) to find the next element of the diagonal and add it to the array | |
# continue this process until the last coordinate in the diagonal is found | |
if left[0] > right[0] | |
until row == 0 or column == (@board.length - 1) #the purpose of this instead of 3 is to accept boggle boards of any size | |
column += 1 | |
diagonal << self.get_cord(row, column) | |
end | |
else left[0] < right[0] | |
until row == (@board.length - 1) or column == (@board.length - 1) | |
row += 1 | |
column += 1 | |
diagonal << self.get_cord(row, column) | |
end | |
end | |
# raises an error if the last coord in the diagonal array is not our right coordinate, if it is not, the coords do not fall | |
# on a diagonal since all diagonals can be expressed as left to right | |
raise ArgumentError.new("This is not on a diagonal") unless diagonal[-1] == self.get_cord(right[0], right[1]) && diagonal[0] == self.get_cord(left[0], left[1]) | |
# returns the diagonal in the order they gave us the coordinates, I think users should be able to give diagonals | |
# right to left and left to right and have them returned that way | |
if reverse | |
diagonal.reverse! | |
else | |
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) | |
# should return the array for row 1 ["1", "o", "d", "t"] | |
p boggle_board.get_row(1) | |
# should return the array for collumn 3 ["e", "t", "r", "e"] | |
p boggle_board.get_col(3) | |
# should create word "dock" | |
p boggle_board.create_word([1,2], [1,1], [2,1], [3,2]) | |
# driver code should return true | |
# should return the array for row 1 ["i", "o", "d", "t"] | |
p boggle_board.get_row(1) == ["i", "o", "d", "t"] | |
# should return the array for collumn 3 ["e", "t", "r", "e"] | |
p boggle_board.get_col(3) == ["e", "t", "r", "e"] | |
# should create word "dock" | |
p boggle_board.create_word([1,2], [1,1], [2,1], [3,2]) == "dock" | |
# puts each row and each colum "take" occurs on row 3 | |
(0..3).each do |x| | |
p boggle_board.get_row(x) | |
end | |
(0..3).each do |x| | |
p boggle_board.get_col(x) | |
end | |
# should access a single coordinate - should return k for 3,2 | |
p boggle_board.get_cord(3,2) == "k" #checks to see if we can access a single coordinate'e | |
p boggle_board.get_diagonal([0,0],[3,3]) == ["b", "o", "l", "e"] # tests the long diagonal | |
p boggle_board.get_diagonal([0,1], [2,3]) == ["r", "d", "r"] # test shorter down diagonals | |
p boggle_board.get_diagonal([0,2], [1,3]) == ["a", "t"] | |
p boggle_board.get_diagonal([3,0], [0,3]) == ["t", "c", "d", "e"] # test up diagonals | |
p boggle_board.get_diagonal([3,3], [0,0]) == ["e", "l", "o", "b"] #tests reverse order diagonals | |
p boggle_board.get_diagonal([0,0], [1,3]) #tests argument error if coords arent diagonal | |
p boggle_board.get_diagonal([0,0], [0,0]) #test argument error if coors are the same | |
# Reflection | |
# | |
# I realize the #create_word could have been used as #get_cord, but I thought it made more sense to have to | |
# separate methods for the different functions - the get cord function was useful for | |
# | |
# Object oriented programming is being able to make an object that the computer understands to have certain funtionality | |
# so that you can continue to modify and work with that object and make new objects with the same capabilities. | |
# it seems like the main implementation difference is creating a class and building those methods within the class. So that you | |
# can repeat the process again. | |
# my strategy was veru successful for this, the only thing I had to deal with that hadnt been accomplished in the past | |
# exercise was teh get diagonal bonus. ultimately I was able to solve it fairly easily. My logic was left to right. I think | |
# a more elegant solution may have come out of using a #map method and using a top to bottom logic |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment