2011-08-18 5 views
1

저는 오래된 비디오 게임 인 Drug Wars에 느슨하게 기반한 "기차 게임"을 만들려고합니다. 저는 현재 LRTHW를 통해 자신의 길을 가고 있습니다. OOP를 사용해야한다고 생각합니다.하지만 저는 아직 그 수업을 듣지 않습니다.Ruby를 사용하여 값을 저장하고 검색하는 방법은 무엇입니까?

전제는 기차에 정해진 수의 차량이 있고 다른 도시에서 판매 할 제품을 볼 수 있다는 것입니다 (기차에 맞을 수 있다고 가정 할 때 구매 또는 판매 할 수있는 금액에 제한이 없음) . 이 코드는 완전하지는 않지만 합리적인 방법으로 제품 가격을 만들고 액세스하는 것과 관련하여이 절반의 방법으로 접근하고 있는지 궁금합니다.

#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

이 더 구체적인 질문이 있습니까 : 여기

코드인가? 이것은 꽤 일반적입니다. – jtbandes

+0

그래, 나는 내가 제품 가격에 정확하게 접근하고있는 것이 걱정되는 것 같아? –

+1

전혀 액세스하지 않습니까? 좀 더 구체적으로 질문을 편집하고 코드에 질문이있는 곳을 지적 해주십시오. – jtbandes

답변

4

평가는 액세스 데이터 (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 
+0

내가 말했듯이, 나는 당신과 OOP를 사용하는 것에 동의합니다. 그러나 나는 LRTHW와는 거리가 멀다. 그러나 나는 적절한 장을 마친 후에 그것이 나를 위해 좋은 다음 프로젝트가 될 것이라고 생각했다. –

+1

물론, 나는 요점을 되풀이하고 있었다. 모든 배열을 해시로 이동하여 데이터 액세스를 개선했습니다. 내 코드에서 설명하고 싶은 것이 있다면 알려주십시오. – Gazler

+0

이것은 의미가 있습니다. 나는 irb에서 새 부품을 가지고 놀아서 이해할 수 있도록 할 것입니다. –

관련 문제