Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save rmathur101/8376369 to your computer and use it in GitHub Desktop.
Save rmathur101/8376369 to your computer and use it in GitHub Desktop.
phase 0 unit 2 week 1 boggle class challenge
#Create a Boggle Board Class using the methods created in BoggleBoardArray.rb
class BoggleBoard
attr_accessor :board
def initialize(board)
@board = board
end
def create_word(*coords) #I think the * in front of coords significes that there are mulitiple such variables that can be passed as arguments
coords.map { |coord| @board[coord.first][coord.last]}.join("") #.map method: is called upon the *coords arguments(s) which is a collection of coordinates in this case. It takes each coordinate and uses the board (with the letters) and the coordiantes to find the corresponding letter for each coordinate, and it joins this letter with the letter that corresponds with the next coordinate that is passed, and so on
end
def get_row(row)
return (@board[row]).join(", ")
end
def get_col(col)
col_array = []
@board.each{
|row|
col_array.push(row[col])
}
return col_array.join(", ")
end
def get_diagonal(c1, c2)
diagonal_array = []
if (c1[0] - c2[0]).abs != (c1[1] - c2[1]).abs
return "These coordinates don't make a diagonal"
end
result = (c1[0] - c2[0]) * (c1[1] - c2[1])
if result > 0
while c1[0] > 0 && c1[1] > 0
c1[0] = c1[0] - 1
c1[1] = c1[1] - 1
end
while (c1[0] <= @board.size - 1)
diagonal_array.push(@board[c1[0]][c1[1]]) #change to @board
c1[0] = c1[0] + 1
c1[1] = c1[1] + 1
end
diagonal_array.delete(nil)
return diagonal_array
else
while c1[0] < @board.size - 1 && c1[1] > 0
c1[0] = c1[0] + 1
c1[1] = c1[1] - 1
end
while (c1[0] <= @board.size - 1 && c1[0] >= 0)
diagonal_array.push(@board[c1[0]][c1[1]]) #change to @board
c1[0] = c1[0] - 1
c1[1] = c1[1] + 1
end
diagonal_array.delete(nil)
return diagonal_array
end
end
end
dice_grid = [["b", "r", "a", "e"],
["i", "o", "d", "t"],
["e", "c", "l", "r"],
["t", "a", "k", "e"]]
#DRIVER CODE
boggle_board = BoggleBoard.new(dice_grid) #Expect: create new instance of BoggleBoard called boggle_board that takes in a 2-D array as an argument
#TESTING create_word
p boggle_board.create_word([2,1], [1,1], [1,2], [0,3]) == "code" #Expect: True
p boggle_board.create_word([1,2], [1,1], [2,1], [3,2]) == "dock" #Expect: True
#TESTING get_row
p boggle_board.get_row(0) #Expect row 1
p boggle_board.get_row(1) #Expect row 2
p boggle_board.get_row(2) #Expect row 3
p boggle_board.get_row(3) #Expect row 4 (word: "take")
#TESTING get_col
p boggle_board.get_col(0) #Expect col 1
p boggle_board.get_col(1) #Expect col 2 (word: "roca")
p boggle_board.get_col(2) #Expect col 3
p boggle_board.get_col(3) #Expect col 4
#TESTING get a letter by inputing a coordinate
p boggle_board.board[3][2] #Expect "k"; I am directly accessing a letter of the board. To do this I first had to write the attr_accessor :board which allows me to access the object that I'm passing in as an argument to the object. I can then access the dice_grid like anyother 2-D array.
#TESTING get_diagonal
p boggle_board.get_diagonal([0,0], [3,3]) #Expect array with "b o l e"
p boggle_board.get_diagonal([0,3], [3,0]) #Expect array with "t c d e"
p boggle_board.get_diagonal([1,0], [0,1]) #Expect array with "i r"
#Brainstorming #get_diagonal
#We are going to put two coordinates as the input to the method
#We need to check if these coordiantes are diagonal in the first place before we can print out the diagonal array
#Two coordinates must be given (otherwise a number of arguments error will be thrown)
#For each coordinate, the other coordinate must be (+ or -) x(1,1) where x is an integer => Pseudocode
=begin
[0,0] [0,1] [0,2] [0,3]
[1,0] [1,1] [1,2] [1,3]
[2,0] [2,1] [2,2] [2,3]
[3,0] [3,1] [3,2] [3,3]
=end
=begin
REFLECTION
Most of this was pretty standard. The exercise was simply to take procedural programming and to make it object oriented. The idea is that now we can
generalize the case of boards. Whenever we want to use a board we can create a an instance of that object of the class BoggleBoard. This class
comes complete with its own set of methods that we can use on the object we can create. It's a much neater way of organizing the machinery
that we are creating. It allows us flexibility and generalizability that allows us to be more methodical in our approach to coding. Aside from all this
the most challenging part of this was creating the get_diagonal method. It was good that I started with pseudocode which allowed me to get perspective
but the exercise still took way too long. I even made a diagram (shown above) that was supposed to help me thing about it but I really didn't get a
firm grasp on it. I think I wasn't methodical enough in my approcah. I should have asked more questions and really interrogated the problem so that
even though it felt like I was getting through it slowly it would have been more efficient. I think pseudocode is vital to getting a good grasp on
1) a description of the problem 2) questions that need to be answered 3) a general process to approach. Do more pseudocode next time.
=end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment