Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save MilanGrubnic70/8701021 to your computer and use it in GitHub Desktop.
Save MilanGrubnic70/8701021 to your computer and use it in GitHub Desktop.
phase 0 unit 2 week 1boggle class challenge
class BoggleBoard
def initialize(arrays) # Initialize Class.
@arrays = arrays
end
def create_word(*coords) # Method takes *splat arguments.
coords.map { |coord| @arrays[coord.first][coord.last]}.join("") # Iiterastes through nested-array to create new array.
end # Uses #join to create new string.
def get_row(row) # Returns contents of row.
@arrays[row]
end # Method end.
def get_col(col)
@arrays.map {|row| row[col] } # Returns an array of the same indexed element of the nested array.
end # Method end.
def get_letter(coord_array) # Method to access letter.
@arrays[coord_array.first][coord_array.last]# Objective 3) Access a coordinate
end # Method end.
def get_diagonal(*coord_array) # Objective 4) Bonus: Create a #get_diagonal method.
beg_row = coord_array[0].first # The two arguments passed are the beginning and ending coordinates.
beg_col = coord_array[0].last
end_row = coord_array[-1].first
end_col = coord_array[-1].last
row = beg_row
col = beg_col
diag_array = []
if coord_array[0].last < coord_array[-1].last # One direction for diagonals.
until row > end_row do
diag_array << @arrays[row][col]
row += 1
col += 1
end # until
else # The other direction for diagonals.
until row > end_row do
diag_array << @arrays[row][col]
row += 1
col -= 1
end # until
end # if statement
diag_array
end #methods
end # class
#Objective 1) Instantiate a new board object
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:
#
# Objective 2) Create driver test code to retrieve a value at a coordinate here:
puts
p boggle_board.create_word([1,2], [1,1], [2,1], [3,2]) == "dock" #=> true
puts
p boggle_board.get_row(0) == ["b", "r", "a", "e"] #=> true
p boggle_board.get_row(1)== ["i", "o", "d", "t"] #=> true
p boggle_board.get_row(2) == ["e", "c", "l", "r"] #=> true
p boggle_board.get_row(3) == ["t", "a", "k", "e"] #=> true
puts
p boggle_board.get_col(0) == ["b", "i", "e", "t"] #=> true
p boggle_board.get_col(1) == ["r", "o", "c", "a"] #=> true
p boggle_board.get_col(2) == ["a", "d", "l", "k"] #=> true
p boggle_board.get_col(3) == ["e", "t", "r", "e"] #=> true
puts
p "Objective 2) Now print out all the rows of the board as strings."
p boggle_board.get_row(0) #=> ["b", "r", "a", "e"]
p boggle_board.get_row(1) #=> ["i", "o", "d", "t"]
p boggle_board.get_row(2) #=> ["e", "c", "l", "r"]
p boggle_board.get_row(3) #=> ["t", "a", "k", "e"]
puts
p "Objective 2) Now print out all the columns of the board as strings."
p boggle_board.get_col(0) #=> ["b", "i", "e", "t"]
p boggle_board.get_col(1) #=> ["r", "o", "c", "a"]
p boggle_board.get_col(2) #=> ["a", "d", "l", "k"]
p boggle_board.get_col(3) #=> ["e", "t", "r", "e"]
# Objectiver3) Access a coordinate.
puts
p "Can you access the 'k' character at row 3 column 2?"
p boggle_board.get_letter([3,2]) == "k" #=> true
p boggle_board.get_letter([3,2]) #=> "k"
puts
puts "Objectiver 4) Bonus: Create a #get_diagonal method"
puts
p "NW to SE diagonal test:"
p boggle_board.get_diagonal([0,0],[3,3])
p boggle_board.get_diagonal([0,0],[3,3]) == ["b", "o", "l", "e"] #=> true
p boggle_board.get_diagonal([1,0],[3,2])
p boggle_board.get_diagonal([1,0],[3,2]) == ["i", "c", "k"] #=> true
puts
p "NE to SW diagonal test:"
p boggle_board.get_diagonal([0,3],[3,0])
p boggle_board.get_diagonal([0,3],[3,0]) == ["e", "d", "c", "t"] #=> true
p boggle_board.get_diagonal([0,2],[2,0])
p boggle_board.get_diagonal([0,2],[2,0]) == ["a", "o", "e"] #=> true
puts
# create driver test code to retrieve a value at a coordinate here:
=begin
Solo Challenge (Unit 2, Week 4)
Take your time to see what concepts you feel comfortable and confident with. You are welcome to conduct research online, but make sure your code represents your own work. Please refrain from asking others for help.
Overview
Create a class BoggleBoard that includes the functionality of your methods from the previous challenge.
To do this, take a look at the methods you've created. How can they be integrated into your BoggleBoard class? What needs to change?
1) Instantiate a new board object
Transform your driver code so that it creates a new board object. You'll need to pass the original 2D array as an argument (let's call that dice_grid because boggle_board is going to be an object now.)
class BoggleBoard
#your code here
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)
How does the boggle_board object hold the dice_grid?
2) Implement your methods
One method at a time, create a test to access your new boggle_board object.
The first method should be #create_word.
(Don't get thrown off with the #method_name syntax, using # before a method name is a ruby convention.)
Write out a test with it's expectation in a comment, and then create the method in the BoggleBoard class.
Try these coordinates: (1,2), (1,1), (2,1), (3,2).
Then, write methods for #get_row and #get_col.
Can you interact with the boggle_board object and get the values you expect?
Now print out all the rows and columns of the board as strings.
You should end up with 8 four letter words. Are there any real words shown?
Add your total output as a comment in your gist.
3) Access a coordinate
Now write some driver code to access an individual coordinate in your boggle_board object.
Make this as simple as possible.
Can you access the "k" character at row 3 column 2?
4) Bonus: Create a #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.
Error checking to make sure the coordinates are actually a diagonal
would probably be a good idea.
5) Review and Reflect
You just made a transition from procedural programming to object-oriented programming! How is the implementation different?
What are the benefits to using the Object Oriented approach (even if it is a bit more code?)
What I understand is the difference between OOP and PP is that with OOP we use objects and objects hold data & code.
The code interacts with the data inside the object i.e. encapsulation. Objects in the same class share the same methods.
Objects have inheritance. Procedural Programming writes code to manipulate data.
So if I’m correct in this, with creating a class BoggleBoard that had instance methods in it we didn't have to write procedures
multiple times for each game.
All we have to do is call the method on each game. The methods are encapsulated in the game when it is created.
=>What I understand is the difference between OOP and PP is that with OOP we use objects and objects hold data & code.
The code interacts with the data inside the object i.e. encapsulation. Objects in the same class share the same methods.
Objects have inheritance. Procedural Programming writes code to manipulate data.
So if I’m correct in this, with creating a class BoggleBoard that had instance methods in it we didn’t have to write procedures
multiple times for each game.
All we have to do is call the method on each game. The methods are encapsulated in the game when it is created.
6) Submit your solution
Take a moment to look at other solutions after you submit. Were there any interesting implementations?
=end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment