如何可视化模拟树生长的红宝石程序?



作为 Cpine 教程的一部分,在此处输入链接描述,我使用 Ruby 编写了一个模拟橙树生长的程序: 1. 你种它 2. 你等待 3. 它成长 4.你可以收获它的果实 等等...

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

代码如下:

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 树表示形式

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

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是 heigh 的增量增长,:f是水果,:e是吃的水果。 每次树增长时,都会向数组中添加一个新的哈希值。当吃水果时,现有的哈希值会更新。

我最终使用了 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

最新更新