Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save smithjason/9150181 to your computer and use it in GitHub Desktop.
Save smithjason/9150181 to your computer and use it in GitHub Desktop.
phase 0 unit 2 week 1boggle class challenge
=begin
PSEUDOCODE FOR BONUS QUESTION
after going through the exercise and then "f@#! it, it's friday"-ing the bonus, it bugged
me that I did, so I doodled on some paper and found a relationship that I think makes this
bonus question much more approachable:
for any valid diagonal pair of coordinates, the following is true:
- The quotient of (end_coord.first - start_coord.first) and (end_coord.last - start_coord.last) is 1 or -1.
- Therefore, the absolute value of this quotient will always be 1.
- If the value deried from this absolute value quotient is anything except 1, then the pair of coordinates
are not valid diagonals.
I can implement #are_diagonals?(start_coord, end_coord) to check for valid diagonal coordinate pairs before
doing any work for generating the diagonal array of values between(inclusive) the two values.
The differences also give me the length of the translations for the row and columns and the directions I need
to go as well.
I can use the start, a current, and the end coordinates to grab the diagonal. I start off the diagonal array with
the value at start and, while current != end, push the value at current onto the diagonal array and set current to
the next coordinate value based on the direction I need to go.
=end
class BoggleBoard
@@BOARD_LOWERBOUND = 0
@@BOARD_UPPERBOUND = 3
def initialize(board)
@board = board
end
def create_word(*coords)
coords.map { |coord| @board[coord.first][coord.last]}.join("")
end
def get_row(row)
@board[row].inspect
end
def get_col(col)
@board.map {|row| row[col] }.inspect
end
def get_coord(coord)
@board[coord.first][coord.last] if valid_coordinate?(coord)
end
#ITERATIVE METHOD
# def get_diagonal(start_coord, end_coord)
# if are_diagonals?(start_coord, end_coord) && valid_coordinate?(start_coord) && valid_coordinate?(end_coord)
# row_direction = row_difference(start_coord, end_coord) / row_difference(start_coord, end_coord).abs # -1 or 1
# col_direction = col_difference(start_coord, end_coord) / col_difference(start_coord, end_coord).abs # -1 or 1
# current_coord = [start_coord.first + row_direction, start_coord.last + col_direction]
# diagonal_selection = []
# diagonal_selection << @board[start_coord.first][start_coord.last]
# while current_coord != end_coord # base case conditional for iterative
# diagonal_selection << @board[current_coord.first][current_coord.last]
# current_coord = [current_coord.first + row_direction, current_coord.last + col_direction]
# end
# diagonal_selection << @board[end_coord.first][end_coord.last]
# else
# "Invalid Diagonal Coordinates"
# end
# end
# RECURSIVE METHOD
def get_diagonal(start_coord, end_coord)
if are_diagonals?(start_coord, end_coord) && valid_coordinate?(start_coord) && valid_coordinate?(end_coord)
row_direction = row_difference(start_coord, end_coord) / row_difference(start_coord, end_coord).abs # -1 or 1 denotes which way on board it has to move
col_direction = col_difference(start_coord, end_coord) / col_difference(start_coord, end_coord).abs # -1 or 1 denotes which way on board it has to move
current_coord = [start_coord.first + row_direction, start_coord.last + col_direction]
diagonal_selection = []
diagonal_selection.push(@board[start_coord.first][start_coord.last])
if current_coord == end_coord
return diagonal_selection.push(@board[end_coord.first][end_coord.last])
else
return diagonal_selection.push(get_diagonal(current_coord, end_coord)).flatten
end
else
"Invalid Diagonal Coordinates"
end
end
def valid_coordinate?(coord)
coord.all? {|value| value >= @@BOARD_LOWERBOUND && value <= @@BOARD_UPPERBOUND }
end
def are_diagonals?(start_coord, end_coord)
begin
1 == (row_difference(start_coord, end_coord).to_f / col_difference(start_coord, end_coord).to_f).abs
rescue ZeroDivisionError
false
end
end
def row_difference(start_coord, end_coord)
end_coord.first - start_coord.first
end
def col_difference(start_coord, end_coord)
end_coord.last - start_coord.last
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:
puts "create_word tests"
puts boggle_board.create_word([1,2], [1,1], [2,1], [3,2]) == "dock" #=> returns "dock"
puts boggle_board.create_word([2,1], [1,1], [1,2], [0,3]) == "code" #=> returns "code"
puts boggle_board.create_word([0,1], [0,2], [1,2]) == "rad" #=> returns "rad"
puts boggle_board.create_word([0,0], [1,0], [1,1]) == "bio" #=> returns "bio"
puts boggle_board.create_word([3,0], [3,1], [2,2], [3,2]) == "talk" #=> returns "talk"
puts "get_row and get_col tests"
puts boggle_board.get_row(0) == "[\"b\", \"r\", \"a\", \"e\"]" #=> ["b", "r", "a", "e"]
puts boggle_board.get_row(1) == "[\"i\", \"o\", \"d\", \"t\"]" #=> ["i", "o", "d", "t"]
puts boggle_board.get_row(2) == "[\"e\", \"c\", \"l\", \"r\"]" #=> ["e", "c", "l", "r"]
puts boggle_board.get_row(3) == "[\"t\", \"a\", \"k\", \"e\"]" #=> ["t", "a", "k", "e"]
puts boggle_board.get_col(0) == "[\"b\", \"i\", \"e\", \"t\"]" #=> ["b", "i", "e", "t"]
puts boggle_board.get_col(1) == "[\"r\", \"o\", \"c\", \"a\"]" #=> ["r", "o", "c", "a"]
puts boggle_board.get_col(2) == "[\"a\", \"d\", \"l\", \"k\"]" #=> ["a", "d", "l", "k"]
puts boggle_board.get_col(3) == "[\"e\", \"t\", \"r\", \"e\"]" #=> ["e", "t", "r", "e"]
puts "get_coord tests"
puts boggle_board.get_coord([3,2]) == "k" #=> k
puts boggle_board.get_coord([0,1]) == "r" #=> r
puts boggle_board.get_coord([1,3]) == "t" #=> t
puts boggle_board.get_coord([2,0]) == "e" #=> e
puts "valid_coordinate? tests"
puts boggle_board.valid_coordinate?([3,2]) == true
puts boggle_board.valid_coordinate?([-1, 3]) == false
puts boggle_board.valid_coordinate?([0,4]) == false
puts boggle_board.valid_coordinate?([0,3]) == true
puts "are_diagonals? tests"
puts boggle_board.are_diagonals?([1,1],[3,3]) == true
puts boggle_board.are_diagonals?([3,1],[2,0]) == true
puts boggle_board.are_diagonals?([1,3],[2,0]) == false
puts boggle_board.are_diagonals?([0,2],[3,2]) == false
puts "diagonal tests"
puts boggle_board.get_diagonal([3,0],[0,3]) == ["t","c","d","e"]
puts boggle_board.get_diagonal([3,2],[1,0]) == ["k","c","i"]
puts boggle_board.get_diagonal([3,3],[0,0]) == ["e","l","o","b"]
puts boggle_board.get_diagonal([2,1],[1,0]) == ["c","i"]
puts boggle_board.get_diagonal([1,1],[3,3]) == ["o","l","e"]
puts boggle_board.get_diagonal([2,0],[3,1]) == ["e","a"]
puts boggle_board.get_diagonal([1,3],[3,1]) == ["t","l","a"]
puts "invalid tests"
puts boggle_board.get_diagonal([3,0],[3,0]) == "Invalid Diagonal Coordinates"
puts boggle_board.get_diagonal([0,0],[0,0]) == "Invalid Diagonal Coordinates"
puts boggle_board.get_diagonal([1,1],[3,2]) == "Invalid Diagonal Coordinates"
puts boggle_board.get_diagonal([1,3],[2,0]) == "Invalid Diagonal Coordinates"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment