Forked from dbc-challenges/0.2.1-boggle_class_from_methods.rb
Last active
January 2, 2016 21:49
-
-
Save jwrobes/8365747 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
=begin | |
To create a class for BoggleBoard I need to initialize the class | |
and set an instance variable for the board that is passed inside | |
and it should test that what is passed in is an array with has four columns | |
and four rows and raise an argument error if it does not | |
I could create a separate method just to test that | |
and run that test method in the initialize method of the BoggleBoard class | |
I can create a #create_word method by passing in the coordinates as an argument | |
and have those coordinates call on the instance variable holding the boggle board | |
Next create a method for get_row and get_col that take the row and the column as arguemtns | |
and then run those rows on the method on the instance variable | |
#Create DIAGOAL METHOD PSUEDOCODE | |
#to create the diagonal it must take two coordinates they must have special relationship | |
# that sets them apart as diagonals | |
# and I need to see what makes a diagonal, do do this I'm going to | |
# identify a few two coordinates that make diagonals and a few that don't | |
# to see if there is a difference I can test for | |
# diagonals: | |
# [0,0] has diagonals with the folloiwng: [1,1][2,2][3,3] | |
# => [0,0] has these as NOT diagonals [0,1][0,2][2,1][2,3][3,2][0,3] | |
# [0,1] has diagonals with the following [1,2][2,3] | |
# [2,0] has these as diagonals [3,1] and [1,1][0,3] | |
# if we represent the two coordinates as [y1,x1] [y2,x2] | |
# the two coordinates are diagonal if (y2-y1).absolute = (x2-x1).absolute | |
# Create a conditional that tests if that is true or false | |
# and raise argument error if it's NOT true | |
# the next step is to move from the y1,x1 to the y2,x2 coordinate to get each | |
# letter and to do this you either add or subtract 1 to y1,x1 until it equals y2,x2 | |
# If the second y2 is GREATER than y1 or if the Second x2 IS GREATER than x1 you ADD 1 | |
IF the second y2 is LESS than y1 or the second x2 is LESS than x1 you SUBTRACT 1 | |
# First create an array of the y coordinates | |
# Next create an array of the x coordinates | |
# Combine the two arrays into a multidimensional array, to get the coordinates of the diagonal | |
# Use coordinates to create array of values from board based on coordinates | |
=end | |
#FIRST ATTEMPT________________________________________________________________________ | |
class BoggleBoard | |
attr_reader :board | |
def initialize(grid) | |
is_boggle?(grid) ? @board = grid : (raise ArgumentError.new("This is not a boggle board")) | |
end | |
def is_boggle?(test_object) | |
if test_object.is_a?(Array) && (test_object.length == 4 && test_object.transpose.length == 4) | |
return true | |
else | |
return false | |
end | |
end | |
def get_coord(coord1,coord2) | |
@board[coord1][coord2] | |
end | |
def create_word(*coords) | |
coords.map { |coord| @board[coord.first][coord.last]}.join("") | |
end | |
def get_row(row) | |
@board[row] | |
end | |
def get_col(col) | |
@board.transpose[col] | |
end | |
def get_diagonal(coord1, coord2) | |
# create array for diagonal | |
diagonalx = [] | |
diagonaly = [] | |
diff_y = coord2.first - coord1.first | |
diff_x = coord2.last - coord1.last | |
#raise argumenterror if not diagonal | |
raise ArgumentError.new("These coordinates do not make a diagonal") unless (coord1.last - coord2.last).abs == (coord1.first - coord2.first).abs | |
# create the diagonaly array by checking if the second coordinate y coordiate is greater or less | |
# than the first y coordiante and if it's greater add 1 until it equals, and if less, subtract one | |
# and add each value to an array | |
if coord2.first > coord1.first | |
y = coord1.first | |
while y <= coord2.first #check coord1 les | |
diagonaly << y | |
y += 1 | |
end #=>while y < coord2.first end | |
else | |
y = coord1.first | |
while y >= coord2.first | |
diagonaly << y | |
y -= 1 | |
end #while y > coord2.first end | |
end #end for if coord2.first > coord1.first | |
if coord2.last > coord1.last | |
x = coord1.last | |
while x <= coord2.last | |
diagonalx << x | |
x += 1 | |
end # end for while y < coord2.last | |
else | |
y = coord1.firs | |
while x >= coord2.last | |
diagonalx << x | |
y -= 1 | |
end | |
end # end for if coord2.last > coord1.last | |
diagonal = diagonaly.zip(diagonalx) | |
return diagonal.map { |coord| @board[coord.first][coord.last]} | |
end | |
end | |
#REFACTORED CODE _________________________________________________________ | |
class BoggleBoard | |
attr_reader :board | |
def initialize(grid) | |
is_boggle?(grid) ? @board = grid : (raise ArgumentError.new("This is not a boggle board")) | |
end | |
def is_boggle?(test_object) | |
if test_object.is_a?(Array) && (test_object.length == 4 && test_object.transpose.length == 4) | |
return true | |
else | |
return false | |
end | |
end | |
def get_coord(coord1,coord2) | |
@board[coord1][coord2] | |
end | |
def create_word(*coords) | |
coords.map { |coord| @board[coord.first][coord.last]}.join("") | |
end | |
def get_row(row) | |
@board[row] | |
end | |
def get_col(col) | |
@board.transpose[col] | |
end | |
def is_diagonal?(coord1,coord2) | |
diff_first = (coord1.first - coord2.first).abs | |
diff_last = (coord1.last - coord2.last).abs | |
diff_first == diff_last ? true : false | |
end | |
def diagonal_direction?(coord1,coord2) | |
if coord1.first < coord2.first && coord1.last < coord2.last | |
return :SE | |
elsif coord1.first > coord2.first && coord1.last > coord2.last | |
return :NW | |
elsif coord1.first < coord2.first && coord1.last > coord2.last | |
return :SW | |
else | |
return :NE | |
end | |
end | |
def get_diagonal(coord1, coord2) | |
# coordintates [x,y] | |
# create x_first and y_first for coord1 & x_scnd and y_scnd for coord2 | |
x_first = coord1.first | |
y_first = coord1.last | |
x_scnd = coord2.first | |
y_scnd = coord2.last | |
# gets absolute difference between index in coord1 | |
diff_first_last = (coord1.last - coord1.first) | |
#raise argumenterror if not diagonal | |
raise ArgumentError.new("Coordinates can't make diagonal") unless is_diagonal?(coord1,coord2) | |
direction = diagonal_direction?(coord1,coord2) | |
p direction | |
case direction | |
when :SE | |
(x_first..x_scnd).map{|i| @board[i][i + diff_first_last]} | |
when :NW | |
(x_scnd..x_first).map{|i| @board[i][i + diff_first_last]}.reverse | |
when :SW | |
y_counter = y_first +1 | |
(x_first..x_scnd).map{|i| @board[i][y_counter-=1]} | |
when :NE | |
y_counter = y_scnd + 1 | |
(x_scnd..x_first).map{|i| @board[i][y_counter-=1]}.reverse | |
end | |
end | |
end | |
false_grid = [["b", "r", "a", "e","t"], | |
["i", "o", "d", "t", "f"], | |
["e", "c", "l", "r","q"], | |
["t", "a", "k", "e", "l"]] | |
dice_grid = [["b", "r", "a", "e"], | |
["i", "o", "d", "t"], | |
["e", "c", "l", "r"], | |
["t", "a", "k", "e"]] | |
boggle_board = BoggleBoard.new(dice_grid) | |
#boggle_board = BoggleBoard.new(false_grid) | |
puts boggle_board.create_word([1,2],[1,1],[2,1],[3,2]) | |
# implement tests for each of the methods here: | |
#boggle_board.get_col(1) | |
puts "Print all the rows: " | |
(0..3).each {|x| puts boggle_board.get_row(x).join('')} | |
puts "Print all the columns:" | |
(0..3).each {|x| puts boggle_board.get_col(x).join('')} | |
# create driver test code to retrieve a value at a coordinate here: | |
p boggle_board.board[3][2] # => ="k" | |
p boggle_board.board[2][3] # => "r" | |
p boggle_board.board[0][3] # => "e" | |
p boggle_board.get_coord(0,3) #=. "e" | |
puts "These coordinates are a diagonal? 0,0 and 3,3" | |
p boggle_board.is_diagonal?([0,0],[3,3]) #=> true | |
puts "These coordinates are a NOT diagonal? 0,1 and 3,3" | |
p boggle_board.is_diagonal?([0,1],[3,3]) #=> false | |
puts "This is a SE diagonal and it should be i c k" | |
p boggle_board.get_diagonal([1,0],[3,2]) #=> ["i","c","k"] | |
puts "This is a SE diagonal and it should be b o l e" | |
p boggle_board.get_diagonal([0,0],[3,3]) #=> ["b", "o","l","e"] | |
puts "This is a SE diagonal and it should be 'r' 'd' 'r'" | |
p boggle_board.get_diagonal([0,1],[2,3]) #=> ["r","d","r"] | |
puts "This is a NW diagonal and it should be 'e' 'l' 'o' 'b'" | |
p boggle_board.get_diagonal([3,3],[0,0]) #=> ["r","d","r"] | |
puts "This is a SW diagonal and it should be 'a' 'o' 'e'" | |
p boggle_board.get_diagonal([0,2],[2,0]) #=> ["a","o","e"] | |
puts "This is a SW diagonal and it should be 'e' 'd' 'c','t'" | |
p boggle_board.get_diagonal([0,3],[3,0]) #=> ["e","d","c","t"] | |
puts "This is a NE diagonal and it should be 'e' 'o' 'a'" | |
p boggle_board.get_diagonal([2,0],[0,2]) #=> ["e","o","a"] | |
puts "This is a NE diagonal and it should be 't' 'c' 'd','e'" | |
p boggle_board.get_diagonal([3,0],[0,3]) #=> ["t","c","d","e"] | |
puts "This is a NE diagonal and it should be 'a' 'l' 't'" | |
p boggle_board.get_diagonal([3,1],[1,3]) #=> ["a","l","t"] | |
#p boggle_board.get_diagonal([0,0],[0,3]) #=> Raise argument error | |
#Create DIAGONAL METHOD PSUEDOCODE | |
#to create the diagonal it must take two coordinates they must have special relationship | |
# that sets them apart as diagonals | |
# and I need to see what makes a diagonal, do do this I'm going to | |
# identify a few two coordinates that make diagonals and a few that don't | |
# to see if there is a difference I can test for | |
# diagonals: | |
# [0,0] has diagonals with the folloiwng: [1,1][2,2][3,3] | |
# => [0,0] has these as NOT diagonals [0,1][0,2][2,1][2,3][3,2][0,3] | |
# [0,1] has diagonals with the following [1,2][2,3] | |
# [2,0] has these as diagonals [3,1] and [1,1][0,3] | |
# if we represent the two coordinates as [y1,x1] [y2,x2] | |
# the two coordinates are diagonal if (y2-y1).absolute = (x2-x1).absolute | |
# Create a conditional that tests if that is true or false | |
# and raise argument error if it's NOT true | |
# the next step is to move from the y1,x1 to the y2,x2 coordinate to get each | |
# letter and to do this you either add or subtract 1 to y1,x1 until it equals y2,x2 | |
# If the second y2 is GREATER than y1 or if the Second x2 IS GREATER than x1 you ADD 1 | |
# IF the second y2 is LESS than y1 or the second x2 is LESS than x1 you SUBTRACT 1 | |
# First create an array of the y coordinates | |
# Next create an array of the x coordinates | |
# Combine the two arrays into a multidimensional array, to get the coordinates of the diagonal | |
# Use coordinates to create array of values from board based on coordinates | |
# DIAGONAL REFACTORING | |
# I looked to simplify my code for the diagonal | |
# Here are the steps | |
# 1. def diagonal that takes two coordinates | |
# 2. create variables for each of the indexes in the first and second coordinates | |
# 3. create a variable for the difference between the to indexes in the first coordinate | |
# 4. raise argument error if method testing if coordinates create diagonal is false | |
# 5. run method that returns direction of diagonal based on coordinates | |
# 6. Run different four different versions of the diagonal based on the direction | |
# of the diagonal returned from the method | |
# 7. Each of the methods uses the runs through through the first index of the coordinates and | |
# depending on the direction manipulates the second coordinate differently | |
# Instead of making four different versions use the reverse method to reverse one of them |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment