Forked from dbc-challenges/0.2.1-boggle_class_from_methods.rb
Last active
January 1, 2016 15:19
-
-
Save chrisdolendo/8163266 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 | |
attr_reader :board | |
#attr_reader allows the user to access data without writing it | |
def initialize(grid) | |
@board = grid | |
end | |
def create_word(*coords) | |
coords.map { |coord| @board[coord.first][coord.last]}.join("") | |
end | |
def get_row(row) | |
p @board[row] | |
end | |
def get_col(col) | |
col_length = @board.length | |
column = [] | |
start = 0 | |
until start == col_length do | |
column.push(@board[start][col]) | |
start += 1 | |
end | |
p column | |
end | |
#BONUS | |
#get_diag doesn't work and it should! | |
def get_diag(first_coord, last_coord) | |
row = first_coord[0] | |
column = first_coord[1] | |
endpoint = last_coord[1] | |
array = [] | |
until column == endpoint do | |
array.push(@board[row][column]) | |
row += 1 | |
column += 1 | |
end | |
p diagonal | |
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([1,2], [1,1], [2,1], [3,2]) == "dock" #=> should be true | |
p boggle_board.get_row(1) == ["i", "o", "d", "t"] #=> true | |
p boggle_board.get_col(1) == ["r", "o", "c", "a"] #=> true | |
#this one doesn't work and I don't know why not! | |
p boggle_board.get_diag([3,0],[0,3]) == ["t", "c", "d", "e"] #=> true | |
# create driver test code to retrieve a value at a coordinate here: | |
puts boggle_board.board[3][2] == "k" | |
# for get diagonal | |
# I would input the first coordinate of the start of the diag | |
# and the last coordinate of the end of the diag | |
# create an array, push in first letter using first coord | |
# increment each first and last of the first coordinate by 1 | |
# until it reaches the same value as the last coordinate | |
# return array | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment