Forked from dbc-challenges/0.2.1-boggle_class_from_methods.rb
Last active
January 4, 2016 04:29
-
-
Save mcivorsteiner/8568608 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_row(row) | |
@board[row] | |
end | |
def get_col(col) | |
col_arr = [] | |
@board.each {|row| col_arr << row[col]} | |
col_arr | |
end | |
def get_diagonal(coord_1, coord_2) | |
slope = ((coord_1[0]-coord_2[0]).to_f/(coord_1[1] - coord_2[1])) | |
# returning ArgumentError if slope of coordinates not equal to 1 or -1 (not diagonal) | |
return ArgumentError.new("coordinates not diagonal") if slope.abs != 1 | |
row_qty = @board.length | |
col_qty = @board[0].length | |
curr_row = coord_1[0] | |
curr_col = coord_1[1] | |
# running loop to determine left-most coordinate in diagonal | |
until curr_col == 0 || ((curr_row == row_qty-1) && (slope == -1)) || ((curr_row == 0) && (slope == 1)) | |
curr_row -= slope | |
curr_col -= 1 | |
end | |
diag_arr = [] | |
# starting with left-most coordinate in the diagonal, pushing all element in diagonal to diag_arr | |
while (curr_row >= 0) && (curr_row < row_qty) && (curr_col < col_qty) | |
diag_arr << @board[curr_row][curr_col] | |
curr_row += slope | |
curr_col += 1 | |
end | |
diag_arr | |
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: | |
p boggle_board.create_word([0,1], [1,1], [2,1], [3,2]) == "rock" | |
p boggle_board.create_word([2,1], [1,1], [2,2], [1,2]) == "cold" | |
p boggle_board.create_word([1,2], [1,1], [2,1], [3,2]) == "dock" | |
p boggle_board.get_row(1) == ["i", "o", "d", "t"] | |
p boggle_board.get_row(0) == ["b", "r", "a", "e"] | |
p boggle_board.get_row(3) == ["t", "a", "k", "e"] | |
p boggle_board.get_col(1) == ["r", "o", "c", "a"] | |
p boggle_board.get_col(0) == ["b", "i", "e", "t"] | |
p boggle_board.get_col(3) == ["e", "t", "r", "e"] | |
# calls of #get_diagonal method | |
p boggle_board.get_diagonal([3,0], [0,2]).class == ArgumentError | |
p boggle_board.get_diagonal([2,1], [1,2]) == ["t", "c", "d", "e"] | |
p boggle_board.get_diagonal([2,0], [1,1]) == ["e", "o", "a"] | |
p boggle_board.get_diagonal([3,2], [2,3]) == ["k", "r"] | |
p boggle_board.get_diagonal([2,3], [1,2]) == ["r", "d", "r"] | |
p boggle_board.get_diagonal([3,3], [0,0]) == ["b", "o", "l", "e"] | |
# create driver test code to retrieve a value at a coordinate here: | |
p boggle_board.board[3][2] == "k" | |
# print all rows as strings: | |
(0..3).to_a.each { |n| p boggle_board.get_row(n).join } #=> prints "brae" "iodt" "eclr" "take" | |
# print all columns as strings: | |
(0..3).to_a.each { |n| p boggle_board.get_col(n).join } #=> prints "biet" "roca" "adlk" "etre" | |
# REFLECTION | |
# This challenge was pretty simple until I started the #get_diagonal method. I understood the | |
# question as requiring the method to return an array of the elements in the diagonal from left to | |
# right. I had issues with the boolean statements in the until and while loops. I did learn a lot | |
# from the process of finding the bugs. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment