Forked from dbc-challenges/0.2.1-boggle_class_from_methods.rb
Last active
January 2, 2016 13:19
-
-
Save paopay/8309369 to your computer and use it in GitHub Desktop.
phase 0 unit 2 week 1
boggle class challenge
This file contains 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 | |
def initialize(board) | |
@board = board | |
end | |
def create_word(*coords) | |
#map through each row/col coord and return the corresponding letter | |
#join the array in to a string | |
coords.map { |coord| @board[coord.first][coord.last]}.join("") | |
end | |
def get_row(row) | |
@board[row] | |
end | |
def get_column(col) | |
column =[] | |
#iterate through board(each row is an element) and push the element | |
#inside each row which is given by col | |
@board.each { |row| column << row[col]} | |
column | |
end | |
def get_diagonal(row, col) #NOTE: Update solution below | |
# push @board[row][col] in to an array | |
# add 1 to both row and col | |
# loop until row or col == 4, which is nil in dice_grid | |
diag = [] | |
loop do | |
diag << @board[row][col] | |
row +=1 | |
col +=1 | |
break if row == 4 || col == 4 | |
end | |
diag | |
end | |
end | |
#I updated my solution for get_diagonal since I didn't understand the objectives the first time. Final solution below | |
def get_diagonal(coord1, coord2) | |
diag = [] | |
row1 = coord1[0] #=> row of coord1 | |
col1 = coord1[1] #=> col of coord1 | |
row2 = coord2[0] #=> row of coord2 | |
col2 = coord2[1] #=> col of coord2 | |
if row1 < row2 && col1 < col2 #=> S/E direction | |
loop do | |
diag << @board[row1][col1] | |
row1 +=1 | |
col1 +=1 | |
break if row1 > row2 || col1 > col2 | |
end | |
elsif row1 > row2 && col1 > col2 #=> N/W direction | |
loop do | |
diag << @board[row1][col1] | |
row1 -=1 | |
col1 -=1 | |
break if row1 < row2 || col1 < col2 | |
end | |
elsif row1 > row2 && col1 < col2 #=> N/E direction | |
loop do | |
diag << @board[row1][col1] | |
row1 -=1 | |
col1 +=1 | |
break if row1 < row2 || col1 > col2 | |
end | |
elsif row1 < row2 && col1 > col2 #=> S/W direction | |
loop do | |
diag << @board[row1][col1] | |
row1 +=1 | |
col1 -=1 | |
break if row1 > row2 || col1 < col2 #=> when coordinates don't form diagonal | |
end | |
else row1 == row2 || col1 == col2 | |
raise ArgumentError.new('invalid diagonal') | |
end | |
diag | |
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) | |
puts boggle_board.create_word([1,2], [1,1], [2,1], [3,2]) #=> returns "dock" | |
puts boggle_board.get_row(0).join("") #=> brae | |
puts boggle_board.get_row(1).join("") #=> iodt | |
puts boggle_board.get_row(2).join("") #=> eclr | |
puts boggle_board.get_row(3).join("") #=> take | |
puts boggle_board.get_column(0).join("") #=> biet | |
puts boggle_board.get_column(1).join("") #=> roca | |
puts boggle_board.get_column(2).join("") #=> adlk | |
puts boggle_board.get_column(3).join("") #=> etre | |
puts boggle_board.create_word([3,2]) #=> k | |
p boggle_board.get_diagonal(1, 1) #=> ['o', 'l', 'e'] | |
#REFLECT | |
# using the object oriented approach made it easier to manipulate objects | |
# |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment