Forked from dbc-challenges/0.2.1-boggle_class_from_methods.rb
Last active
August 29, 2015 13:55
-
-
Save Szarko/8772564 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(grid) | |
@grid = grid | |
end | |
def create_word(gridlayout, *coordinates) | |
coordinates.map { |coords| gridlayout[coords.first][coords.last]}.join("") | |
end | |
def get_row(rowtoreturn) | |
@grid[rowtoreturn] | |
end | |
def get_col(coltoreturn) | |
column = [] | |
@grid.each { |row| column << row[coltoreturn]} | |
column | |
end | |
def specfic_coord(coordinate) | |
@grid[coordinate.first][coordinate.last] | |
end | |
def generate_diagonal(coordinate) | |
test_diagonal = [[coordinate.first,coordinate.last]] | |
coord_x_base = test_diagonal.last.first | |
coord_y_base = test_diagonal.last.last | |
if (coord_x_base < (@grid.length / 2)) && (coord_y_base < (@grid.length / 2)) | |
while ((coord_x_base < (@grid.length-1)) && (coord_y_base < (@grid.length - 1))) | |
coord_x_base += 1 | |
coord_y_base += 1 | |
test_diagonal << [coord_x_base,coord_y_base] | |
end | |
elsif (coord_x_base < (@grid.length / 2)) && (coord_y_base >= (@grid.length / 2)) | |
while ((coord_x_base < (@grid.length-1)) && (coord_y_base > 0)) | |
coord_x_base += 1 | |
coord_y_base -= 1 | |
test_diagonal << [coord_x_base,coord_y_base] | |
end | |
elsif (coord_x_base >= (@grid.length / 2)) && (coord_y_base >= (@grid.length / 2)) | |
while ((coord_x_base > 0) && (coord_y_base > 0)) | |
coord_x_base -= 1 | |
coord_y_base -= 1 | |
test_diagonal << [coord_x_base,coord_y_base] | |
end | |
elsif (coord_x_base >= (@grid.length / 2)) && (coord_y_base < (@grid.length / 2)) | |
while ((coord_x_base > 0) && (coord_y_base < (@grid.length - 1))) | |
coord_x_base -= 1 | |
coord_y_base += 1 | |
test_diagonal << [coord_x_base,coord_y_base] | |
end | |
end | |
test_diagonal | |
end | |
def get_diagonal(coordinate1,coordinate2) | |
diagonal = [] | |
if (coordinate1.first != coordinate2.first) && ((coordinate1.first - coordinate2.first).abs > 1) | |
diagonal = generate_diagonal(coordinate1) | |
if diagonal.include? coordinate2 | |
diagonal.map { |x| @grid[x.first][x.last] }.join("") | |
else | |
puts "Invalid coordinates!" | |
end | |
else | |
puts "Invalid coordinates!" | |
end | |
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 boggle_board.create_word(dice_grid, [1,2],[1,1],[2,1],[3,2]) # => returns 'dock' | |
puts "All rows:" | |
puts boggle_board.get_row(0).join("") # => returns 'brae' | |
puts boggle_board.get_row(1).join("") # => returns 'iodt' | |
puts boggle_board.get_row(2).join("") # => returns 'eclr' | |
puts boggle_board.get_row(3).join("") # => returns 'take' | |
puts "All columns:" | |
puts boggle_board.get_col(0).join("") # => returns 'biet' | |
puts boggle_board.get_col(1).join("") # => returns 'roca' | |
puts boggle_board.get_col(2).join("") # => returns 'adlk' | |
puts boggle_board.get_col(3).join("") # => returns 'etre' | |
puts "Specific co-ordinate:" | |
puts boggle_board.specfic_coord([3,2]) # => returns 'k' | |
puts "Diagonal values:" | |
puts boggle_board.get_diagonal([0,0],[3,3]) # => returns 'bole' | |
puts boggle_board.get_diagonal([3,3],[0,0]) # => returns 'elob' | |
puts boggle_board.get_diagonal([3,0],[0,3]) # => returns 'tcde' | |
puts boggle_board.get_diagonal([0,3],[3,0]) # => returns 'edct' | |
puts boggle_board.get_diagonal([0,1],[2,3]) # => returns 'rdr' | |
puts boggle_board.get_diagonal([3,1],[1,3]) # => returns 'rdr' | |
puts boggle_board.get_diagonal([0,1],[0,2]) # => returns 'Invalid coordinates!' | |
puts "" | |
# create driver test code to retrieve a value at a coordinate here: | |
puts (boggle_board.specfic_coord([0,0])) == "b" && (boggle_board.specfic_coord([3,3])) == "e" # => returns true | |
# Reflection | |
# Implementing separate methods that do a specific function into a class is not that different from having the methods | |
# outside of the class. Essentially they function the same way and expect the same passed parameters. | |
# However, the benefits of using the Object Oriented approach is quite clear when compared with the previous challenge. | |
# With all the functioning methods placed into a OO Class, they can be called upon when a new object of that class is created. | |
# This allows us to create multiple grids with different values, and apply the same functioning methods to them without, | |
# having to alter our code in any way. | |
# I will continue to work on a better method to calculate the diagonal, I am not fully satisfied with the above method. | |
# Update: I worked on solving the diagonal method. And I managed to generate the diagonals between any 2 sets of coordinates | |
# that create a diagonal of at least 3 values. | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment