Forked from dbc-challenges/0.2.1-boggle_class_from_methods.rb
Last active
August 29, 2015 13:56
-
-
Save e7chun/9180235 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_reader :dice_grid | |
def initialize(dice_grid) | |
@dice_grid = dice_grid | |
end | |
def create_word(*coords) | |
p coords.map {|coord| @dice_grid[coord.first][coord.last]}.join("") | |
end | |
def get_row(row) | |
p @dice_grid[row].join | |
end | |
def get_column(col) | |
column_array = [] | |
@dice_grid.each_index do |i| | |
@dice_grid[i].each_index do |j| | |
if j == col | |
column_array.push(@dice_grid[i][j]) | |
end | |
end | |
end | |
p column_array.join | |
end | |
def get_diagonal(coord1, coord2) | |
start0 = coord1[0] | |
start1 = coord1[1] | |
end0 = coord2[0] | |
end1 = coord2[1] | |
diagonal0 = (end0 - start0).abs | |
diagonal1 = (end1 - start1).abs | |
diagonal_array = [] | |
if diagonal0 == diagonal1 | |
if start0 < end0 && start1 < end1 #going SE from starting point | |
while start0 < end0 | |
diagonal_array.push(@dice_grid[start0][start1]) | |
start0 += 1 | |
start1 += 1 | |
end | |
diagonal_array.push(@dice_grid[end0][end1]) | |
elsif start0 > end0 && start1 > end1 #going NW from starting point | |
while start0 > end0 | |
diagonal_array.push(@dice_grid[start0][start1]) | |
start0 -= 1 | |
start1 -= 1 | |
end | |
diagonal_array.push(@dice_grid[end0][end1]) | |
elsif start0 < end0 && start1 > end1 #going SW from starting point | |
while start0 < end0 | |
diagonal_array.push(@dice_grid[start0][start1]) | |
start0 += 1 | |
start1 -= 1 | |
end | |
diagonal_array.push(@dice_grid[end0][end1]) | |
elsif start0 > end0 && start1 < end1 #going NE from starting point | |
while start0 > end0 | |
diagonal_array.push(@dice_grid[start0][start1]) | |
start0 -= 1 | |
start1 += 1 | |
end | |
diagonal_array.push(@dice_grid[end0][end1]) | |
end | |
p diagonal_array | |
else | |
puts "This is not a 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) | |
# implement tests for each of the methods here: | |
boggle_board.create_word([1,2], [1,1], [2,1], [3,2]) | |
boggle_board.get_row(0) #==> brae | |
boggle_board.get_row(1) #==> iodt | |
boggle_board.get_row(2) #==> eclr | |
boggle_board.get_row(3) #==> take | |
boggle_board.get_column(0) #==> biet | |
boggle_board.get_column(1) #==> roca | |
boggle_board.get_column(2) #==> adlk | |
boggle_board.get_column(3) #==> etre | |
boggle_board.get_diagonal([3,1],[1,3]) | |
# create driver test code to retrieve a value at a coordinate here: | |
boggle_board.create_word([3,2]) | |
##Reflection | |
#The implementation is very similar. In terms of logic, it's very similar, if not the same | |
#as the nested array problem. The fact that we're using an OOP makes it very easy to | |
#manipulate the primary object, which was dice_grid. All we had to do was convert dice_grid | |
#into a boggle_board and then the manipulation from there was very simple. | |
#As for the diagonal method, I was having trouble until I realized that it has to | |
#be a perfect diagonal (i.e. [1,3] -> [3,1]) as opposed to coordinates such as [1,2] -> [3,3]. | |
#This made things much easier. I took consideration of all possible diagonals and manipulated | |
#the x and y coordinates of the start and ending points. | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment