Forked from dbc-challenges/0.2.1-boggle_class_from_methods.rb
Last active
August 29, 2015 13:55
-
-
Save gpavey/8718935 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 | |
attr_reader :dice_grid | |
def initialize(dice_grid) | |
@dice_grid = dice_grid | |
end | |
def create_word(*coords) | |
p coords.map {|coord| @dice_grid[coord.first][coord.last]}.join("") | |
end | |
def get_row(row) | |
@dice_grid[row] | |
end | |
def get_col(col) | |
@dice_grid.transpose[col] | |
end | |
def get_diagonal(coordinate1,coordinate2) | |
coord1a = coordinate1[0] | |
coord1b = coordinate1[1] | |
coord2a = coordinate2[0] | |
coord2b = coordinate2[1] | |
first = coord2a - coord1a | |
second = coord2b = coord1b | |
if (first == -1 && second == -1 || second == -1 || second == 0) || (first == 1 && second == -1 || second == 1) | |
puts "The coordinates #{first,second} are diagonal in the array" | |
else | |
puts "These coordinates in the array are not diagonal" | |
end | |
end | |
end | |
dice_grid = [["b", "r", "a", "e"], | |
["i", "o", "d", "t"], | |
["e", "c", "l", "r"], | |
["t", "a", "k", "e"]] | |
# Section 1 - Instantiate New Board Object | |
boggle_board = BoggleBoard.new(dice_grid) #dice_grid is held as a variable in the Initialze Method | |
# Section 2 - Implement your methods | |
# implement tests for each of the methods here: | |
boggle_board.create_word([1,2], [1,1], [2,1], [3,2]) #=> "dock" | |
p boggle_board.get_row(0) #=> ["b","r","a","e"] | |
p boggle_board.get_row(1) #=> ["i","o","d","t"] | |
p boggle_board.get_row(2) #=> ["e","c","l","r"] | |
p boggle_board.get_row(3) #=> ["t','a','k','e'] # real word "take" | |
p boggle_board.get_col(0) #=> ["b","i","e","t"] | |
p boggle_board.get_col(1) #=> ["r","o","c","a"] | |
p boggle_board.get_col(2) #=> ["a","d","l","k"] | |
p boggle_board.get_col(3) #=> ["e","t","r","e"] | |
# Section 3 - create driver test code to retrieve a value at a coordinate here: | |
p dice_grid[3][2] | |
# Section 4 - Bonus Question | |
p boggle_board.get_diagonal([1,3],[2,1]) # Not Diagonal | |
p boggle_board.get_diagonal([0,0],[1,1]) # Diagonal | |
# Section 5 - Review and Reflect | |
=begin | |
Definately have had to wrap my head around using Classes this week. Its one thing to write a bunch of methods and quite another | |
thing to create a class and use them in the class. Took me a bit but I think Im getting it. Slowly. | |
One of the benefits of using OOP is that it makes it easier to modify existing code. It' broken up into smaller chunks. It also | |
allows you to initialize single instances of objects. Pretty cool. In the beginning I couldnt see how we would be making | |
large programs but now I understand that its just one small step at a time. Makes things less daunting. | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment