Forked from dbc-challenges/0.2.1-boggle_class_from_methods.rb
Last active
January 3, 2016 02:09
-
-
Save Answart/8394017 to your computer and use it in GitHub Desktop.
phase 0 unit 2 week 1
boggle class challenge
This file contains 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
=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? | |
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 ow.) | |
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 | |
=end | |
class BoggleBoard | |
attr_reader :nested_array | |
def initialize(nested_array) | |
@nested_array = nested_array | |
end | |
def create_word(*coords) | |
coords.map { |coord| @nested_array[coord.first][coord.last]}.join("") | |
end | |
def get_row(row) | |
@nested_array[row] | |
end | |
def get_row_string(row) | |
@nested_array[row].join("") | |
end | |
def get_col(col) | |
@nested_array.transpose[col] | |
end | |
def get_col_string(col) | |
@nested_array.transpose[col].join("") | |
end | |
def print_rows | |
@nested_array.each {|row| p row.join("") } | |
end | |
# BONUS | |
def get_diagonal | |
(0...@nested_array[0].size).collect {|i| @nested_array[i][i]} # [ [0,0], [1,1], [2,2], [3,3] ] | |
# (0...@nested_array[0].size) = [0, 1, 2, 3] | |
end | |
end | |
dice_grid = [["b", "r", "a", "e"], | |
["i", "o", "d", "t"], | |
["e", "c", "l", "r"], | |
["t", "a", "k", "e"]] | |
# TESTS & DRIVER TEST CODE | |
# implement tests for each of the methods here: | |
# create driver test code to retrieve a value at a coordinate here: | |
boggle_board = BoggleBoard.new(dice_grid) | |
p boggle_board | |
p boggle_board.nested_array | |
puts "\n" | |
puts boggle_board.create_word([1,2], [1,1], [2,1], [3,2]) | |
puts "\n" | |
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 "\n" | |
p boggle_board.get_row_string(0) == "brae" | |
p boggle_board.get_row_string(1) == "iodt" | |
p boggle_board.get_row_string(2) == "eclr" | |
p boggle_board.get_row_string(3) == "take" | |
puts "\n" | |
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"] | |
puts "\n" | |
p boggle_board.get_col_string(0) == "biet" | |
p boggle_board.get_col_string(1) == "roca" | |
p boggle_board.get_col_string(2) == "adlk" | |
p boggle_board.get_col_string(3) == "etre" | |
# Total ouputs are 4 rows and 4 columns. The 4th row, boggle_board[3], creates the word 'take'. | |
puts "\n" | |
puts "Can you access the 'k' character at row 3 column 2?" | |
p boggle_board.nested_array[3][2] == 'k' | |
puts "\n" | |
#BONUS | |
puts "BONUS: " | |
p boggle_board.print_rows #== "brae\niodt\neclr\ntake" | |
puts "\n" | |
# Bonus: Create a #get_diagonal method | |
p boggle_board.get_diagonal == ["b", "o", "l", "e"] | |
puts "\n" | |
=begin | |
Learned: | |
- .transpose method | |
- accessing elements in nested array with indexes: nested_array[x][y] | |
- better understanding of attr_reader | |
- difference between puts and p | |
=end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment