gpt4 book ai didi

ruby - 如何使用 Ruby 存储和检索值?

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

我正在尝试基于旧视频游戏“毒品 war ”构建一个松散的“火车游戏”。我目前正在学习 LRTHW,我相信我应该使用 OOP,但我还没有上那堂课。

前提是您的火车上有一定数量的车厢,并且您可以看到其他城市有哪些商品在售(不限制您可以购买或销售的数量,前提是您可以将它们装入您的火车)。这段代码并不完整,但我想知道在以合理的方式创建和访问产品价格方面,我是否已经做到了一半。

#Initializing variables. Current_location should be changed to random 
#in the future.

current_location = 'omaha'
train = []
new_york = []
chicago = []
omaha = []
dallas = []
seattle = []

def prompt()
print "> "
end

#Here is the selection menu. It is possible to exploit this and
#buy, sell and move all within the same turn.
#There needs to be a "safe selection" so that once you have moved you
#can't move again, but you can get info, buy and sell
#as many times as you would like.

def selection()
puts "Do you want to travel, buy, sell or get info?"

prompt; selection = gets.chomp

if selection.include? "travel"
puts "Where would you like to travel?"
prompt; city = gets.chomp
return 'city', city
elsif selection.include? "buy"
puts "Current Prices Are:"
puts "What would you like to Buy?"
elsif selection.include? "sell"
puts "Current Prices Are:"
puts "What would you like to sell?"
elsif selection.include? "info"
puts "What city or train would you like info on?"
else
puts "Would you like to exit selection or start selection again?"
end
end

#This generates a new cost for each good at the start of each turn.
def generate_costs(new_york, chicago, omaha, dallas, seattle)
new_york[0] = rand(10)
new_york[1] = rand(10) + 25
new_york[2] = rand(5) + 10

omaha[0] = rand(10)
omaha[1] = rand(10) + 25
omaha[2] = rand(5) + 10

chicago[0] = rand(25) + 5
chicago[1] = rand(5) + 10
chicago[2] = rand(4)

dallas[0] = rand(6) + 11
dallas[1] = rand(3) + 10
dallas[2] = rand(8)

seattle[0] = rand(6)
seattle[1] = rand(10) + 24
seattle[2] = rand(14) + 13


return new_york, chicago, omaha, dallas, seattle

end


# This is my main() loop. It drives the game forward.
for i in (0..5)
new_york, chicago, omaha, dallas, seattle = generate_costs(new_york, chicago, omaha, dallas, seattle)

turns = 5 - i
puts "You are currently in #{current_location}. You have #{turns} remaining."

puts "{ ___________________________ }"

#Code Here evaluates and accesses pricing based on current_location.
#Is this the correct way to do this?
fish = eval("#{current_location}[0]")
coal = eval("#{current_location}[1]")
cattle = eval("#{current_location}[2]")
puts "Fish is worth #{fish}"
puts "Coal is worth #{coal}"
puts "Cattle is worth #{cattle}"
puts "{ ___________________________ }"

change, value = selection()
if change == 'city'
current_location = value
elsif change == 'buy'
puts 'So you want to buy?'
else
puts "I don't understand what you want to do"
end

end

最佳答案

eval 是一种访问数据的讨厌方式 (When is `eval` in Ruby justified?)。您应该考虑将事物移动到对象中。

我稍微改进了代码,将城市存储在散列中,从而摆脱了评估。我已经删除了 generate_costs 逻辑,但您可以通过以下方式分配它:

cities[:new_york][0] = rand(10)

理想情况下,代码应该以面向对象的语法重写。如果我有时间,我会为您举个例子。

代码如下:

#Initializing variables. Current_location should be changed to random 
#in the future.

current_location = :omaha
train = []
cities = {
:new_york => [],
:chicago => [],
:omaha => [],
:dallas => [],
:seattle => []
}

def prompt()
print "> "
end

#Here is the selection menu. It is possible to exploit this and
#buy, sell and move all within the same turn.
#There needs to be a "safe selection" so that once you have moved you
#can't move again, but you can get info, buy and sell
#as many times as you would like.

def selection()
puts "Do you want to travel, buy, sell or get info?"

prompt; selection = gets.chomp

if selection.include? "travel"
puts "Where would you like to travel?"
prompt; city = gets.chomp
return 'city', city
elsif selection.include? "buy"
puts "Current Prices Are:"
puts "What would you like to Buy?"
elsif selection.include? "sell"
puts "Current Prices Are:"
puts "What would you like to sell?"
elsif selection.include? "info"
puts "What city or train would you like info on?"
else
puts "Would you like to exit selection or start selection again?"
end
end

#This generates a new cost for each good at the start of each turn.
def generate_costs(cities)
cities.each do |key,city|
0.upto(2) do |i|
city[i] = rand(10)
end
end
end


# This is my main() loop. It drives the game forward.
for i in (0..5)
generate_costs(cities)

turns = 5 - i
puts "You are currently in #{current_location}. You have #{turns} remaining."

p cities

puts "{ ___________________________ }"
fish = cities[current_location][0]
coal = cities[current_location][1]
cattle = cities[current_location][2]
puts "Fish is worth #{fish}"
puts "Coal is worth #{coal}"
puts "Cattle is worth #{cattle}"
puts "{ ___________________________ }"

change, value = selection()
if change == 'city'
current_location = value
elsif change == 'buy'
puts 'So you want to buy?'
else
puts "I don't understand what you want to do"
end

end

关于ruby - 如何使用 Ruby 存储和检索值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7113208/

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