Forked from dbc-challenges/0.2.1-boggle_class_from_methods.rb
Last active
August 29, 2015 13:56
-
-
Save msabdeljalil/8798062 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_accessor :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) | |
# baord.transpose[col] | |
# @board.map {|row| row.slice(col).join('')} | |
accumulator = [] | |
@board.each {|r| accumulator << r[col]} | |
accumulator | |
end | |
def get_diagonal (corner1, corner2) | |
startx = corner1.first | |
starty = corner1.last | |
endx = corner2.first | |
endy = corner2.last | |
accumulator = [] | |
if startx < endx && starty < endy | |
while startx <= endx | |
accumulator << @board[startx][starty] | |
startx += 1 | |
starty += 1 | |
end | |
elsif startx > endx && starty > endy | |
while startx >= endx | |
accumulator << @board[startx][starty] | |
startx -= 1 | |
starty -= 1 | |
end | |
elsif startx > endx && starty < endy | |
while startx >= endx | |
accumulator << @board[startx][starty] | |
startx -= 1 | |
starty += 1 | |
end | |
elsif startx < endx && starty > endy | |
while startx <= endx | |
accumulator << @board[startx][starty] | |
startx += 1 | |
starty -= 1 | |
end | |
else | |
puts "Your inputs are incorrect" | |
end | |
accumulator | |
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.get_diagonal([3,0], [0,3]) == ["t", "c", "d", "e"] | |
p boggle_board.create_word([0,0], [0,1], [0,2], [1,2]) == "brad" | |
p boggle_board.create_word([3,0], [2,0], [3,1]) == "tea" | |
p boggle_board.get_col(2) == ["a", "d", "l", "k"] | |
p boggle_board.get_col(0) == ["b", "i", "e", "t"] | |
p boggle_board.get_row(3) == ["t", "a", "k", "e"] | |
p boggle_board.get_row(1) == ["i", "o", "d", "t"] | |
p boggle_board.get_diagonal([0,0], [3,3]) == ["b", "o", "l", "e"] | |
p boggle_board.get_diagonal([0,3], [3,0]) == ["e", "d", "c", "t"] | |
boggle_board.board.each {|i| p i.join('')} #=> "brae", "iodt", "eclr", "take" # Problem 2A | |
for i in 0..boggle_board.board[0].length-1 #Problem 2B | |
p boggle_board.get_col(i).join('') | |
end | |
p boggle_board.create_word([3,2]) #=> "k" # Problem 3 | |
# create driver test code to retrieve a value at a coordinate here: | |
boggle_board.create_word([3,2]) #=> 'k' | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment