Forked from dbc-challenges/0.2.1-boggle_class_from_methods.rb
          
        
    
          Last active
          January 1, 2016 12:09 
        
      - 
      
- 
        Save jrygh00/8142433 to your computer and use it in GitHub Desktop. 
    phase 0 unit 2 week 1
boggle 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
    
  
  
    
  | class BoggleBoard | |
| #2) Implement your methods | |
| def initialize (boggle_board) | |
| @boggle_board = boggle_board | |
| end | |
| #1) Access multiple elements of a nested array | |
| def create_word(*coords) | |
| coords.map { |coord| @boggle_board[coord.first][coord.last]}.join("") | |
| end | |
| #2) Write a method that takes a row number and returns all the elements in the row. | |
| def get_row(row) | |
| @boggle_board[row] | |
| end | |
| #3) Now write a method that takes a column number and returns all the elements in the column. | |
| def get_col(col) | |
| @boggle_board.collect {|row| row[col]} | |
| end | |
| #4) Access an individual coordinate and call "k" in the driver code | |
| def get_coord(col, row) # note format is reversed compared to mathematics for arrays | |
| @boggle_board[col][row] | |
| end | |
| def get_diagonal_down(col,row) #feed the starting position! | |
| #@length = @boggle_board.length | |
| #ends at the end of the board | |
| until row > 3 #@boggle_board.length | |
| print @boggle_board[col][row] | |
| col +=1 | |
| row +=1 | |
| end | |
| end | |
| end | |
| #1) Instatiate a new board or 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) | |
| # BoggleBoard "holds" the new boggle_board by feeding in the dice_grid | |
| #2) Test create_word | |
| puts boggle_board.create_word([1,2], [1,1], [2,1], [3,2]) #== "dock" | |
| puts boggle_board.get_row(0).join"" #=> brae | |
| puts boggle_board.get_row(1).join"" #=> iodt | |
| puts boggle_board.get_row(2).join"" #=> eclr | |
| puts boggle_board.get_row(3).join"" #=> take | |
| puts boggle_board.get_col(0).join"" #=> biet | |
| puts boggle_board.get_col(1).join"" #=> roca | |
| puts boggle_board.get_col(2).join"" #=> adlk | |
| puts boggle_board.get_col(3).join"" #=> etre | |
| puts boggle_board.get_coord(3,2) #=> "k" | |
| puts boggle_board.get_diagonal_down(0,0) | |
| puts boggle_board.get_diagonal_down(2,1) | |
| puts boggle_board.get_diagonal_down(3,3) #why isn't this printing out? | |
| #Reflect: | |
| # I could put the join function up in the method, otherwise, this is much like the last exercise. | |
| # We had trouble understanding why our diagonal method did not print 3,3. | |
| # I would also like it to be more flexible, on any size board. | |
| # I intend to continue to work on this... | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment