Forked from dbc-challenges/0.2.1-boggle_class_from_methods.rb
Last active
August 29, 2015 13:55
-
-
Save cyancey/8726574 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(board) | |
@board = board | |
end | |
attr_accessor :board | |
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 print_all_rows | |
row = 0 | |
while row < @board.length do | |
puts self.get_row(row).join("") | |
row +=1 | |
end | |
end | |
def print_all_cols | |
col = 0 | |
while col < @board.length do | |
puts self.get_col(col).join("") | |
col +=1 | |
end | |
end | |
def is_diag?(coord1, coord2) #returns true if coordinates are diagonal to one another | |
(top_left_diag(coord1) == top_left_diag(coord2)) || (top_right_diag(coord1) == top_right_diag(coord2)) | |
end | |
def top_left_diag(coord) #returns the most upper left diagonal coordinate of the coord passed in | |
min_row = coord.first | |
min_col = coord.last | |
while min_row > 0 && min_col > 0 do | |
min_row -= 1 | |
min_col -= 1 | |
end | |
[min_row, min_col] | |
end | |
def top_right_diag(coord) #returns the most upper right diagonal coordinate of the coord passed in | |
min_row = coord.first | |
min_col = coord.last | |
while min_row > 0 && min_col < @board[0].length-1 do | |
min_row -= 1 | |
min_col += 1 | |
end | |
[min_row, min_col] | |
end | |
def get_diagonal(coord1, coord2) #returns array of all objects on the same diagonal line as the coords passed in | |
raise ArgumentError.new("Coordinates are not diagonals of one another") unless is_diag?(coord1, coord2) | |
coord1_row = coord1.first | |
coord1_column = coord1.last | |
coord2_row = coord2.first | |
coord2_column = coord2.last | |
if (coord1_column < coord2_column && coord1_row < coord2_row) || (coord2_column < coord1_column && coord2_row < coord1_row) #returns true if coords slope diagoanlly from top left to bottom right | |
diag_array = [] | |
diag_element = top_left_diag(coord1) | |
while diag_element.first <= @board.length-1 && diag_element.last <= @board[0].length-1 do | |
diag_array << @board[diag_element.first][diag_element.last] | |
diag_element = [diag_element.first + 1, diag_element.last + 1] | |
end | |
diag_array | |
else | |
diag_array = [] | |
diag_element = top_right_diag(coord1) | |
while diag_element.first <= @board.length-1 && diag_element.last >= 0 do | |
diag_array << @board[diag_element.first][diag_element.last] | |
diag_element = [diag_element.first + 1, diag_element.last - 1] | |
end | |
diag_array.reverse | |
end | |
end | |
end | |
dice_grid = [["b", "r", "a", "e"], | |
["i", "o", "d", "t"], | |
["e", "c", "l", "r"], | |
["t", "a", "k", "e"]] | |
#ARRAY LOCATIONS | |
# [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) | |
boggle_board.print_all_rows | |
boggle_board.print_all_cols | |
puts "----" | |
#DRIVER CODE | |
p boggle_board.create_word([1,2], [1,1], [2,1], [3,2]) == "dock" | |
p boggle_board.get_row(2) == ["e", "c", "l", "r"] | |
p boggle_board.get_col(2) == ["a", "d", "l", "k"] | |
p boggle_board.board[3][2] == "k" | |
p boggle_board.get_diagonal([3,2], [2,1]) == ["i", "c", "k"] | |
p boggle_board.get_diagonal([1,1], [2,0]) == ["e", "o", "a"] | |
#All rows and cols output | |
# brae | |
# iodt | |
# eclr | |
# take | |
# biet | |
# roca | |
# adlk | |
# etre | |
#REFLECTION | |
=begin | |
You just made a transition from procedural programming to object-oriented programming! | |
How is the implementation different? | |
What are the benefits to using the Object Oriented approach (even if it is a bit more code?) | |
The implementation of object-oriented programming is different than procedural programming | |
in that you work to determine what types of activities or manipulation (i.e. methods) should be | |
available for application to a certain type of data or object (in the case of this challenge, a nested | |
array that is acting as a Boggle Board). By defining the BoggleBoard class and incorporating a logical | |
set of methods which are useful for Boggle Boards (and nested arrays) the program is now more flexible, | |
understandable, and functional for users who want to manipulate a Boggle Board nested array. | |
The object oriented approach to coding enables a programmer to create sets of logical data maniuplations | |
that are applicable to certain types of objects. When building a program that utilizes a number of | |
different object types, using the object oriented approach makes the program much more easier to understand | |
and user friendly. | |
=end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment