Forked from dbc-challenges/0.2.1-boggle_class_from_methods.rb
Last active
August 29, 2015 13:56
-
-
Save smithjason/9226143 to your computer and use it in GitHub Desktop.
phase 0 unit 2 week 1boggle 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
=begin | |
REFACTOR ON 2/25 AFTER READING 2ND CHAPTER OF "Practical Object Oriented Design in Ruby" | |
I'm going to attempt to add in an approach that utilizes a Struct for the board so that I can let people | |
specify rows and columns as they see them using numbers 1-4 instead of 0-3 (so that they're not even | |
thinking about the underlying multi-dimensional array data structure when making calls to a BoggleBoard | |
object). This is also an attempt at refactoring so that methods outside of #get_row, #get_col, and #get_coord | |
don't need to worry about knowing what data structure is used to hold the board information: they can just | |
request the info they want. According to POODR, this really helps keep the code as changeable as possible | |
while minimizing the likelihood of a change adversely affecting other areas. | |
get_diagonal pseudocode: | |
#if coords are diagonals and valid | |
# => determine row direction and column direction | |
# => form array of inclusive coordinates in the diagonal | |
# => map new array from get_coord'ing diagonal coordinate array and return it | |
=end | |
=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 = 1 | |
@@BOARD_UPPERBOUND = 4 | |
attr_reader :rows | |
def initialize(board) | |
@rows = rowify(board) | |
end | |
def create_word(*coords) | |
coords.collect{|coord| get_coord(coord)}.join("") | |
end | |
def get_row(row) | |
rows[row-1].values | |
end | |
def get_col(col) | |
rows.collect{|row| row.send("col_#{col}")} | |
end | |
def get_coord(coord) | |
rows[coord.first-1].send("col_#{coord.last}") if valid_coordinate?(coord) | |
end | |
def get_diagonal(start_coord, end_coord) | |
if are_diagonals?(start_coord, end_coord) && valid_coordinate?(start_coord) && valid_coordinate?(end_coord) | |
diagonal_coords = build_diagonal_coordinates(start_coord, end_coord) | |
diagonal_coords.collect{|coord| get_coord(coord)} | |
else | |
"Invalid Diagonal Coordinates" | |
end | |
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 valid_coordinate?(coord) | |
coord.all?{|value| value >= @@BOARD_LOWERBOUND && value <= @@BOARD_UPPERBOUND} | |
end | |
private | |
Row = Struct.new(:col_1,:col_2,:col_3,:col_4) | |
def rowify(board) | |
board.collect{|row| Row.new(row[0],row[1],row[2],row[3])} | |
end | |
def build_diagonal_coordinates(start_coord, end_coord) | |
row_direction = row_direction(start_coord, end_coord) | |
col_direction = col_direction(start_coord, end_coord) | |
diagonal_coords = [start_coord] | |
current_coord = start_coord | |
until current_coord == end_coord | |
current_coord = [current_coord.first + row_direction, current_coord.last + col_direction] | |
diagonal_coords << current_coord | |
end | |
diagonal_coords | |
end | |
def row_direction(start_coord, end_coord) | |
row_difference(start_coord, end_coord) / row_difference(start_coord, end_coord).abs | |
end | |
def col_direction(start_coord, end_coord) | |
col_difference(start_coord, end_coord) / col_difference(start_coord, end_coord).abs | |
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([2,3], [2,2], [3,2], [4,3]) == "dock" #=> returns "dock" | |
puts boggle_board.create_word([3,2], [2,2], [2,3], [1,4]) == "code" #=> returns "code" | |
puts boggle_board.create_word([1,2], [1,3], [2,3]) == "rad" #=> returns "rad" | |
puts boggle_board.create_word([1,1], [2,1], [2,2]) == "bio" #=> returns "bio" | |
puts boggle_board.create_word([4,1], [4,2], [3,3], [4,3]) == "talk" #=> returns "talk" | |
puts "get_row and get_col tests" | |
puts boggle_board.get_row(1) == ["b", "r", "a", "e"] #{}"[\"b\", \"r\", \"a\", \"e\"]" | |
puts boggle_board.get_row(2) == ["i", "o", "d", "t"] #{}"[\"i\", \"o\", \"d\", \"t\"]" #=> | |
puts boggle_board.get_row(3) == ["e", "c", "l", "r"] #{}"[\"e\", \"c\", \"l\", \"r\"]" #=> | |
puts boggle_board.get_row(4) == ["t", "a", "k", "e"] #{}"[\"t\", \"a\", \"k\", \"e\"]" #=> | |
puts boggle_board.get_col(1) == ["b", "i", "e", "t"] #{}"[\"b\", \"i\", \"e\", \"t\"]" #=> | |
puts boggle_board.get_col(2) == ["r", "o", "c", "a"] #{}"[\"r\", \"o\", \"c\", \"a\"]" #=> | |
puts boggle_board.get_col(3) == ["a", "d", "l", "k"] #{}"[\"a\", \"d\", \"l\", \"k\"]" #=> | |
puts boggle_board.get_col(4) == ["e", "t", "r", "e"] #{}"[\"e\", \"t\", \"r\", \"e\"]" #=> | |
puts "get_coord tests" | |
puts boggle_board.get_coord([4,3]) == "k" #=> k | |
puts boggle_board.get_coord([1,2]) == "r" #=> r | |
puts boggle_board.get_coord([2,4]) == "t" #=> t | |
puts boggle_board.get_coord([3,1]) == "e" #=> e | |
puts "valid_coordinate? tests" | |
puts boggle_board.valid_coordinate?([4,3]) == true | |
puts boggle_board.valid_coordinate?([-1, 3]) == false | |
puts boggle_board.valid_coordinate?([0,4]) == false | |
puts boggle_board.valid_coordinate?([4,0]) == false | |
puts boggle_board.valid_coordinate?([1,4]) == true | |
puts "are_diagonals? tests" | |
puts boggle_board.are_diagonals?([2,2],[4,4]) == true | |
puts boggle_board.are_diagonals?([4,2],[3,1]) == true | |
puts boggle_board.are_diagonals?([2,4],[3,1]) == false | |
puts boggle_board.are_diagonals?([1,3],[4,3]) == false | |
puts "diagonal tests" | |
puts boggle_board.get_diagonal([4,1],[1,4]) == ["t","c","d","e"] | |
puts boggle_board.get_diagonal([4,3],[2,1]) == ["k","c","i"] | |
puts boggle_board.get_diagonal([4,4],[1,1]) == ["e","l","o","b"] | |
puts boggle_board.get_diagonal([3,2],[2,1]) == ["c","i"] | |
puts boggle_board.get_diagonal([2,2],[4,4]) == ["o","l","e"] | |
puts boggle_board.get_diagonal([3,1],[4,2]) == ["e","a"] | |
puts boggle_board.get_diagonal([2,4],[4,2]) == ["t","l","a"] | |
puts "invalid tests" | |
puts boggle_board.get_diagonal([4,1],[4,1]) == "Invalid Diagonal Coordinates" | |
puts boggle_board.get_diagonal([1,1],[1,1]) == "Invalid Diagonal Coordinates" | |
puts boggle_board.get_diagonal([2,2],[4,3]) == "Invalid Diagonal Coordinates" | |
puts boggle_board.get_diagonal([2,4],[3,1]) == "Invalid Diagonal Coordinates" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment