Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save coopers/8394812 to your computer and use it in GitHub Desktop.
Save coopers/8394812 to your computer and use it in GitHub Desktop.
phase 0 unit 2 week 1 boggle class challenge
=begin
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?
Remember to Enter the collaboration hangout. Please turn off your video and sound. Use the chat function.
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? #Yes, take 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?
=end
class BoggleBoard
def initialize(dice_grid)
@dice_grid = dice_grid
end
def create_word (*coords)
coords.map { |coord| @dice_grid[coord.first][coord.last]}.join("")
end
def get_row(row)
@dice_grid[row]
end
def get_column(column)
@dice_grid.transpose[column]
end
def print_row(row)
puts @dice_grid[row].join("")
end
def print_column(column)
puts @dice_grid.transpose[column].join("")
end
def print_all_rows
@dice_grid.each_index {|i| print_row(i)}
end
def print_all_columns
@dice_grid.each_index {|i| print_column(i)}
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)
puts boggle_board.create_word([2,1], [1,1], [1,2], [0,3]) == "code"
puts boggle_board.create_word([0,1], [0,2], [1,2]) == "rad"
puts boggle_board.create_word([1,2], [1,1], [2,1], [3,2]) == "dock"
puts boggle_board.get_row(0) == ["b", "r", "a", "e"]
puts boggle_board.get_column(0) == ["b", "i", "e", "t"]
puts
boggle_board.print_row(0)
puts
boggle_board.print_column(0)
puts
boggle_board.print_all_rows
boggle_board.print_all_columns
puts
puts boggle_board.create_word([3,2]) == "k"
# Reflection:
# This problem did reveal a problem that I haven't found the best solution for.
# I am getting better at writing tests that return a series of true statements, if the program works correctly.
# We were asked to create a print_all_rows method, which prints out each row from the boggle_board.
# What is the correct way to write a test for that? The one I wrote didn't work.
# Puts and print don't return any value, they just print to the console.
# I'd rather read true, true, true than look at a series of print_all_rows output.
# I added a return statement to the method so that it would return the value.
# That didn't work out the way I wanted either.
#
# Again, I want to write driver code for the print_all_rows method that returns a true or a series of true statements,
# if the program returns what it is expected to return.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment