Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save plondon/8712745 to your computer and use it in GitHub Desktop.
Save plondon/8712745 to your computer and use it in GitHub Desktop.
phase 0 unit 2 week 1 boggle class challenge
class BoggleBoard
attr_reader :board
def initialize(board)
@board = board
end
# Joining words and finding them based on the coordinate position
# we iterate through coords, which are array positions such as [1,2]
# and find their value on the board by writing board[coord.first][coord.last]
# finally after we are doing mapping them into an array we join each
# element together.
def create_word(*coords)
coords.map { |coord| board[coord.first][coord.last] }.join("")
end
# This method lets us view the entire row
def get_row(row)
board[row]
end
# This method let's us view the entire column by setting the row equal
# to the range of 0..board.length-1 (in the first case 3) we then iterate
# through each and create an array of the row value for each column
def get_col(col)
row = (0..board.length-1).to_a
row.map { |pos| board[pos][col] }
end
# This method allows us to get a coordinate. It is pretty unnecessary but
# was helpful when I was trying to figure my way through the diagonal method.
def get_coord(coord1, coord2)
board[coord1][coord2]
end
def diagonal(c1, c2, c3, c4)
diagonal_array = [get_coord(c1,c2)]
if (c1 - c2).abs == (c3 - c4).abs || (c1+c2) == (c3+c4)
if c1 > c3 && c2 < c4
until c1 == c3
c1 -= 1
c2 += 1
diagonal_array << get_coord(c1,c2)
end
end
if c1 > c3 && c2 > c4
until c1 == c3 && c2 == c4
c1 -= 1
c2 -= 1
diagonal_array << get_coord(c1,c2)
end
end
if c1 < c3 && c2 < c4
until c1 == c3
c1 += 1
c2 += 1
diagonal_array << get_coord(c1,c2)
end
end
if c1 < c3 && c2 > c4
until c1 == c3 && c2 == c4
c1 += 1
c2 -= 1
diagonal_array << get_coord(c1,c2)
end
end
else
raise ArgumentError, "Not valid"
end
diagonal_array
end
end
dice_grid = [["b", "r", "a", "e"],
["i", "o", "d", "t"],
["e", "c", "l", "r"],
["t", "a", "k", "e"]]
# driver code for get_row, get_col, and diagonal methods
boggle_board = BoggleBoard.new(dice_grid)
p boggle_board.create_word([1,2],[1,1],[2,1],[3,2]) == "dock" #=> true
p boggle_board.get_row(1) == ["i", "o", "d", "t"] #=> true
p boggle_board.get_col(1) == ["r", "o", "c", "a"] #=> true
p boggle_board.get_coord(1,2) == "d"
p boggle_board.diagonal(0,1,2,3)
p boggle_board.diagonal(0,2,2,0)
p boggle_board.diagonal(1,2,3,0)
p boggle_board.diagonal(0,3,3,0)
# create driver test code to retrieve a value at a coordinate here:
p dice_grid[3][2] == "k"
# reflection
# took awhile to get this one because I hadn't traveresed this many layers of an
# array before. hard to keep track of which array was nested where, and counting
# by zeros just adds to the overall confusion. this can definitely be refactored
# and I should probably change some of the variable names, like c1 and c2
# I couldn't figure out how to pass through arguments that were connected. Maybe try
# refactoring with instead of greater than the other variable, if c2 is greater
# than 1, or greater than or equal to half the nested arrays length, if it is
# than I need to count down.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment