Forked from dbc-challenges/0.2.1-boggle_class_from_methods.rb
Last active
January 1, 2016 03:09
-
-
Save katherineimogene/8084102 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 | |
# 1. Initialize | |
def initialize(dice_grid) | |
@dice_grid = dice_grid | |
end | |
# 2. create_word | |
def create_word(*coords) | |
coords.map { |coord| @dice_grid[coord.first][coord.last]}.join("") | |
end | |
# 3. get_row | |
def get_row(row) | |
@dice_grid[row] | |
end | |
# 4. get_col | |
def get_col(col) | |
col_array = [] | |
@dice_grid.each { |row| col_array << row.at(col) } | |
return col_array | |
end | |
def all_rows_cols | |
row_count = 0 | |
until row_count == @dice_grid.length | |
p get_row(row_count).join("") | |
row_count += 1 | |
end | |
col_count = 0 | |
until col_count == @dice_grid[0].length | |
p get_col(col_count).join("") | |
col_count += 1 | |
end | |
end | |
# 6. #get_diagonal method. Just like the #get_col or #get_row method, the #get_diagonal method should | |
# return an array of values, but it will need 2 coordinates entered to define the diagonal. | |
def get_diagonal(coord1,coord2) | |
diagonal = [] | |
row_difference = coord1.first-coord2.first | |
col_difference = coord1.last-coord2.last | |
col = coord1.last | |
row = coord1.first | |
diagonal_length = row_difference.abs + 1 | |
if(row_difference).abs==(col_difference).abs | |
if row_difference < 0 && col_difference < 0 | |
col_incrementer = 1 | |
row_incrementer = 1 | |
elsif row_difference < 0 && col_difference > 0 | |
col_incrementer = -1 | |
row_incrementer = 1 | |
elsif row_difference > 0 && col_difference < 0 | |
col_incrementer = 1 | |
row_incrementer = -1 | |
else | |
col_incrementer = -1 | |
row_incrementer = -1 | |
end | |
diagonal_length.times { diagonal.push(@dice_grid[row][col]) | |
col += col_incrementer | |
row += row_incrementer | |
} | |
else | |
raise ArgumentError.new("those coordinates aren't on a diagonal!")#raise argument error | |
end | |
return 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: | |
puts boggle_board.create_word([0,0], [0,1], [1,0], [2,1], [3,2]) == "brick" #=> true | |
puts boggle_board.create_word([3,0], [3,1], [3,2], [3,3], [2,3]) == "taker" #=> true | |
puts boggle_board.create_word([1,2],[1,1],[2,1],[3,2]) == "dock" #= true | |
puts boggle_board.get_row(2) == ["e","c","l","r"] #=> true | |
puts boggle_board.get_col(1) == ["r","o","c","a"] #=> true | |
# Print out all the rows and columns of the board as strings | |
boggle_board.all_rows_cols | |
=begin boggle_board.all_rows_cols output: | |
"brae" ====> real word: steep bank or hillside. I had to look it up. | |
"iodt" | |
"eclr" | |
"take" ====> real word! | |
"biet" | |
"roca" ====> real name of a delicious candy | |
"adlk" | |
"etre" ====> real French word | |
=end | |
# create driver test code to retrieve a value at a coordinate here: | |
puts boggle_board.create_word([0,0]) == "b" #=> true | |
puts boggle_board.create_word([3,2]) == "k" #=> true | |
# Driver code for get_diagonal | |
p boggle_board.get_diagonal([0,0],[3,3]) == ["b", "o", "l", "e"] #=> true | |
p boggle_board.get_diagonal([1,1],[3,3]) == ["o", "l", "e"] #=> true | |
p boggle_board.get_diagonal([2,2],[1,1]) == ["l", "o"] #=> true | |
p boggle_board.get_diagonal([0,1],[0,2]) #=> argument error! | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment