저는 오래된 비디오 게임 인 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
이 더 구체적인 질문이 있습니까 : 여기
코드인가? 이것은 꽤 일반적입니다. – jtbandes그래, 나는 내가 제품 가격에 정확하게 접근하고있는 것이 걱정되는 것 같아? –
전혀 액세스하지 않습니까? 좀 더 구체적으로 질문을 편집하고 코드에 질문이있는 곳을 지적 해주십시오. – jtbandes