2017-01-11 26 views
0

好吧,我認真地吮吸傳遞方法,只要我想從方法返回一些東西。你們能解釋一下我怎麼去傳遞它。 這裏是我的散列獨立方法本身調用另一種方法

$choosen_gun = {} 
$Weapon = { 
    :Bazoka => ["Bazoka",5], 
    :Machine_gun => ["Machine_gun",1000], 
    :Hand_gun => ["Hand_gun",24,2], 
    :Double_Hand_gun => ["Double_Hand_gun",24,4], 
    :Sniper => ["Sniper",12,1], 
    :Shot_gun => ["Shot_gun",8,2] 
} 

這裏是我的方法支招

def Weapon 
     puts "Now it's time to select your weapon." 
     puts "Please choose a weapon that is good throughout the game." 
     puts "Whenever you are shortage of bullets, please reload it." 
     puts "Please avoid last minute of reloading of weapon." 
     puts "Now choose your weapon based on your own preferences." 
     print "\n" 

     puts "Type 1" 
     puts "Gun Name: Bazoka" 
     puts "Description: A powerful gun that is strong with only 5 bullets." 
     puts "Rating: ★ ★ ★ ★" 
     num = gets.chomp.to_i 

     case num 
      when 1 
      puts "Selection of Bazoka is chosen" 
      puts "Loaded 5 bullets only" 
      $choosen_gun[num] = $Weapon[:Bazoka] 
     end  
    return num 
end 

代碼在調用該方法。用戶會選擇他的武器,並與它的NUM將其添加到$ choosen_gun哈希值,它的回報是NUM什麼類型的用戶

這裏是我的方法的代碼ZombieRoom

def ZombieRoom(w) 
    zombie = { 
     :Construcied => [5], 
     :Invader => [5], 
     :Damned => [5], 
     :Steampunk => [5], 
     :Stoner => [5], 
     :Wasted => [5], 
     :Romero => [5] 
    } 
      puts "Welcome to the worst night mare of Zombie Room" 
      puts "You will be fighting with a random zombie" 


      while true 
      puts ".........." 
      puts "Selecting a random zombie" 
      puts "Selecting your prefered gun...." 
      case w 
        when 1 
        $choosen_gun[1] 
        puts "Your selected gun is #{$choosen_gun[1][0]}" 
        #values = zombie.values 
        #puts values[rand(values.size)] 
        #random_zombie = zombie.keys.sample(1) 
        #puts random_zombie[ 
        random_zombie = zombie.to_a.sample(1).to_h 
        random_zombie.each do |key,value| 
        puts "Your random zombie is #{key}" 
        puts "With a health value of #{value[0]}" 


        puts "Time to take down that zombie now." 
        while true 
        puts "Type Shoot to knock it down or quit." 
        choice = gets.chomp 
        if $choosen_gun[1][1] >= 1 
         health = value[0] -= 1 
         $choosen_gun[1][1] -= 1 
        puts "#{key} health now is #{health}" 
        else 
        puts "Please reload your gun" 
        puts "Reloading......" 
        $choosen_gun[1][1] += 5 
        end 

        if health == 0 
         puts "You have defeated #{key}" 
         puts "Congrats!!!" 
         puts "We are happy for you" 
         puts "Lets begins to collect your prize" 
         CollectPrize() 
        else 
         puts "You did not defeat the #{key} yet" 
        end 

        end 

        end 
     end 
    end 
    end 

這裏是我的代碼方法CollectPrize

def CollectPrize 
     puts "Congratulations on defeating" 
     puts "We would now like to give you some case prizes" 

     print "\n" 

     puts "Please choose only 1 prize for yourself" 
     print "\n" 
     puts "Type 1" 
     puts "$50,000" 
     print "\n" 
     puts "Type 2" 
     puts "$25,000" 
     print "\n" 
     puts "Type 3" 
     puts "$55,000" 
     hoho = gets.chomp.to_f 


     if hoho == 1 
      puts "hehe" 
     end 
end 

這裏,我怎麼叫我的方法

ZombieRoom(Weapon()) 
CollectPrize() 

現在的問題是每當CollectPrize方法被調用時,我輸入我的輸入來收集獎勵示例1,然後打印「$ 50,000」。而不是結束這個問題,它回到了殭屍房間,並繼續在「類型射擊擊倒或退出」循環。有人能告訴我一個正確的方法來解決這個問題,或者還有什麼其他的方式來傳遞一個方法嗎?

+0

你的問題還不清楚。 「傳遞方法到方法」是什麼意思?從我所看到的,你永遠不會傳遞一個方法到任何地方,所以爲什麼這是相關的?爲什麼你需要這樣做「每當我想從方法中返回一些東西」? 「你們能解釋一下我怎樣才能通過它」。參考?另外,請提供[mcve],並特別注意「M」(最小)部分。我非常懷疑你需要120行代碼來複制你的問題。事實上,我發現了幾個與您的問題完全無關的錯誤,但使它變得非常... –

+0

...無法診斷。 –

+0

閱讀最後的陳述,你就會明白。 – Suresh

回答

1

您的代碼位於大型while true循環中。由於true始終爲真,所以它永遠不會結束,所以在它調用CollectPrize()之後,它會返回到while聲明。

你可以通過在CollectPrize()之後插入一條break行來擺脫它,但是還有另一個while true循環。

我想你需要密切關注你想如何退出while循環。

puts "Time to take down that zombie now." 
while true # <---------------- this is ALWAYS going to loop, without end 
    puts "Type Shoot to knock it down or quit." 
    choice = gets.chomp 
    if $choosen_gun[1][1] >= 1 
    health = value[0] -= 1 
    $choosen_gun[1][1] -= 1 
    puts "#{key} health now is #{health}" 
    else 
    puts "Please reload your gun" 
    puts "Reloading......" 
    $choosen_gun[1][1] += 5 
    end 
    if health == 0 
    puts "You have defeated #{key}" 
    puts "Congrats!!!" 
    puts "We are happy for you" 
    puts "Lets begins to collect your prize" 
    CollectPrize() 
    else 
    puts "You did not defeat the #{key} yet" 
    end 
end 
+0

好的,謝謝。這是我的錯誤,我沒有檢查CollectPrize方法是在zombie_room方法。這就是爲什麼它保持循環。 – Suresh

+0

沒問題,如果你發現我的答案有幫助,隨時接受它。 – SteveTurczyn

1

紅寶石常量以大寫字母開頭。方法總是用小寫來定義。

試試這個在IRB

irb(main):001:0> def Weapon 
irb(main):002:1> end 
=> :Weapon 
irb(main):003:0> Weapon 
NameError: uninitialized constant Weapon 

要使用Ruby的命名約定解決您的問題同名的方法:
zombie_roomcollect_prize

那麼這個代碼將工作:
zombie_room(weapon())

你在做什麼並不是真正的通過方法武器來方法殭屍室。 真正發生的是方法武器被執行,然後它返回一個值,並將該值的結果傳遞給方法zombie_room。

我認爲這就是你想要的。

如果您需要傳遞方法,請查閱文檔proclambda或僅使用塊。

+0

這不是嚴格正確的。如果你使用'def Weapon'和'end'和'Weapon',你會看到'未初始化的常量'錯誤。但是如果你明確地把它稱爲​​一種方法,就像OP那樣用圓括號......'Weapon()',那麼它將起作用。這並不是說這是很好的做法,但這不是OP問題的原因。 – SteveTurczyn

+0

好點。我仍然會留下這個答案,因爲他可能會從中吸取教訓。 –

+0

謝謝你們。 :) – Suresh