Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save youfoundron/8325509 to your computer and use it in GitHub Desktop.
Save youfoundron/8325509 to your computer and use it in GitHub Desktop.
phase 0 unit 2 week 1 boggle class challenge
class BoggleBoard
def initialize(board) # takes nested array and sets it to board attribute
@board = board
end
def create_word(*coords) # takes multiple arrays (representing individual coordinates), return string of the joined elements at the given coordinates
coords.map { |coord| @board[coord.first][coord.last]}.join("") # splat operator '*' let's the method take multiple paramaters as an array
end
def get_letter(row, col) # takes index of row, index of column; returns letter at coordinate
@board[row][col]
end
def get_row(row) # takes index of row, returns row as an array
@board[row]
end
def get_col(col) # takes index of column, returns column as an array
col_arr = Array.new # a column can be thought of as an array containing the elements in each row at index 'col'
@board.each do |row| col_arr << row[col] end # in each row, append the item at index 'col', to the col_array
col_arr
end
def print_board # prints neatly formatted boggle board
@board.each do |row|
row.each do |letter|
print " " + letter
print " |" unless letter==row.last
end
puts
puts "---|---|---|---" unless row==@board.last
end
end
# b | r | a | e
# ---|---|---|---
# i | o | d | t
# boggle_board.print_board #=> ---|---|---|---
# e | c | l | r
# ---|---|---|---
# t | a | k | e
def get_diagonal(coord1, coord2) # takes the coordinates of two items in a diagonal, returns array of diagonal elements left->right
unless (coord2[1] - coord1[1]).abs == (coord2[0] - coord1[0]).abs # checks if coordinates are on a diagonal
raise ArgumentError.new("The coordinates must be on a diagonal inside of the Boggle Board")
else
diagonal_arr = Array.new
row = coord2[0]
col = coord2[1]
positive_slope = (coord2[1] - coord1[1] != coord2[0] - coord1[0])
if positive_slope # if positive slope
until row == @board.length-1 || col == 0 # travel left along diagonal to find left-most point
row += 1
col -= 1
end
until @board[row].nil? || @board[row][col].nil? # travel right along diagonal (adding current point to diagonal_arr) until furthest right (out of bounds)
diagonal_arr << @board[row][col]
row -= 1
col += 1
end
else # if negative slope
until row == 0 || col == 0 # travel left along diagonal to find left-most point
row -= 1
col -= 1
end
until @board[row].nil? || @board[row][col].nil? # travel right along diagonal (adding current point to diagonal_arr) until furthest right (out of bounds)
diagonal_arr << @board[row][col]
row += 1
col += 1
end
end
diagonal_arr
end
end
# Array out of bounds:
# arr = [0,1]
# arr[2]==nil #=> true
# return arr[2] #=> nil
# characteristics of a diagonal
# r,c:
# (0,0) (0,1) (0,2) (0,3)
# (1,0) (1,1) (1,2) (1,3)
# (2,0) (2,1) (2,2) (2,3)
# (3,0) (3,1) (3,2) (3,3)
#
# On a Diagonal:
# ex. (3,1)->(2,2)
# |2-1| == |2-3|
# |1| == |-1| #=> true
#
# Slope:
# :pos if c2-c1!=r2-r1
# :neg if c2-c1==r2-r1
#
# Traveling the diagonal:
# use nil return from out of bounds array call to check whether or not we are in the array
# traveling right on a positive slope: row-1, col+1
# traveling left on a positive slope: row+1, col-1
# traveling right on a negative slope: row+1, col+1
# travleing left on a negative slope: row-1, col-1
end
dice_grid = [["b", "r", "a", "e"], # instantiate nested array to represent board
["i", "o", "d", "t"],
["e", "c", "l", "r"],
["t", "a", "k", "e"]]
boggle_board = BoggleBoard.new(dice_grid) # pass dice_grid to .new to initialize a new BoggleBoard object
# create_word tests:
puts boggle_board.create_word([2,1], [1,1], [1,2], [0,3]) #=> returns "code"
puts boggle_board.create_word( [0,1], [0,2], [1,2]) #=> returns "rad"
puts boggle_board.create_word([1,3], [2,3], [3,3], [3,2]) #=> returns "trek"
puts boggle_board.create_word([3,0], [3,1], [2,1], [1,1]) #=> returns "taco"
puts
# get_row tests:
puts boggle_board.get_row(1).to_s #=> returns ["b", "r", "a", "e"]
puts boggle_board.get_row(2).to_s #=> returns ["i", "o", "d", "t"]
puts
# get_col tests:
puts boggle_board.get_col(0).to_s #=> returns ["b", "i", "e", "t"]
puts boggle_board.get_col(1).to_s #=> returns ["r", "o", "c", "a"
puts
# get_letter tests:
puts boggle_board.get_letter(0,0) #=> returns "b"
puts boggle_board.get_letter(2,3) #=> returns "r"
puts
# print_board test:
boggle_board.print_board
puts
# get_diagonal test:
puts boggle_board.get_diagonal([3,3],[0,0]).to_s #=> returns ["b","o","l","e"]
puts boggle_board.get_diagonal([0,0],[3,3]).to_s #=> returns ["b","o","l","e"]
puts boggle_board.get_diagonal([2,2],[3,1]).to_s #=> returns ["a","l","t"]
puts boggle_board.get_diagonal([0,0],[2,3]).to_s #=> raises ArgumentError
# REFLECTION:
# Object Orientation allows for greater flexibility and customization
# along with the advantage of being able to add functionality later on
# Implementation of OO requires an organizational approach that must not be ignored
# My implementation of the get_diagonal method was not very DRY but I
# am happy with it for the present moment
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment