Forked from dbc-challenges/0.2.1-boggle_class_from_methods.rb
Last active
August 29, 2015 13:55
-
-
Save nickmeehan/8769961 to your computer and use it in GitHub Desktop.
phase 0 unit 2 week 1boggle 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(dice_grid) | |
@board = dice_grid | |
end | |
def create_word(*coords) | |
coords.map { |coord| @board[coord.first][coord.last] }.join("") | |
end | |
def get_row(row) | |
@board.at(row).join("") | |
end | |
def get_col(col) | |
@board.transpose[col].join("") | |
end | |
def get_diagonal(starting_coord, ending_coord) | |
diagonal_output = [] | |
if starting_coord == [0,0] && ending_coord == [3,3] | |
start = 0 | |
@board.each do |i| | |
diagonal_output << i.at(start) | |
start += 1 | |
end | |
return diagonal_output | |
elsif starting_coord == [3,3] && ending_coord == [0,0] | |
get_diagonal([0,0], [3,3]).reverse | |
elsif starting_coord == [0,3] && ending_coord == [3,0] | |
start = -1 | |
@board.each do |i| | |
diagonal_output << i.at(start) | |
start -= 1 | |
end | |
return diagonal_output | |
elsif starting_coord == [3,0] && ending_coord == [0,3] | |
get_diagonal([0,3], [3,0]).reverse | |
else | |
"Please input two valid diagonals." | |
end | |
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_row(0) == "brae" #=> true | |
p boggle_board.get_row(1) == "iodt" #=> true | |
p boggle_board.get_row(2) == "eclr" #=> true | |
p boggle_board.get_row(3) == "take" #=> true | |
puts | |
p boggle_board.get_col(0) == "biet" #=> true | |
p boggle_board.get_col(1) == "roca" #=> true | |
p boggle_board.get_col(2) == "adlk" #=> true | |
p boggle_board.get_col(3) == "etre" #=> true | |
puts | |
p boggle_board.get_diagonal([0,0], [3,3]) == ["b", "o", "l", "e"] #=> true | |
p boggle_board.get_diagonal([3,3], [0,0]) == ["e", "l", "o", "b"] #=> true | |
p boggle_board.get_diagonal([0,3], [3,0]) == ["e", "d", "c", "t"] #=> true | |
p boggle_board.get_diagonal([3,0], [0,3]) == ["t", "c", "d", "e"] #=> true | |
# create driver test code to retrieve a value at a coordinate here: | |
p boggle_board.create_word([3,2]) == "k" #=> true | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
My output for getting columns and rows:
"brae"
"iodt"
"eclr"
"take"
"biet"
"roca"
"adlk"
"etre"