Forked from dbc-challenges/0.2.1-boggle_class_from_methods.rb
Last active
January 2, 2016 10:18
-
-
Save dwilbank68/8288698 to your computer and use it in GitHub Desktop.
phase 0 unit 2 week 1
boggle 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 | |
def initialize(board) | |
@boggle_board = board | |
@diag = [] | |
end | |
def create_word(*coords) | |
coords.map { |coord| @boggle_board[coord.first][coord.last]}.join("") | |
end | |
def get_row(row) | |
@boggle_board[row] | |
end | |
def get_col(col) | |
column = [] | |
@boggle_board.each { |row| column << row[col] } | |
column | |
end | |
def print_rows_columns | |
@boggle_board.each_with_index do |dummy, index| | |
p self.get_row(index).join('') | |
p self.get_col(index).join('') | |
end | |
end | |
def get_diagonal(coord1, coord2) #0,0 3,3 | |
@c1 = coord1; @c2 = coord2 | |
@d_row, @d_col = 1, 1 if @c1.first < @c2.first && @c1.last < @c2.last #se | |
@d_row, @d_col = -1, -1 if @c1.first > @c2.first && @c1.last > @c2.last #nw | |
@d_row, @d_col = -1, 1 if @c1.first > @c2.first && @c1.last < @c2.last #ne | |
@d_row, @d_col = 1, -1 if @c1.first < @c2.first && @c1.last > @c2.last #sw | |
scan_diag # calls method with the delta values established above | |
end | |
def scan_diag | |
@row = @c1.first; @col = @c1.last | |
@diag.push(@boggle_board[@row][@col]) | |
loop do | |
@row += @d_row; @col += @d_col | |
break if out_of_bounds? | |
@diag << @boggle_board[@row][@col] | |
break if @row == @c2.first || @col == @c2.last | |
end | |
if @row == @c2.first && @col == @c2.last | |
puts @diag.join('') | |
else | |
puts "not a diagonal" | |
end | |
@diag.clear | |
end | |
def out_of_bounds? | |
width = @boggle_board[0].length - 1 | |
height = @boggle_board.length - 1 | |
@row < 0 || @row > height || @col < 0 || @col > width | |
end | |
end | |
dice_grid = [["b", "r", "a", "e"], | |
["i", "o", "d", "t"], | |
["e", "c", "l", "r"], | |
["t", "a", "k", "e"]] | |
# ----------------- 1 -------------------- | |
boggle_board = BoggleBoard.new(dice_grid) | |
# ----------------- 2 -------------------- | |
puts boggle_board.create_word([1,2], [1,1], [2,1], [3,2]) #=> returns "dock" | |
boggle_board.print_rows_columns | |
#"brae" scottish word | |
#"biet" | |
#"iodt" | |
#"roca" | |
#"eclr" | |
#"adlk" | |
#"take" english word | |
#"etre" french word - minus the little symbol | |
# ----------------- 3 -------------------- | |
row = boggle_board.get_row(3) | |
p row[2] # is "k" | |
# ???? you mean like this ???? | |
# ----------------- 4 -------------------- | |
# note - can be refactored with conditionals so that only 1 direction is checked instead of all 4 | |
# | |
boggle_board.get_diagonal( [0,1], [2,3]) # == "rdr" | |
boggle_board.get_diagonal( [3,3], [0,0]) # == "elob" | |
boggle_board.get_diagonal( [3,2], [1,0]) # == "kci" | |
boggle_board.get_diagonal( [3,0], [0,3]) # == "tcde" | |
boggle_board.get_diagonal( [1,1], [2,2]) # == "ol" | |
boggle_board.get_diagonal( [0,2], [2,0]) # == "aoe" | |
boggle_board.get_diagonal( [0,2], [0,2]) # == "not a diagonal" | |
boggle_board.get_diagonal( [0,1], [3,3]) # == "not a diagonal" | |
# ------------------ 5 -------------------- | |
# What are the benefits to using the Object Oriented approach (even if it is a bit more code?) | |
# all methods are compartmentalized |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment