Forked from dbc-challenges/0.2.1-boggle_class_from_methods.rb
Last active
December 27, 2015 23:39
-
-
Save ballauriena/7407639 to your computer and use it in GitHub Desktop.
phase 0 unit 2 week 1
boggle class challenge
This file contains 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 | |
def initialize (board) | |
@board = board | |
end | |
def find(x, y) | |
@board[x][y] | |
end | |
def get_row(row) | |
@board[row-1] | |
end | |
def get_col(col) | |
@board.transpose[col-1] | |
end | |
def get_diagonal(a, b) | |
if (a.first - b.first).abs != (a.last - b.last).abs | |
return ArgumentError,('These coordinates do not produce a valid diagonal') #return an error if you can't make a diagonal | |
else | |
diag = [] | |
x = a[0] | |
y = a[1] | |
diag_length = (a.first - b.first).abs + 1 | |
until diag.length == diag_length | |
diag.push(@board[x][y]) | |
if a.first > b.first && a.last > b.last | |
x -= 1 | |
y -= 1 | |
elsif a.first < b.first && a.last < b.last | |
x += 1 | |
y += 1 | |
elsif a.first < b.first && a.last > b.last | |
x += 1 | |
y -= 1 | |
else | |
x -= 1 | |
y += 1 | |
end | |
end | |
end | |
diag | |
end | |
def create_word(*coords) | |
coords.map { |coord| @board[coord.first][coord.last]}.join("") | |
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: | |
puts boggle_board.create_word([1,2], [1,1], [2,1], [3,2]) #=> dock | |
puts boggle_board.get_row(2) #=> i, o, d, t | |
puts boggle_board.get_row(1) #=> b, r, a, e | |
puts boggle_board.get_row(4) #=> t, a, k, e | |
puts boggle_board.get_row(3) #=> e, c, l, r | |
puts boggle_board.get_col(1) #=> b, i, e, t | |
puts boggle_board.get_col(3) #=> a, d, l, k | |
puts boggle_board.get_col(2) #=> r, o, c, a | |
puts boggle_board.get_col(4) #=> e, t, r, e | |
# create driver test code to retrieve a value at a coordinate here: | |
puts boggle_board.retrieve(3,2) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment