如何可视化我的 ruby 模拟树生长的程序?

How to visualize my ruby program which simulates the growth of a tree?

作为Cpine教程enter link description here的一部分,我用Ruby写了一个模拟橘子树生长的程序: 1. 你种它 2.你等等 3.它成长 4.你可以收获它的果实 等等...

现在我想尽可能简单地想象一下。你对如何做到这一点有什么想法或建议吗? IE。为它提供一个交互式用户界面,它实际上向用户显示树的生长? 越简单越好

代码如下:

class OrangeTree

  def initialize
    @age = 0
    @orangeCount = 0
    @height = 0
    puts "You planted a tree"
    height
    countTheOranges
    waitOneYear
  end

  def waitOneYear
    puts "Do you want to wait another year?"
    wantsToWait = gets.downcase.chomp
    if wantsToWait == "yes"
      puts
      oneYearPasses
    else
      puts "Goodbye"
      exit
    end
  end

  def height
    puts "The tree is #{@height} ft tall"
  end

  def oneYearPasses
    @orangeCount = 0
    puts "Another year has passed"
    @age = @age + 1
    puts "The tree is #{@age.to_s} years old"
    @height = @height + 1
    height
    orangeGrowth
    countTheOranges
    pickAnOrange
    waitOneYear
  end

  def orangeGrowth
    @orangeCount = @orangeCount + @age - 3
  end

  def countTheOranges
    if @orangeCount < 1
      puts "There are still no oranges"
    elsif @orangeCount == 1
      puts "There is one orange on the tree"
      puts "Would you like to eat an orange?"
      @wantsApple = gets.downcase.chomp
    else
    puts "There are #{@orangeCount} oranges on the tree"
    puts "Would you like to eat an orange?"
    @wantsApple = gets.downcase.chomp
    end
  end

  def pickAnOrange
    if @orangeCount > 0
      if @wantsApple == "yes"
        @orangeCount = @orangeCount - 1
        puts "That was delicious"
        if @orangeCount < 1
          puts "There are no more oranges left"
        elsif @orangeCount == 1
          puts "There is one more orange left"
        else
          puts "There are #{@orangeCount} oranges left"
          puts "Would you like another one?"
          @wantsApple = gets.downcase.chomp
          if @wantsApple == "yes"
            pickAnOrange
          end
        end
      else
        puts "Alright, hombre"
      end
    end
  end

end

tree = OrangeTree.new

只是一个想法。 如果您需要适应您的代码。

tree = []

def stats tree
  p fruits_growth = tree.sum{ |t| t[:f] }
  p fruits_eaten = tree.sum{ |t| t[:e] }
  p fruits_on_tree = fruits_growth - fruits_eaten
  p height = tree.sum{ |t| t[:h] }
end

def show_tree tree
  puts
  tree.each { |t| print "" * t[:h]}
  tree.each { |t| print "" * (t[:f] - t[:e]) + "❌" * t[:e] }
  puts
end

def eat_fruits_from tree, n
  eat = n
  tree.each do |t|
    eatable = t[:f] - t[:e]
    next if eatable == 0
    if eat >= eatable
      then
        t[:e] += eatable
        eat -= eatable
      else
        t[:e] += eat
        eat = 0
    end
  end
end

def grow tree, h, f
  tree << {h: h, f: f, e: 0}
end

grow tree, 3, 0
show_tree tree #=> 
grow tree, 4, 2
show_tree tree #=> 
eat_fruits_from tree, 2
show_tree tree #=> ❌❌
grow tree, 4, 4
show_tree tree #=> ❌❌
eat_fruits_from tree, 3
show_tree tree #=> ❌❌❌❌❌

树数组包含一系列哈希,如 {:h=>3, :f=>1, :e=>0},其中 :h 是高度的增量增长,:f 是水果,:e 是吃掉的水果。 每次树生长时,都会将一个新的散列添加到数组中。吃水果时,现有的哈希值会更新。

好吧,这可能是一个愚蠢的想法,但它应该可以工作,无需修改您的程序,以便在您使用时匹配,用于树的视觉表示(描述它)的文本,也许您可​​以使用树的 ASCII 图

=> :draw_small_tree
[2] pry(main)> draw_small_tree
                                   # #### ####
                                ### /#|### |/####
                               ##/#/ ||/##/_/##/_#
                             ###  /###|/ / # ###
                           ##__#_## | #/###_/_####
                          ## #### #  #| /  #### ##/##
                           __#_--###`  |{,###---###-~
                                      }{
                                      }}{
                                      }}{
                                 ejm  {{}
                                , -=-~{ .-^- _
                                      `}
                                       {
=> nil

def draw_small_tree
  tree = <<-EOF
                                    # #### ####
                                ### \/#|### |/####
                               ##\/#/ \||/##/_/##/_#
                             ###  \/###|/ \/ # ###
                           ##_\_#\_\## | #/###_/_####
                          ## #### # \ #| /  #### ##/##
                           __#_--###`  |{,###---###-~
                                     \ }{
                                      }}{
                                      }}{
                                 ejm  {{}
                                , -=-~{ .-^- _
                                      `}
                                       {
         EOF
puts tree
end

您可以google获得更多 ASCII 树表示

我最终结合了 iGian 和 anquegi 的答案(谢谢!)。

您可以在以下位置试玩游戏:https://repl.it/@benisburgers/OrangeTree

class OrangeTree

  def initialize
    @age = 0
    @orangeCount = 0
    @height = 0
    puts "Welcome to the orange-tree game. You can leave the game at any time by typing 'exit'. "
    puts "Would you like to plant an orange tree?"
    getFirstInput
  end

  def getFirstInput
    @firstInput = gets.downcase.chomp
    if @firstInput == 'yes'
      drawTree
      puts @tree
      puts "Congratulations. You have planted an orange tree"
      puts "There are still no oranges. Would you like to wait a few more years?"
      waitFewYears?
    elsif @firstInput == 'no'
      puts "Goodbye"
      exit
    elsif @firstInput == "exit"
      puts "Goodbye"
      exit
    else
      puts "Please type 'yes', 'no', or 'exit'"
      getFirstInput
    end
  end

  def waitFewYears?
    @input = gets.downcase.chomp
    if @input == 'yes'
      puts "Here you go"
      @age = 4
      orangeGrowth
      countTheOranges
    elsif @input == 'no'
      puts "Gooybye"
      exit
    elsif @input == 'exit'
      puts "Goodbye"
      exit
    else
      puts "Please type 'yes', 'no', or 'exit'"
      waitFewYears?
    end
  end

  def waitOneYear?
    puts "Do you want to wait another year?"
    @wantsToWait = gets.downcase.chomp
    if @wantsToWait == "yes"
      puts
      oneYearPasses
    elsif @wantsToWait == "no"
      puts "Goodbye"
      exit
    elsif @wantsToWait == "exit"
      puts "Goodbye"
      exit
    else
      puts "Please type 'yes', 'no', or 'exit'"
      waitOneYear?
    end
  end

  def oneYearPasses
    @orangeCount = 0
    @tree.replace(@originalTree)
    puts "Another year has passed"
    @age = @age + 1
    puts "The tree is #{@age.to_s} years old"
    @height = @height + 1
    orangeGrowth
    countTheOranges
    pickAnOrange?
  end

  def orangeGrowth
    if @age < 20
      @orangeCount = @orangeCount + @age - 3
    else
      @orangeCount = 17
    end
    i = 0
    while i < @orangeCount
      @tree["_"] = ""
      i = i + 1
    end
    puts @tree
  end

  def countTheOranges
    if @orangeCount < 1
      puts "There are still no oranges"
    elsif @orangeCount == 1
      puts "There is one orange on the tree"
      pickAnOrange?
    else
    puts "There are #{@orangeCount} oranges on the tree"
    pickAnOrange?
    end
  end

  def pickAnOrange?
    puts "Would you like to pick an orange?"
    @wantsApple = gets.downcase.chomp
    if @orangeCount > 0
      if @wantsApple == "yes"
        @orangeCount = @orangeCount - 1
        @tree[""] = "_"
        puts @tree
        puts "That was delicious"
        if @orangeCount < 1
          puts "There are no more oranges left"
        elsif @orangeCount == 1
          puts "There is one more orange left"
          pickAnOrange?
        else
          puts "There are #{@orangeCount} oranges left"
          pickAnOrange?
        end
      elsif @wantsApple == "no"
        puts "Alright, hombre"
      elsif @wantsApple == "exit"
        puts "Goodbye"
        exit
      else
        puts "Please type 'yes', 'no', or 'exit'"
        pickAnOrange?
      end
    end
    waitOneYear?
  end

  def drawTree
    @tree = <<-'EOF'

                \/ |    |/
              \/ / \||/  /_/___/_
              \/   |/ \/
          _\_\_\    |  /_____/_
                \  | /          /
        __ _-----`  |{,-----------~
                  \ }{
                  }{{
                  }}{
                  {{}
            , -=-~{ .-^- _
        ejm        `}
                    {

      EOF


      @originalTree = <<-'EOF'

                    \/ |    |/
                  \/ / \||/  /_/___/_
                  \/   |/ \/
              _\_\_\    |  /_____/_
                    \  | /          /
            __ _-----`  |{,-----------~
                      \ }{
                      }{{
                      }}{
                      {{}
                , -=-~{ .-^- _
            ejm        `}
                        {

          EOF

  end

end

tree = OrangeTree.new