Forked from dbc-challenges/0.2.1-boggle_class_from_methods.rb
Last active
January 2, 2016 11:39
-
-
Save arcin/8298164 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
class BoggleBoard | |
attr_reader :board | |
def initialize(board) | |
@board = board | |
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) | |
col_items = [] | |
@board.each { |row| col_items << row[col] } | |
col_items | |
end | |
def get_diagonal(row, col) | |
left = get_col(col.pred) | |
right = get_col(col.next) | |
case 0 | |
when row | |
col == 0 ? [right[row.next]] : [right[row.next],left[row.next]].compact | |
when col | |
[right[row.pred], right[row.next]].compact | |
else | |
[left[row.pred], left[row.next], right[row.pred], right[row.next]].compact | |
end | |
end | |
end | |
# 1) Instantiate a new board object | |
# How does the boggle_board object hold the dice_grid? | |
# During initialization, dice_grid is passed to the BoggleBoard instance | |
# as a parameter. I used a instance variable to capture this attribute. | |
dice_grid =[["b", "r", "a", "e"], | |
["i", "o", "d", "t"], | |
["e", "c", "l", "r"], | |
["t", "a", "k", "e"]] | |
boggle_board = BoggleBoard.new(dice_grid) | |
# 2) Implement your methods | |
p boggle_board.create_word([1,2], [1,1], [2,1], [3,2]) # => "dock" | |
p boggle_board.get_col(0) #=> ["b", "i", "e", "t"] | |
p boggle_board.get_row(3) #=> ["t", "a", "k", "e"] | |
# All rows printed as strings | |
4.times {|row| p boggle_board.get_row(row).to_s} | |
# All columns printed as strings | |
4.times {|col| p boggle_board.get_col(col).to_s} | |
# Real Words | |
# - brae | |
# - take | |
# Total: 2 points | |
# 3) Access a coordinate | |
p boggle_board.create_word([3,2]) == "k" | |
p boggle_board.create_word([0,3]) == "e" | |
p boggle_board.create_word([1,1]) == "o" | |
# 4) Bonus: Create a #get_diagonal method | |
p boggle_board.get_diagonal(0,0) == ["o"] | |
p boggle_board.get_diagonal(3,1) == ["e", "l"] | |
p boggle_board.get_diagonal(2,1) == ["i", "t", "d", "k"] | |
# 5) Review and Reflect | |
# The change was fun! I accidentally made the change a little early (during the previous challenge) | |
# but I think it was because of the apparent benefits of Object Oriented programming. | |
# The implementation is different because it structures the code differently. | |
# Instead of creating a program that only flows from top to bottom, OOP | |
# Allows you to create a program that can interact with other object and | |
# respond to different situations. OOP allows flexability with the implementation | |
# of a program and allows for a more real world organization of, and communication | |
# between, objects. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment