Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save pdwittig/8771773 to your computer and use it in GitHub Desktop.
Save pdwittig/8771773 to your computer and use it in GitHub Desktop.
phase 0 unit 2 week 1 boggle class challenge
class BoggleBoard
#your code here
def initialize(dice_grid)
@dice_grid = dice_grid
end
def create_word(*coords)
coords.map { |coord| @dice_grid[coord.first][coord.last] }.join
end
def get_row(row)
@dice_grid[row]
end
def get_col(col)
@dice_grid.transpose[col]
end
def get_coord(row, col)
@dice_grid[row][col]
end
# First check to see if two coord are diagonal - raise error if not, else proceed
# def get_diagonal(*coords)
# coords.sort!
# if coords.first.last > coords.last.last
# while
# else
# 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:
# constuctor should take 1 argument
p boggle_board.method(:initialize).arity == 1
# Test #create_word method
p boggle_board.create_word([0,0] , [0,1] , [0,2] , [1,3]) == "brat"
p boggle_board.create_word([2,1] , [1,1] , [0,1] , [1,2]) == "cord"
p boggle_board.create_word([0,0] , [1,1] , [2,2] , [1,2]) == "bold"
p boggle_board.create_word([3,2] , [3,1] , [3,0] , [2,0]) == "kate"
p boggle_board.create_word([1,2] , [1,1] , [2,1] , [3,2]) == "dock"
# Test the #get_row method
p boggle_board.get_row(0) == ["b", "r", "a", "e"]
p boggle_board.get_row(1) == ["i", "o", "d", "t"]
p boggle_board.get_row(2) == ["e", "c", "l", "r"]
p boggle_board.get_row(3) == ["t", "a", "k", "e"]
# Test the #get_col method
p boggle_board.get_col(0) == ["b", "i", "e", "t"]
p boggle_board.get_col(1) == ["r", "o", "c", "a"]
p boggle_board.get_col(2) == ["a", "d", "l", "k"]
p boggle_board.get_col(3) == ["e", "t", "r", "e"]
# Test the #get_coord method
p boggle_board.get_coord(0,1) == "r"
p boggle_board.get_coord(1,3) == "t"
p boggle_board.get_coord(2,1) == "c"
p boggle_board.get_coord(0,3) == "e"
# Test the #get_diagonal method
# p boggle_board.get_diagonal([0,1],[1,1]) == ["b", "o", "l", "e"]
# p boggle_board.get_diagonal([3,1] , [2,2]) == ["a" , "l" , "t"]
# p boggle_board.get_diagonal([0,2],[1,2]) == ["r" , "d" , "r"]
# p boggle_board.get_diagonal([1,0] ,[2,1]) == ["i" , "c" , "k"]
# test = [ [3, 0] ,[1,1] , [0,1]]
# puts test.sort!
# puts "aha #{test.last.last}"
# Test board coords
# puts boggle_board[0][1] == "i" # => true
# puts boggle_board[1][3] == "a" # => true
# puts boggle_board[3][2] == "r" # => true
# puts boggle_board[0][3] == "t" # => true
# REFLECTION
=begin
The main parts of this challenge were not to tough because they were just
hitting on the same theory from the nested arraty challenge but building it
using OOP. OOP provides us with a couple of cool benefits - we can model real
world objects along with their state/actions, and then create as many objects of
this type as we need. I did plan on doing the bonus section but even though it may
be simple, I felt like I was banging my head up against a wall (my brain kinda feels like
mush right now as I just had a 16 hour flight to Athens this morning and I super jet lagged).
Anyways, I feel like if I get some rest, and come back and try the get_diagonal method
in the monring, it wont be as hard as it seems now...
=end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment