2015-05-21 473 views
1

即時通訊試圖讓我的祕密號碼遊戲正常工作。紅寶石 - 祕密號碼遊戲,如何讓遊戲重新啓動?

我在與獲得比賽的時候重新啓動的問題:

你有沒有更多的猜測留下 你贏得比賽

我用了while循環,但有麻煩通過函數訪問它/法..

編輯:

所以基本上玩家都有3次猜測祕密數字。如果他們贏了,他們會被問到他們是否想重新開始遊戲。如果他們輸了,他們也會被問到他們是否想重試。

這裏是代碼:

在此先感謝球員

def get_input 
     gets.chomp 
    end 

    puts "Welcome to the Secret Number Game! Please tell me your name" 
    player_name = get_input 
    puts "Welcome #{player_name}!" 
    puts "Guess a number between 1 - 10. You only have 3 attempts!" 
    secret_number = 1 + rand(10) 
    restart = true 

    while restart 

     def playAgain (restart) 
      puts "Would you like to play again? (y/n)" 
      answer = get_input 
      if answer == "n" 
       restart = false 
      end 
     end 

    def guess_check (player_input, secret_number)  
     if player_input > secret_number 
      puts "Wrong, sorry! Too high!" 
     elsif player_input < secret_number 
      puts "Wrong, sorry! Too low!" 
     else 
      puts "Congratulations, you've guessed the secret number! #{[secret_number]}" 
      playAgain (restart) 
     end 
    end 


     ############################## START GAME ########################################### 

     guesses = [] 
     attempts = 3  

     while attempts 

      attempts = attempts - 1 
      if attempts == -1 
       puts "Sorry, you have no more tries" 
       playAgain (restart) 
      else 
       puts "Guess the secret number (You have #{attempts} tries left):" 
      end 

      player_input = get_input.to_i 
      guesses.push (player_input) 
      guess_check(player_input, secret_number) 
      puts "You've guessed - #{guesses}" 

     end 

      playAgain (restart) 

    end 

回答

1

在紅寶石0是truthy,不falsey,矛盾的大多數語言。爲了打破while循環應該明確地檢查它的大於零:

- while attempts 
+ while attempts > 0 

,或者更rubyish:

3.downto(0) do |attempts| 
    ... 
end 

UPD

與重啓部分。您獲得本地重新啓動變量,在playAgain中定義。 Google對於ruby作用域,簡而言之:函數中的局部變量不在這個函數的作用域之外;通過值將參數傳遞給功能。也就是說,在playAgain之內定義restart = false根本就沒有意義,因爲局部變量在功能範圍內死亡。可能的解決方案是聲明一個實例變量@restart。在這種情況下,你不需要將它作爲參數傳遞。但最合理的方式做事情就是,從playAgain返回一個布爾值:一個嘗試後

def playAgain 
    puts "Would you like to play again? (y/n)" 
    answer = get_input 
    answer != "n" # return a boolean from the function in last statement 
end 

def guess_check (player_input, secret_number)  
    if player_input > secret_number 
    puts "Wrong, sorry! Too high!" 
    elsif player_input < secret_number 
    puts "Wrong, sorry! Too low!" 
    else 
    puts "Congratulations! Result: #{secret_number}" 
    end 
    player_input == secret_number # return true on success 
end 

# let’s go! 
loop do # main loop 
    guesses = [] 
    3.downto(1) |attempts| # 3 attemts 
    puts "Guess the secret number (You have #{attempts} tries left):" 

    player_input = get_input.to_i 
    guesses.push(player_input) 
    # you have to return true/false from guess check 
    #  and rerun the topmost loop if success 
    # see the trick with logical and below: 
    #  on normal execution the loop will return it’s initial 
    #  (3 in this particular case) 
    #  on successful guess is will break, returning false 
    break false if guess_check(player_input, secret_number) 
    puts "You've guessed - #{guesses}" 
    end && puts "Sorry, you have no more tries" 

    break unless playAgain # will break a topmost loop unless `y` input 
end 
+1

會給這個時候,我回來了:

def playAgain puts "Would you like to play again? (y/n)" answer = get_input answer != "n" # return a boolean from the function in last statement end 

然後整個範圍會看起來像下班回家。所以因爲我想在嘗試等於-1時打破嘗試循環,我應該寫「 - while attempts> -1」?這將如何讓我允許玩家在完成所有嘗試之後重新開始遊戲,或者如果他們贏了比賽?例如訪問「while = restart」循環 – giantqtipz

+1

你是否介意用簡單的英文描述你想實現的邏輯?我剛剛看到了「試圖」,導致無限循環並指出了這一點。 – mudasobwa

+1

哈哈抱歉。我應該解釋。所以這些步驟將是1)詢問玩家他們的名字是什麼。 2)告訴玩家從1到10猜數字3)他們只有3次嘗試猜測4)如果他們猜錯了3次,玩家將被問及是否要重新開始遊戲5)如果他們猜對了,他們也會被詢問是否要重新啓動。我的問題是讓「重啓」部分運行。 – giantqtipz