Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save jonathonnordquist/8373034 to your computer and use it in GitHub Desktop.
Save jonathonnordquist/8373034 to your computer and use it in GitHub Desktop.
phase 0 unit 2 week 1 boggle class challenge
class BoggleBoard < Array
def get_row(row)
return self[row]
end
def get_col(col)
output_arr = []
self.each_index{|row|
output_arr << self[row][col]
}
return output_arr
end
def get_diagonal(first, second) #work in progress
w = first[0]
x = first[1]
y = second[0]
z = second[1]
if w == y - 1 && x == z - 1
output_arr = [self[w][x], self[y][z]]
i = 1
j = 1
while w - i >= 0 && x - i >= 0
output_arr.unshift(self[w - i][x - i])
i += 1
end
while y + j < self.length && z + j < self.length
output_arr.push(self[y + j][z + j])
j += 1
end
return output_arr
elsif w == y + 1 && x == z - 1
output_arr = [self[w][x], self[y][z]]
i = 1
j = 1
while w - i > 0 && x + i < self.length
output_arr.unshift(self[w + i][x - i])
i += 1
end
while y + j < self.length && z - j > 0
output_arr.push(self[y - j][z + j])
j += 1
end
return output_arr
end
end
def create_word(*letter)
word = []
letter.each{|letter|
word << self[letter[0]][letter[1]]
}
return word.join
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)
# dice_grid is already an array so I feed it to the BoggleBoard class and the boggle_board instance is already an array. I made the
# BoggleBoard a child of the Array class so that it will inherit the array methods that power my methods.
puts boggle_board.create_word([1,2],[1,1],[2,1],[3,2])
# Wasn't really sure what to do for the first part of 2. As I understand it the idea is to take a serries of coordinates input and output the
# letters that correspond. I uses arrays as input as they were eaisier, I suppose I could have used strings as well with some modifications to
# the method.
p boggle_board.get_col(0).join
p boggle_board.get_col(1).join
p boggle_board.get_col(2).join
p boggle_board.get_col(3).join
p boggle_board.get_row(0).join
p boggle_board.get_row(1).join
p boggle_board.get_row(2).join
p boggle_board.get_row(3).join
# This was eaisier, I just reused earlier methods and called .join to make them into neat strings.
p boggle_board[3][2]
# Simple enough. The boggle_board instnce is just an array object due to inheritance so I could call the coordinates just like any other array.
p boggle_board.get_diagonal([1, 1], [2, 2])
p boggle_board.get_diagonal([1, 0], [2, 1])
p boggle_board.get_diagonal([2, 1], [1, 2])
# My diagonal method seems to work given that the board is a square. Further modifications could allow it to work with boards of diferent lengths
# and the code could be possibly made DRYer but that would require changing opperators instead of variables and I'm not sure how to do that.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment