Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6369151
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T00:46:00+00:00 2026-05-25T00:46:00+00:00

I am trying to build a train game based loosely on the old video

  • 0

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
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T00:46:00+00:00Added an answer on May 25, 2026 at 12:46 am

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

    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.

    Here is the code:

    #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
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

im trying to build a application or a method that goes through all of
I'm trying build an sort of property set in ksh. Thought the easiest way
Trying to build a linq query based on parameters a user selects (reporting) but
Trying to build a shopping cart based on the Agile Web Development 4th edition.
Currently trying to build a script utilizing cmdlets from the MS released Team Foundation
I'm traing to build a lite version for my app the way it is
I'm trying build a method which returns the shortest path from one node to
I'm trying build an App Engine connected Android application and am having some problems
I'm trying build a MVC framework, but I'm confused about manage themes. Well... I
Trying to build sslsniff on a RHEL 5.2 system here. When compiling sslsniff on

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.