Forked from dbc-challenges/0.2.1-boggle_class_from_methods.rb
Last active
August 29, 2015 13:55
-
-
Save helloanh/8725292 to your computer and use it in GitHub Desktop.
phase 0 unit 2 week 1boggle class challenge
This file contains hidden or 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 | |
Anh Kim Hoang | |
Phase 0, Unit 2, Week 1 | |
Challenge: Boggle Class from Methods | |
Requirements: | |
1.Create a class BoggleBoard that includes the functionality of your methods from the previous challenge. | |
2.One method at a time, create a test to access your new boggle_board object. | |
The first method should be #create_word. | |
Then, write methods for #get_row and #get_col. | |
Now print out all the rows and columns of the board as strings. | |
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. | |
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. | |
Can you access the "k" character at row 3 column 2? | |
Bonus: | |
Pseudocode: | |
Input: coordinates pair [x,y] | |
Output: string based on diagonal characters from boggle board | |
1. check to see if coordinates are not horizontal or vertical | |
2. diagonal pattern seems to say x == y for board to be diagonal | |
[0,0] | |
[1,1] | |
[2,2] | |
if we loop the pair and iterate +1 to both values on each pass, then | |
the each time | |
[x,y] | |
[x+1, y+1] | |
[x+2, y+1] | |
4. how to do the opposite for right to left/reverse diagonal strings? perhaps use the reverse string method | |
5. return diagonal string | |
Reflections: I'm still stuck on writing the diagonal method. | |
=end | |
#--------------------------------------------------------------------------------------- | |
# Version 1.0, code breaks, error: undefined local variable or method `row' for #<BoggleBoard:0x000001011885d8> | |
class BoggleBoard | |
attr_accessor :boggle_board | |
def initialize(boggle_board) | |
@boggle_board = boggle_board | |
end | |
def create_word(*coords) | |
coords.map { |x| boggle_board[x.first][x.last]}.join | |
end | |
def get_row(row_num) | |
boggle_board[row] | |
end | |
def get_column(col_num) | |
boggle_board.map { |row| row[col_num]} | |
end | |
end | |
# 0 1 2 3 | Writen as matrix-like arrays | |
dice_grid = [ ["b", "r", "a", "e"], # 0 | # [0,0][0,1][0,2][0,3] | |
["i", "o", "d", "t"], # 1 | # [1,0][1,1][1,2][1,3] | |
["e", "c", "l", "r"], # 2 | # [2,0][2,1][2,2][2,3] | |
["t", "a", "k", "e"] ] # 3 | # [3,0][3,1][3,2][3,3] | |
# implement tests for each of the methods here: | |
# ------------- driver code -------------- # | |
#the boggle_board object holds an initialized object instance of the class BoggleBoard | |
boggle_board = BoggleBoard.new(dice_grid) | |
#Write out a test with it's expectation in a comment, and then create the method in the BoggleBoard class. | |
boggle_board.create_word([1,2], [1,1], [2,1], [3,2]) # => "dock" | |
boggle_board.create_word([0,1], [0,2], [1,0], [2,2]) # => "rail" | |
boggle_board.get_row(0) #=> ["b", "r", "a", "e"] | |
boggle_board.get_row(1) #=> ["i", "o", "d", "t"] | |
boggle_board.get_row(2) #=> ["e", "c", "l", "r"] | |
boggle_board.get_row(3) #=> ["t", "a", "k", "e"] | |
boggle_board.get_col(0) #=> ["b", "i", "e", "t"] | |
boggle_board.get_col(1) #=> ["r", "o", "c", "a"] | |
boggle_board.get_col(2) #=> ["a", "d", "l", "k"] | |
boggle_board.get_col(3) #=> ["e", "t", "r", "e"] | |
# create driver test code to retrieve a value at a coordinate here: | |
p boggle_board.dice_grid[3][2] #=> "k" | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment