Forked from dbc-challenges/0.2.1-boggle_class_from_methods.rb
Last active
August 29, 2015 13:56
-
-
Save sdickey/9175721 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 :board | |
def initialize(board) | |
@board = board | |
end | |
def create_word(*coords) | |
coords.map { |coord| board[coord.first][coord.last] }.join("") | |
end | |
def get_a_letter(coords) | |
board[coords[0]][coords[1]] | |
end | |
def get_row(row) | |
board[row] | |
end | |
def get_col(col) | |
board.transpose[col] | |
end | |
def get_diagonal(start_coords, direction_coords) | |
case | |
# diagonals for the top to corners of the grid | |
when start_coords == [0,0], start_coords == [(board.length - 1),(board.length - 1)] | |
row = 0 | |
column = 0 | |
diagonal = [] | |
while column < board.length | |
diagonal << board[row][column] | |
row += 1 | |
column += 1 | |
end | |
diagonal | |
# diagonals for the bottom two corners of the grid | |
when start_coords == [0,(board.length - 1)], start_coords == [(board.length - 1),0] | |
row = board.length - 1 | |
column = 0 | |
diagonal = [] | |
while column < board.length | |
diagonal << board[row][column] | |
row -= 1 | |
column += 1 | |
end | |
diagonal | |
# diagonals for the inside letters in the first row | |
when start_coords[0] == 0 && start_coords[1] > 0 && start_coords[1] < board.length - 1 | |
row = start_coords[0] | |
column = start_coords[1] | |
diagonal = [] | |
if direction_coords[1] > start_coords[1] | |
while column < board.length | |
diagonal << board[row][column] | |
row += 1 | |
column += 1 | |
end | |
diagonal | |
elsif direction_coords[1] < start_coords[1] | |
while column >= 0 | |
diagonal << board[row][column] | |
row += 1 | |
column -= 1 | |
end | |
diagonal | |
end | |
# diagonals for the inside letters in the last row | |
when start_coords[0] == 3 && start_coords[1] > 0 && start_coords[1] < board.length - 1 | |
row = start_coords[0] | |
column = start_coords[1] | |
diagonal = [] | |
if direction_coords[1] > start_coords[1] | |
while column < board.length | |
diagonal << board[row][column] | |
row -= 1 | |
column += 1 | |
end | |
diagonal | |
elsif direction_coords[1] < start_coords[1] | |
while column >= 0 | |
diagonal << board[row][column] | |
row -= 1 | |
column -= 1 | |
end | |
diagonal | |
end | |
# diagonals of inside letters in first column | |
when start_coords[1] == 0 && start_coords[0] > 0 && start_coords[0] < board.length - 1 | |
row = start_coords[0] | |
column = start_coords[1] | |
diagonal = [] | |
if direction_coords[0] > start_coords[0] | |
while row < board.length | |
diagonal << board[row][column] | |
row += 1 | |
column += 1 | |
end | |
diagonal | |
elsif direction_coords[0] < start_coords[0] | |
while row >= 0 | |
diagonal << board[row][column] | |
row -= 1 | |
column += 1 | |
end | |
diagonal | |
end | |
# diagonals of inside letters in last column | |
when start_coords[1] == board.length - 1 && start_coords[0] > 0 && start_coords[0] < board.length - 1 | |
row = start_coords[0] | |
column = start_coords[1] | |
diagonal = [] | |
if direction_coords[0] > start_coords[0] | |
while row < board.length | |
diagonal << board[row][column] | |
row += 1 | |
column -= 1 | |
end | |
diagonal | |
elsif direction_coords[0] < start_coords[0] | |
while row >= 0 | |
diagonal << board[row][column] | |
row -= 1 | |
column -= 1 | |
end | |
diagonal | |
end | |
when start_coords[0] > 0 && start_coords[0] < board.length - 1 && start_coords[1] > 0 && start_coords[1] < board.length - 1 | |
if direction_coords[0] < start_coords[0] && direction_coords[1] < start_coords[1] || direction_coords[0] > start_coords[0] && direction_coords[1] > start_coords[1] | |
row = start_coords[0] | |
column = start_coords[1] | |
diagonal_above = [] | |
while row >= 0 | |
diagonal_above << board[row][column] | |
row -= 1 | |
column -= 1 | |
end | |
row = start_coords[0] | |
column = start_coords[1] | |
diagonal_below = [] | |
unless start_coords[] # lost coding mojo right here.... | |
end | |
end | |
end | |
dice_grid = [["b", "r", "a", "e"], | |
["i", "o", "d", "t"], | |
["e", "c", "l", "r"], | |
["t", "a", "k", "e"]] | |
# Objective 1 | |
# ------------------------------------------------ | |
# The boggle_board object "holds" the dice grid by | |
# assigning it to the "game" variable when the | |
# object is created. This eliminates the need to | |
# include the "board" argument in the method | |
# definitions, letting us refer to it in the | |
# method body via initialization and the attr_reader | |
# method. | |
game = BoggleBoard.new(dice_grid) | |
# Objective 2 | |
# ------------------------------------------------ | |
p game.create_word([1,2], [1,1], [2,1], [3,2]) #=> "dock" | |
p game.create_word([0,1], [1,1], [2,1], [3,2]) #=> "rock" | |
p game.create_word([3,0], [3,1], [2,2], [3,2], [3,3], [2,3]) #=> "talker" | |
p game.get_row(0) == ["b", "r", "a", "e"] #=> true | |
p game.get_row(1) == ["i", "o", "d", "t"] #=> true | |
p game.get_row(2) == ["e", "c", "l", "r"] #=> true | |
p game.get_row(3) == ["t", "a", "k", "e"] #=> true | |
p game.get_col(0) == ["b", "i", "e", "t"] #=> true | |
p game.get_col(1) == ["r", "o", "c", "a"] #=> true | |
p game.get_col(2) == ["a", "d", "l", "k"] #=> true | |
p game.get_col(3) == ["e", "t", "r", "e"] #=> true | |
# # All rows as strings | |
p game.get_row(0).join("") == "brae" #=> true | |
p game.get_row(1).join("") == "iodt" #=> true | |
p game.get_row(2).join("") == "eclr" #=> true | |
p game.get_row(3).join("") == "take" #=> true | |
# # All columns as strings | |
p game.get_col(0).join("") == "biet" #=> true | |
p game.get_col(1).join("") == "roca" #=> true | |
p game.get_col(2).join("") == "adlk" #=> true | |
p game.get_col(3).join("") == "etre" #=> true | |
# Total output for all rows and columns as strings | |
# "brae" | |
# "iodt" | |
# "eclr" | |
# "take" -> real English word | |
# "biet" | |
# "roca" | |
# "adlk" | |
# "etre" -> real French word | |
# Objective 3 | |
# ------------------------------------------------ | |
p game.get_a_letter([3,2]) == "k" #=> true | |
p game.get_a_letter([0,1]) == "r" #=> true | |
p game.get_a_letter([2,1]) == "c" #=> true | |
# Objective 4 | |
# ------------------------------------------------ | |
# My #get_diagonal method starts on line 27 above. | |
# You can't miss it: it's big and gnarly. In fact, | |
# if it somehow escapes the confines of my computer, | |
# I'm certain it will try to take over San Jose. And | |
# then, of course, it will invade San Francisco. | |
# dice_grid = [["b", "r", "a", "e"], | |
# ["i", "o", "d", "t"], | |
# ["e", "c", "l", "r"], | |
# ["t", "a", "k", "e"]] | |
# Driver Code | |
# tests for the four corners and their corresponding | |
# diagonals | |
p game.get_diagonal([0,0], [1,1]) #=> ["b", "o", "l", "e"] | |
p game.get_diagonal([3,3], [1,1]) #=> ["b", "o", "l", "e"] | |
p game.get_diagonal([3,0], [2,2]) #=> ["t", "c", "d", "e"] | |
p game.get_diagonal([0,3], [2,2]) #=> ["t", "c", "d", "e"] | |
# tests for the inside letters on the first row | |
p game.get_diagonal([0,1], [1,0]) #=> ["r", "i"] | |
p game.get_diagonal([0,1], [1,3]) #=> ["r", "d", "r"] | |
p game.get_diagonal([0,2], [2,0]) #=> ["a", "o", "e"] | |
p game.get_diagonal([0,2], [1,3]) #=> ["a", "t"] | |
# tests for the inside letters on the last row | |
p game.get_diagonal([3,1], [2,0]) #=> ["a", "e"] | |
p game.get_diagonal([3,1], [2,2]) #=> ["a", "l", "t"] | |
p game.get_diagonal([3,2], [2,3]) #=> ["k", "r"] | |
p game.get_diagonal([3,2], [2,1]) #=> ["k", "c", "i"] | |
# tests for the inside letters in the first column | |
p game.get_diagonal([1,0], [0,1]) #=> ["i", "r"] | |
p game.get_diagonal([1,0], [2,1]) #=> ["i", "c", "k"] | |
p game.get_diagonal([2,0], [1,1]) #=> ["e", "o", "a"] | |
p game.get_diagonal([2,0], [3,1]) #=> ["e", "a"] | |
# tests for inside letters in last column | |
p game.get_diagonal([1,3], [0,2]) #=> ["t", "a"] | |
p game.get_diagonal([1,3], [2,2]) #=> ["t", "l", "a"] | |
p game.get_diagonal([2,3], [1,2]) #=> ["r", "d", "r"] | |
p game.get_diagonal([2,3], [3,2]) #=> ["r", "k"] | |
# tests for inside letters of the grid | |
# I put in quite some time and couldn't get this thing | |
# going. Looking at the monstrosity that it is (BoggleZilla?) | |
# I'm certain there's a more elegant, simple solution. But | |
# I don't have the programming chops to figure it out. I suppose | |
# I could just hard code the inner positions in my conditional logic, | |
# but then I'd be limited to a 4 X 4 grid, and that's not flexible. | |
# I'm calling this one: BoggleZilla - 1, Sean - nil. | |
# Objective 5 | |
# ------------------------------------------------ | |
# I know that procedural programming vs. object oriented programming | |
# is an important distinction to understand. The biggest difference | |
# that I can see is the way that the dice_grid is "held" | |
# by the BoggleBoard object, as opposed to referring to it every time | |
# a method was called in the nested array challenge. Creating an object | |
# with certain attributes and then manipulating those attributes (reading | |
# them or setting them to new values) seems to model physical reality | |
# better than a procedural approach: most "things" that we interact with | |
# are objects and they have attributes that allow us to take action, make | |
# decisions, etc. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment