Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save alosada/9166689 to your computer and use it in GitHub Desktop.
Save alosada/9166689 to your computer and use it in GitHub Desktop.
phase 0 unit 2 week 1boggle class challenge
class BoggleBoard
def initialize(dice_grid) # Initialize method
raise ArgumentError.new("You did not input a suitable dice grid!") if ! (dice_grid.kind_of?(Array) && dice_grid.length == 4 && dice_grid[0].length == 4 )
# Live above tests for adecuate input
@dice_grid = dice_grid # How does the boggle_board object hold the dice_grid? Making it a class variable.
end
def create_word(*coords) # Defines the method to create a word, taken from the previous boggle excercise.
coords.map { |coord| @dice_grid[coord.first][coord.last]}.join("")
end
def get_row(row) # Method to get rows. Since the parent array is made of "rows", we jsut have to call the index of the desired row.
@dice_grid[row]
end
def get_col(col) # Method to get columns. Achieved by keeping the index of the "row" arrays fixed to the appropiate column, an iterating through rows.
@dice_grid.map { |row| row[col]}
end
def get_coord(coord) # MEthod to get a specific coordinate, we jsut have to include de index for the rown, and then for the column.
raise ArgumentError.new("Your input is not an 2-value array!") if ! coord.kind_of?(Array) && coord.length != 2
raise ArgumentError.new("Your Y coordinate is out of range!") if coord.first > 3 || coord.first < 0
raise ArgumentError.new("Your X coordinate is out of range!") if coord.first > 3 || coord.first < 0
@dice_grid[coord.first][coord.last]
end
def get_diag(coord) # Method to get a diagonal. Asumes only 2 diagonals (no rain method!). Identifies the diagonal checkign if coordnates being
#called are equan to each other, or add "3", the a while loop runs through the diagonal, storing its coordinates in an array.
raise ArgumentError.new("Your input is not an 2-value array!") if ( ! coord.kind_of?(Array)) && coord.length != 2 # Adecuate input test 1
raise ArgumentError.new("Your coordinate is not in a diagonal!") if ! ( coord.first == coord.first || coord.first + coord.first == 3 ) # Adecuate input test 2
diagonal = [] # Output array
index = 0 # Index for while loop.
while index <= 3
if [coord.first] == [coord.last]
diagonal << @dice_grid[index][index]
else
diagonal << @dice_grid[index][3-index]
end
index +=1
end
return diagonal
end
end
dice_grid = [["b", "r", "a", "e"], #Input board.
["i", "o", "d", "t"],
["e", "c", "l", "r"],
["t", "a", "k", "e"]]
# implement tests for each of the methods here:
boggle_board = BoggleBoard.new(dice_grid) # Initiates an isntance of BoggleBoard
puts boggle_board.kind_of?(BoggleBoard) # Test if an instance of BoggleBoard was created.
puts boggle_board.create_word([1,2],[1,1],[2,1],[3,2]) == "dock" # Tests if create_word method is working.
puts boggle_board.get_row(0) == ["b", "r", "a", "e"] # Tests if get_row method is working.
puts boggle_board.get_col(0) == ["b", "i", "e", "t"] # Tests if get_col method is working.
puts boggle_board.get_diag([1,1]) == ["b", "o", "l", "e"] # Tests if get_diag method is working for the first diagonal.
puts boggle_board.get_diag([1,2]) == ["e", "d", "c", "t"] # Tests if get_diag method is working for the second diagonal.
# create driver test code to retrieve a value at a coordinate here:
puts boggle_board.get_coord([3,2]) == "k" # Tests if get_coord method is working.
# Reflection:
# MY strategy worked. Getting the diagonal was a bit tricky, but managed to get it done by using a single while loop. There was little
# new in the excercise otherwise. I wonder if I'm overutilizing arguments as I check the input for errors? This was a fun excercise!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment