Forked from dbc-challenges/0.2.1-boggle_class_from_methods.rb
Last active
August 29, 2015 13:56
-
-
Save kevinlee702/9110160 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
class BoggleBoard | |
def initialize(dice_grid) | |
@dice_grid = dice_grid | |
end | |
def create_word(*coords) | |
coords.map {|coord| @dice_grid[coord.first][coord.last]}.join("") | |
end | |
def get_row(row) | |
print @dice_grid[row] | |
end | |
def get_col(col) | |
column_array =[] | |
0.upto(3) {|row| column_array << @dice_grid[row][col]} | |
print column_array | |
end | |
def get_diagonal(coord1, coord2) | |
diagonal_array = [] | |
row_check = coord1.first | |
col_check = coord1.last | |
# Minus the coordinate values from each other and by taking the absolute value will disregard if | |
# its negative, if they are equal its a valid diagonal. So line 29 checks if | |
# they are not equal its not a diagonal. | |
if (coord1.first - coord2.first).abs != (coord1.last - coord2.last).abs | |
puts "This is not a diagonal" | |
else | |
if coord1.first < coord2.first # checking if the first row is less than the 2nd | |
until row_check > coord2.first # checks if row is greater than the second assuming going from left to right will go diagonally down. | |
diagonal_array << @dice_grid[row_check][col_check] | |
row_check += 1 #If add one to row and column goes diagonally down from left to right. | |
col_check += 1 | |
end | |
else | |
until row_check < coord2.first # checks the first row if less than second row we will go diagonally up. | |
diagonal_array << @dice_grid[row_check][col_check] # add letter to array. | |
row_check -= 1 # minus one to the row to go diagonally up | |
col_check += 1 # plus one to column assuming input from left to right. | |
end | |
end | |
print diagonal_array # prints letters on one line. | |
end | |
end | |
end | |
# Instantiate a new board object. The boggle_board object holds the dice_grid | |
# as an instance variable in the initialize method. | |
dice_grid = [["b","r","a","e"], | |
["i","o","d","t"], | |
["e","c","l","r"], | |
["t","a","k","e"]] | |
# [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] | |
boggle_board = BoggleBoard.new(dice_grid) | |
# test for create_word | |
puts boggle_board.create_word([1,2],[1,1],[2,1],[3,2]) #=> returns dock | |
puts boggle_board.create_word([0,1],[0,2],[1,2]) #=> returns "rad" | |
puts boggle_board.create_word([0,0],[1,1],[0,2],[1,3]) #=> returns "boat" | |
puts boggle_board.create_word([2,1],[3,1],[3,2],[3,3]) #=> returns "cake" | |
# test for row | |
puts boggle_board.get_row(0) #=> ["b","r","a","e"] | |
puts boggle_board.get_row(1) #=> ["i","o","d","t"] | |
puts boggle_board.get_row(2) #=> ["e","c","l","r"] | |
puts boggle_board.get_row(3) #=> ["t","a","k","e"] | |
# test for col | |
puts boggle_board.get_col(0) #=> ["b","i","e","t"] | |
puts boggle_board.get_col(1) #=> ["r","o","c","a"] | |
puts boggle_board.get_col(2) #=> ["a","d","l","k"] | |
puts boggle_board.get_col(3) #=> ["e","t","r","e"] | |
# take is a word. | |
puts boggle_board.create_word([3,2]) #=> "k" | |
# create driver test code to retrieve a value at a coordinate here: | |
puts boggle_board.create_word([1,2]) #=> "d" | |
# test for diagonals | |
puts boggle_board.get_diagonal([0,0],[3,3]) #=> returns "bole" | |
puts boggle_board.get_diagonal([3,0],[1,2]) #=> returns "tcd" | |
puts boggle_board.get_diagonal([2,0],[2,2]) #=> not a diagonal | |
puts boggle_board.get_diagonal([3,1],[1,3]) #=> returns "alt" | |
# Review and Reflect | |
# The implementation is different in you are accessing the boggle_board as an object. | |
# The benefit is you can easily change to a new board and provides more flexiblity | |
# when changing boards and even size of the boards. Also when designing similar models can | |
# inherit characteristics from a class. Basically don't have to repeat yourself and the code. | |
# The diagonal method was definitely the most challenging part. But by looking at the | |
# coordinates and how they relate one can see a pattern forming. If you subtract the | |
# coordinates from each other and disregard if its negative, then if they are equal its a diagonal. | |
# A lot of trial and error and looking at errors. I don't want to look at it any more tonight as | |
# there comes a point when your mind isn't processing effectively anymore. Will look later and see other attempts. | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment