如何使用Ruby存储和检索值? [英] How to store and retrieve values using Ruby?

查看:79
本文介绍了如何使用Ruby存储和检索值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在努力建立一个基于旧电视游戏毒品战争的火车游戏。我目前正在通过LRTHW工作,我相信我应该使用OOP,但我不是那个课程。

I am trying to build a "train game" based loosely on the old video game "Drug Wars." I am currently working my way through LRTHW, and I believe that I should be using OOP, but I'm not to that lesson yet.

前提是你有您列车上的一定数量的汽车,您可以看到在其他城市出售哪些产品(您可以购买或出售的金额限制,假设您可以将其适合您的火车)。这段代码还不完整,但是我想知道如果我以合理的方式创建和访问产品价格,我是否正在半途而废。

The premise is that you have a set number of cars on your train and you can see what products are for sale in other cities (no limit on the amount you can buy or sale presuming you can fit them in your train). This code isn't complete, but I'm wondering if I'm even approaching this half way sanely in regard to creating and accessing the product prices in a reasonable manner.

#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是访问数据的一种讨厌的方式(什么时候在Ruby中的`eval`对齐)。你应该考虑将事物移动到一个对象中。

eval is a nasty way of accessing data ( When is `eval` in Ruby justified ). You should consider moving things into an object.

我稍微改进了代码,将城市存储在一个哈希中,从而摆脱了evals。我已经剔除了generate_costs逻辑,但是可以通过执行以下操作来分配:

I have improved the code slightly, storing the cities in a hash, which gets rid of the evals. I have stubbed out the generate_costs logic but you can assign it by doing:

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

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

Ideally, the code should be re-written in an object-oriented syntax. If I get some time then I'll knock up an example for you.

这是代码:

#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存储和检索值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆