2017-01-11 3 views
0

좋습니다 메서드에서 메서드를 전달할 때마다 메서드에 전달할 때마다 빨아들입니다. 너희들은 내가 어떻게 지나가는 지에 대해 설명 할 수 있니? 여기 여기 메소드를 호출하면 방법 무기독립 실행 형 메서드는 자체적으로 다른 메서드를 호출하고 있습니다.

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 

내 코드의 내 해시

$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] 
} 

입니다. 사용자는 자신의 무기를 선택하고이 납입의와는 $ choosen_gun 해시에 추가합니다, 그리고 그것의 반환의 납입 어떤 사용자 유형

여기에 방법에 대한 내 코드

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 

여기에 내 코드가의 ZombieRoom

입니다 방법 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"을 출력한다는 것입니다. 문제를 끝내는 대신 ZombieRoom으로 돌아가서 "유형 찍기"를 반복하여 계속 두들 기거나 종료합니다. 누군가 atleast 나 에게이 문제를 해결하기 위해 적절한 방법이나 다른 방법을 전달하는 방법을 전달할 수 있습니까?

+0

질문이 명확하지 않습니다. "메소드를 메소드에 전달하는"것은 무엇을 의미합니까? 내가 볼 수 있듯이 메서드를 어디서나 메서드에 전달하지 않으므로 그 이유는 무엇입니까? "내가 그 방법에서 무엇인가를 돌려주고 싶을 때"왜 그렇게해야합니까? "너는 내가 어떻게 지나가는 지 설명 할 수 있니?" 인용하다? 또한 [mcve]를 제공하고 "M"(최소) 부분에 특히주의하십시오. 나는 당신이 당신의 문제를 복제하기 위해 120 줄의 코드가 필요하다는 것을 매우 의아하게 생각한다. 사실, 나는 당신의 문제와는 완전히 무관 한 몇 가지 버그를 발견했지만 꽤 많이 만들었습니다 ... –

+0

... 진단 불가능합니다. –

+0

마지막 문장을 읽으면 이해할 수 있습니다. – Suresh

답변

1

코드가 큰 while true 루프에 있습니다. 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

루비 상수는 대문자로 시작합니다. 메서드는 항상 소문자로 정의됩니다. 그런 다음이 코드가 작동
zombie_room, collect_prize

:

루비의 이름 지정 규칙을 사용하여 문제 이름 방법을 해결하기 위해 IRB

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

이 시도
zombie_room(weapon())

거기에서 뭘하고있는 건 메서드 무기를 메소드 좀비 방에 실제로 전달하지 않는 것입니다. 실제로 무슨 일이 벌어지는가는 무기가 실행 된 다음 값을 반환하고 그 값의 결과가 zombie_room 메소드에 전달됩니다.

나는 그것이 당신이 원하는 것이라고 생각합니다.

방법을 전달해야 할 경우 proc 및 에 대한 문서를 확인하거나 블록 만 사용하십시오.

+0

이것은 정확하지 않습니다. 'def weapon'과'end'와'Weapon'을하면'초기화되지 않은 상수'에러가 보일 것입니다. 하지만 당신이 explicity를 * 메서드로, 괄호를 사용하여 호출한다면 ... Weapon()은 OP처럼 작동합니다. 그건 좋은 습관이라고는 할 수 없지만 OP 문제의 원인은 아닙니다. – SteveTurczyn

+0

좋은 지적. 나는이 사실을 그가 배울지도 모르기 때문에 대답으로 남겨 둘 예정이다. –

+0

감사합니다. :) – Suresh