ruby 阶段0单位2周1boggle班级挑战

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ruby 阶段0单位2周1boggle班级挑战相关的知识,希望对你有一定的参考价值。

=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?)

6) Submit your solution
Take a moment to look at other solutions after you submit. Were there any interesting implementations? 
=end


class BoggleBoard
  def initialize(board)
    @board = board

  end

  def create_word(*coords)
  	coords.map{|coord| @board[coord.first][coord.last]}.join("")
  end

  def get_row(row)
    @board[row][0..3]
  end
  
  def get_col(col)
  	x = 0
  	col_ar = []
  	until x == @board.length
  	  col_ar << @board[x][col]
  	  x += 1
    end
    col_ar
  end


# this will take two coordinates and return any items
# in the grid that lies between them diagonally
#  
  def get_diagonal(*coords) #!!!!not functional!!!


    # counter = 0
  	# until counter == 3
    #    coords.first.map {|x| x + counter}
    #    counter += 1
    #  end
    if coords[1][0] > coords[0][0]
      if coords[1][1] > coords[0][1]
        
        until coords.first == coords.last
           puts coords.first.each{|x| x+1}
        end
      end
    end
      # [coords.first, coords.first.map{|x| x + 1 },
      # coords.first.map{|x| x + 1}.map{|x| x + 1}, coords.last]

    
  end
end
#-----------------------------------------------------------------
# DRIVER CODE

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 "This is test 1 for #create_word"
p boggle_board.create_word([1,2], [1,1], [2,1], [3,2]) == "dock"
puts "This is test 2 for #get_row" 
p boggle_board.get_row(0) == ["b", "r", "a", "e"]
puts "This is test 3 for #get_col"
p boggle_board.get_col(0) == ["b", "i", "e", "t"]
puts "This is test 4 for #create_word to return a single coordinate"
p boggle_board.create_word([3,2]) == "k"
puts "This is test 5 for #get_diagonal"
p boggle_board.get_diagonal([0,0],[3,3])
p boggle_board.get_diagonal([3,0],[0,3])


#-----------------------------------------------------------------
# REFLECTION
# So I got really frustrated with the get diagonal function
# and I had to let it go for now, so it's not functioning.
# I don't understand why you can't iterate over part of the
# input array, and add 1 until it equals the second part of the 
# input array. Gah! 
# Otherwise, ood is awesome. In terms of apperance, it's nice to
# have all of your methods grouped together in a class, and all 
# objects in the class can use the methods.

以上是关于ruby 阶段0单位2周1boggle班级挑战的主要内容,如果未能解决你的问题,请参考以下文章

ruby 阶段0单位2周1boggle班级挑战

ruby 阶段0单位2周1boggle班级挑战

ruby 阶段0单元2周2

ruby 阶段0单元2周2

项目复审——Alpha阶段

每天学点Python案例四:52周存钱挑战