Forked from dbc-challenges/0.2.1-boggle_class_from_methods.rb
Last active
August 29, 2015 13:56
-
-
Save carpenitoThomas/9136395 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 | |
def initialize(dice_grid) | |
@dice_grid=dice_grid | |
end | |
def create_word(dice_grid, *coords) | |
new_word=[] | |
coords.each { |coord| new_word<< dice_grid[coord.first][coord.last]} | |
new_word.join("") | |
end | |
def get_row(row_number) | |
return @dice_grid[row_number] | |
end | |
def get_col(col_number) | |
letters_in_column=[] | |
@dice_grid.each {|x| letters_in_column<< x[col_number]} | |
letters_in_column | |
end | |
def get_diagonal(diag_coord_one, diag_coord_two) | |
if (diag_coord_one==[0,0] && diag_coord_two==[3,3]) or (diag_coord_two==[0,0] && diag_coord_one==[3,3]) or | |
(diag_coord_one==[0,3] && diag_coord_two==[3,0]) or (diag_coord_two==[0,3] && diag_coord_one==[3,0]) | |
if (diag_coord_one==[0,0]) or (diag_coord_two==[0,0]) | |
diag_letters=[] | |
x=0 | |
y=0 | |
until x==4 do | |
diag_letters<<@dice_grid[x][y] | |
x+=1 | |
y+=1 | |
end | |
return diag_letters | |
else | |
diag_letters=[] | |
x=3 | |
y=0 | |
until x<0 do | |
diag_letters<<@dice_grid[x][y] | |
x-=1 | |
y+=1 | |
end | |
return diag_letters | |
end | |
else | |
raise ArgumentError.new("Those are not a diagonal pair!") | |
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) | |
p boggle_board.create_word(dice_grid, [0,1], [0,2], [1,2]) == "rad" | |
p boggle_board.create_word(dice_grid, [0,1], [1,1], [2,1], [3,2]) == "rock" | |
p boggle_board.get_row(0)== ["b", "r", "a", "e"] | |
p boggle_board.get_col(0)== [ "b", "i", "e", "t"] | |
p boggle_board.get_diagonal([3,3],[0,0])== ['b','o','l','e'] | |
p boggle_board.get_diagonal([3,0],[0,3])== ['t', 'c', 'd', 'e'] | |
# create driver test code to retrieve a value at a coordinate here: | |
boggle_board.get_row(0)[0]== "b" | |
#words I've found! bode, bio, ade, ate, lock, lack, tack, tackler | |
#Reflection | |
=begin | |
I'm still working on the "get diagnonal" method for my class; for now, all I'm able to do is raise an argument error if | |
one of the coordinates is not a correct diagonal to the other. While I'm not thrilled that I wasn't able to fully solve this | |
solution,I feel as though my understanding of a class in Ruby has further developed. I've learned that with a class, you're | |
able to create your own methods that are class specific! While in the long run it's a bit more work to set up a class and define | |
all the methods up front, it saves a lot of time when creating similar objects from the class (for example, if I wanted to create | |
a new boggle_board named boggle_board_christmas! Also, is there a way for us to make the indent size smaller than 8? I try | |
to switch it to 2 but it doesn't look like the switch can be made permanent. | |
**Update: Fixed the get diagonal method! | |
=end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment