在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
curr = curr.process 用替换来生成树,因为从来没做过面向对象法建树,所以比较新鲜 再一个就是UI类的运用
UI
def self.say(str) print str + "\n" end def self.say_asking(str) UI.say str =~ /[\?\.]$/ ? str : str + "?" end def self.ask_if(str) UI.say_asking str !((gets =~ /[Yy]/).nil?) end def self.ask(str) UI.say_asking str gets.chomp end end class Animal def initialize(name) @name = name end def ask UI.ask_if "Is it #{@name}" end def process if ask UI.say "I win. Pretty smart, aren' t I?" self else UI.say "You win. Help me learn from my mistake before you go" animal = UI.ask "What animal were you thinking of?" question = UI.ask "Give me a question to distinguish #{animal} from #{@name}." answer = UI.ask_if "For #{animal}, what is the answer to your question?" UI.say "Thanks" animal = Animal.new(animal) Question.new(question,answer ? animal : self,!answer ? animal : self) end end end class Question def initialize(question,yes,no) @question = question @yes = yes @no = no end def ask UI.ask_if @question end def process if ask @yes = @yes.process else @no = @no.process end self end end curr = Animal.new("elephant") loop do curr = curr.process break unless UI.ask_if("Play again?") end |
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论