Đang chuẩn bị liên kết để tải về tài liệu:
Best of Ruby Quiz Pragmatic programmers phần 6

Đang chuẩn bị nút TẢI XUỐNG, xin hãy chờ

Bạn có thể thấy rằng nó hỗ trợ nhiều loại câu trả lời có thể được chuyển đổi bao gồm Integer, Symbol, hoặc thậm chí DateTime.Bao gồm cả những phản hồi và những đề nghị, gợi ý có thể có bất kỳ giao diện, nhưng sẽ có thể chơi chống lại con người | Answer 11. Sokoban 139 sokoban objectified.rb module Sokoban class Crate def to_s o end end class Person def to_s @ end end end Then we get to the meat of the program which is the Level class sokoban objectified.rb module Sokoban class Level attr_reader move s def initialize str @grid str.split n .map ln ln.split .map c Tile.create c throw SokobanError.new No player found on level if player_index throw SokobanError.new No challenge if solved @move s 0 end def r c @grid r c end def to_s @grid.map row row.j oin .join n end returns a 2-element array with the row and column of the player s position respectively def player_index @grid.each_index do row @grid row .each_index do col if @grid row col .respond_to resident Person @grid row col .resident return row col end end end nil end def solved a level is solved when every Storage tile has a Crate @grid.flatten.all tile Storage tile tile.has_crate end def move dir if NORTH SOUTH EAST WEST .include dir Report erratum Answer 11. Sokoban 140 pos player_index target @grid pos 0 dir 0 pos 1 dir 1 if Floor target if Crate target.resident indirect_target @grid pos 0 2 dir 0 pos 1 2 dir 1 if Floor indirect_target indirect_target.resident @grid pos 0 2 dir 0 pos 1 2 dir 1 @grid pos 0 dir 0 pos 1 dir 1 .clear @grid pos 0 dir 0 pos 1 dir 1 @grid pos 0 pos 1 .clear return @moves 1 end else @grid pos 0 dir 0 pos 1 dir 1 @grid pos 0 pos 1 .clear return @moves 1 end end end nil end end end Level objects build a @grid of Tile objects in initialized to manage their state. The methods and to_s provide indexing and display for the @grid. You can also easily locate the Person object in the @grid with player_index and see whether the Level is complete with solved . The final method of Level is move which works roughly the same as Dennis s version. It finds the player and checks the square in the direction the player is trying to move. If a crate is found there it also checks the square behind that one. The rest of Dave s solution is an .