Forked from dbc-challenges/0.2.1-boggle_class_from_methods.rb
Last active
December 27, 2015 15:39
-
-
Save kyeh/7349281 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 | |
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_idx) | |
@board.transpose[col_idx] | |
end | |
def get_diagonal(coord1, coord2) | |
if (coord1[0]-coord2[0]).abs != (coord1[1]-coord2[1]).abs | |
raise ArgumentError | |
end | |
r_increment = (coord2[0]-coord1[0])/((coord2[0]-coord1[0]).abs) | |
c_increment = (coord2[1]-coord1[1])/((coord2[1]-coord1[1]).abs) | |
r = coord1[0] | |
c = coord1[1] | |
final_arr = [] | |
until r == coord2[0] && c == coord2[1] do | |
final_arr<< @board[r][c] | |
r += (r_increment) | |
c += (c_increment) | |
end | |
final_arr<< @board[r][c] | |
final_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: | |
puts boggle_board.create_word([2,1], [1,1], [1,2], [0,3]) #=> returns "code" | |
puts boggle_board.create_word([0,1], [0,2], [1,2]) #=> creates what california slang word? | |
p boggle_board.get_row(2) #=> ["e", "c", "l", "r"] | |
#3 write a method that takes a column | |
p boggle_board.get_col( 1) #=> ["r", "o", "c", "a"] | |
# create driver test code to retrieve a value at a coordinate here: | |
# access k at row 3 col 2 | |
p boggle_board.create_word([3,2]) | |
# diagonal test | |
p boggle_board.get_diagonal([0,0],[3,3]) | |
p boggle_board.get_diagonal([2,3],[3,2]) | |
p boggle_board.get_diagonal([3,3],[0,0]) | |
p boggle_board.get_diagonal([2,0],[0,2]) | |
# Reflection | |
# Objects and classes are good! Also I was not able to find a shorthand for | |
# the ranges, as the increments would change depending on whether the endpoint | |
# was above/below/left/right from each other (For diagonal implementation) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment