Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save brittanydmiller/8741384 to your computer and use it in GitHub Desktop.
Save brittanydmiller/8741384 to your computer and use it in GitHub Desktop.
phase 0 unit 2 week 1boggle class challenge
class BoggleBoard
def initialize(grid)
@grid = grid
end
def create_word(*coords)
coords.map { |coord| @grid[coord.first][coord.last] }.join("")
end
def get_row(row)
@row = row
@grid[row].join("")
end
def get_column(col)
column=[]
@grid.each_index{ |index| column << @grid[index][col]} #Is there a way to use get_row here instead?
column.join("")
end
#BONUS PSEUDOCODE:
#Check to make sure it's actually a diagonal. Return an array of values, will need 2 coordinates entered to define the diagonal.
#Enter the start point and end point (a,b)(c,d). If |a - c| == |b - d| , it's a diagonal. Otherwise raise error.
#Hmm is there a way to use create_word in this method so I'm not repeating myself?
#If I want to open it up to 3-letter diagonals and not just the 2 corner diagonals, I'm going to need a loop.
#push into diag the value at point1
#until point2 (and including)
#push into diag the value at (point1[0]+1, point1[1]+1) if slope is negative, or (point1[0]+1, point1[1]-1) if slope is positive
#hmmm could this use recursion?
def is_diag?(point1, point2)
(point1[0] - point2[0]).abs == (point1[1] - point2[1]).abs ? true:false
end
def get_diagonal(point1, point2)
@point1 = point1
@point2 = point2
raise ArgumentError, 'Coordinates do not describe a diagonal' unless is_diag?(@point1,@point2)
#Breaking out the x's and y's to keep things clear
@row1 = point1[0]
@row2 = point2[0]
@col1 = point1[1]
@col2 = point2[1]
#Need to determine the slope direction to see which way to move through the grid
@slope = @row2 - @row1
#Now move throughout the grid and push letters into array 'diag'
diag=[]
row_coord = @row1
col_coord = @col1
if @slope < 0 #backslash diagonals of any length
while col_coord <= @col2 do
diag << @grid[row_coord][col_coord]
row_coord -= 1
col_coord += 1
end
else # forward slash diagonals of any length
while row_coord <= @row2 do
diag << @grid[row_coord][col_coord]
row_coord += 1
col_coord += 1
end
end
diag.join("")
end
def get_coords(row, col)
@grid[row][col] # I don't like that my coordinate inputs require different formatting here than with create_word...
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([1,2], [1,1], [2,1], [3,2]) == "dock"
p boggle_board.get_row(0) == "brae"
p boggle_board.get_row(1) == "iodt"
p boggle_board.get_row(2) == "eclr"
p boggle_board.get_row(3) == "take"
p boggle_board.get_column(0) == "biet"
p boggle_board.get_column(1) == "roca"
p boggle_board.get_column(2) == "adlk"
p boggle_board.get_column(3) == "etre"
# create driver test code to retrieve a value at a coordinate here:
p boggle_board.get_coords(3,2) == "k"
p boggle_board.get_diagonal([0,0],[3,3]) == "bole"
p boggle_board.get_diagonal([2,0],[0,2]) == "eoa"
### REFLECTION
=begin
I think I finally understand classes. In a way it's like a big-daddy method.
You have to define variables but there are more flavors available.
You have to take arguments somehow (Initialize method).
You have attributes (reminds me of Javascript) and then you just interact with other normal methods inside.
It's not a perfect match, but it's how it started to finally make sense to me.
The bonus problem is a fun puzzle -- one thing that's throwing me is whether the diagonals must be reported
left-to-right or if they can be either way. Having a hard time with slope and x/y coords since the origin point
in a grid like this is the top left, and the x and y axis are swapped from the way you display vars in algebra.
I am so confused!! Finally changed all my x and y refs to row and column refs and then I was able to sort out
my brain.
=end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment