gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 17:13:54 24 4
gpt4 key购买 nike

作为 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

最佳答案

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

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

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

你可以谷歌更多的 ASCII 树表示

关于ruby - 如何可视化模拟树生长的 ruby​​ 程序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53005783/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com