Forked from dbc-challenges/0.2.1-boggle_class_from_methods.rb
Last active
December 31, 2015 21:19
-
-
Save CalderWishne/8046201 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 Boggle | |
def initialize(board) | |
@board = board | |
@board_side_length = board[0].length | |
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(column) | |
col = [] | |
@board.each {|row| col << row[column]} | |
col | |
end | |
def get_diag(coords, slope) | |
slope = -slope # Since, to my way of thinking, the y-axis is inverted. | |
x = coords[1]; y = coords[0] | |
unless [1,-1].include?(slope) | |
raise ArgumentError.new("Slope must be +1 or - 1. This is a boogle board after all.") | |
end | |
until x == 0 # Finds leftmost intercept. | |
x -= 1; y -= slope | |
end | |
diag =[] | |
while x < @board_side_length # Builds diagonal starting with leftmost intercept. | |
if (0...@board_side_length).include?(y) | |
diag << board[y][x] | |
end | |
x += 1; y += slope | |
end | |
diag | |
end | |
end | |
boggle_board = [["b", "r", "a", "e"], | |
["i", "o", "d", "t"], | |
["e", "c", "l", "r"], | |
["t", "a", "k", "e"]] | |
game = Boggle.new(boggle_board) | |
# Driver Code: | |
p game.get_diag([0,0], 1) #=> ['b'] | |
p game.get_diag([0,0], -1) #=> ['b','o','l','e'] | |
p game.get_diag([0,2], 1) #=> ['e','o','a'] | |
p game.get_diag([0,2], -1) #=> ['a','t'] | |
p game.create_word([3,0], [3,1], [3,2], [3,3]) #=> prints "take" | |
p game.create_word([1,2], [1,1], [2,1], [3,2]) #=> prints "dock" | |
p game.get_row(2) #=> ["e","c","l","r"] | |
p game.get_col(3) #=> ['e','t','r','e'] | |
p game.get_col(1) #=> ['r','o','c','a'] | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment