Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save CharlieTruong/7349374 to your computer and use it in GitHub Desktop.
Save CharlieTruong/7349374 to your computer and use it in GitHub Desktop.
phase 0 unit 2 week 1 boggle class challenge
class BoggleBoard
attr_accessor :board
def initialize (board)
#the board is stored in an instance variable
@board=board
end
def create_word(*coords)
coords.map { |coord| board[coord.first][coord.last]}.join("")
end
def get_row(row)
board[row-1]
end
def get_col(col)
board.map { |row| row[col-1]}
end
def get_diagonal(coord1,coord2)
if (coord2.first - coord1.first).abs != (coord2.last - coord1.last).abs
raise ArgumentError.new("This is not a diagonal coordinate")
else
(coord2.first - coord1.first) > 0 ? first_chg = 1 : first_chg = -1
(coord2.last - coord1.last) > 0 ? last_chg = 1 : last_chg = -1
new_array = []
num_letters = (coord2.first - coord1.first).abs
for x in 0..num_letters
new_array.push(board[coord1.first+(first_chg*x)][coord1.last+(last_chg*x)])
end
new_array
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.create_word([1,2], [1,1], [2,1], [3,2]) == "dock"
p boggle_board.get_row(1)==["b","r","a","e"]
p boggle_board.get_col(3)==["a","d","l","k"]
p boggle_board.get_diagonal([0,3],[3,0]) == ["e","d","c","t"]
p boggle_board.get_diagonal([0,1],[2,3]) == ["r","d","r"]
p boggle_board.get_diagonal([0,2],[2,0]) == ["a","o","e"]
#Objective 2 - printing all rows and columns
for num_row in 1..boggle_board.board.length
p boggle_board.get_row(num_row).join("")
end
#Output--
#"brae"
#"iodt"
#"eclr"
#"take"
for num_row in 1..boggle_board.board[0].length
p boggle_board.get_col(num_row).join("")
end
#Output--
#"biet"
#"roca"
#"adlk"
#"etre"
# create driver test code to retrieve a value at a coordinate here:
p boggle_board.board[3][2] == "k"
#This was an easy challenge since I actually created a class for the first boggle board exercise.
#So, most of this was copy and paste. I spent quite a bit of time on the diagonal method.
#I had to do some examples to see how the coordinates would need to change to identify
#the best iteration approach. My programming partner showed me the benefits of using
#attr_accessor, allowing one to call the instance variable without using @
@cnocon
Copy link

cnocon commented Nov 9, 2013

This is awesome! I had almost the same logical steps in my diagonal method (which currently isn't working properly) and now I can see that I wasn't correctly accounting for the direction of the horizontal and vertical changes. I was trying to be too fancy (aka unnecessarily complicated). I really love how simple your solution is, and easy to read, as well. Great work!

I would suggest perhaps instead of using attr_accessor that you use attr_reader. While both work for calling @board outside of the class definition, attr_accessor gives someone the ability to change the board outside of the class method as well. Unless you wanted the user to be able to manipulate the board, I'd recommend just using attr_reader, which will allow you to call the @board object itself outside the definition, but not change it. Attr_accessor is essentially BOTH attr_reader and attr_writer.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment