2016-10-19 65 views
0

我從零開始的第一個hang子手遊戲幾乎完成了真正的基本的東西。當玩家正確地工作時,我只需要一個計數器。我還沒有想出一個好的方法來做到這一點,而不刪除一切,重新開始。 @correct_tries正確計數,然後在elsif中比較數字值與給定單詞中正確回答的元素數量。如何使@correct_tries的elsif正常工作?

當@correct_tries與數組上的.inspect發生比較時,它應該停止計數。但它一直在計數。

class Hangman 

Profanity = ['cobol','snit','crap','court'] 
Adjective = ['foul','repugnant','swift','fast'] 
Noun = ['king','queen','prince','princess'] 


    def self.start_game 
     print "Welcome to Hangman V1 by Clueless! Please select which category of words do you want to use: \n Profanity, Adjective, Noun " 
     @selection = gets.chomp.downcase 
     case 
     when @selection == 'profanity' 
     puts 'You have selected profanity! ' 
     hangman_word_selection 
    when @selection == 'adjective' 
     puts 'You have selected Adjectives! ' 
     hangman_word_selection 
    when @selection == 'noun' 
     puts 'You have selected nouns! ' 
      hangman_word_selection 
    end 
    end 

    def self.hangman_word_selection 

    if @selection == 'profanity' 
     hangman_word = Profanity.sample 
     hangman_word_setup(hangman_word) 
     #puts '_ ' * hangman_word.size 
    elsif @selection == 'adjective' 
     hangman_word = Adjective.sample 
     hangman_word_setup(hangman_word) 
    elsif 
      @selection == 'noun' 
     hangman_word = Noun.sample 
     hangman_word_setup(hangman_word) 

    end 
end 

    def self.hangman_word_setup(hangman_word) 
    hangman_word_array = hangman_word.chars.to_a 
    @hangman_end = false 
    #while(@hangman_end == false) 
    #puts "*" * 40 
    #puts 
    #puts 
    puts 'You have five tries to get the word correct. If you can guess the whole word do so but you only have one try. Or just guess letter by letter.' 
    p hangman_word_array 
    @total_tries = 0 
    @correct_tries = 0 
    game_check = true 

    while game_check == true 
    first_try = gets.chomp.downcase 



    if(first_try == hangman_word_array[0] || first_try == hangman_word_array[1] || first_try == hangman_word_array[2] || first_try == hangman_word_array[3] || first_try == hangman_word_array[4]) 
    puts 'Check' 
    @correct_tries += 1 
    p @correct_tries 
    #correct tries equal to the number of chars in the given word check it. 
    puts 'You have gotten it correct!' 
    elsif(first_try == hangman_word) 
    puts 'You have completed the word! Congratulations you win!' 
       hangman_win 
       break 
    elsif(first_try != hangman_word_array) 
    puts 'Wrong.' 
       @total_tries += 1 
       p @total_tries 
      #puts "*" * 40 
     elsif(@correct_tries == hangman_word_array.inspect) 
       puts 'done.' 
       break 


    end 
    end 
    end 



    def self.hangman_loss 
    puts ' +---+- 
       | | 
       | 0 
       | |\\ 
       | /\\ 
       -+----------' 

    puts 'You lose!'    
    end 


    def self.hangman_win 

    puts 'NEED HANGMAN VICTORY POSE HERE.' 

    end 

start_game 

end 
+0

注意:常量應該在'ALL_CAPS'名稱聲明也可以使用'case'打破當你比較相同的變量與多個值時,你可以使用'@ selection'做的地方。 – tadman

回答

0

elsif條件 elsif(@correct_tries == hangman_word_array.inspect)永遠不會爲真。 @correct_tries是一個數值和hangman_word_array.inspect將以字符串格式返回字的數組劊子手(如hangman_word_array = ['a', 'b', 'c']然後hangman_word_array.inspect"[\"a\", \"b\", \"c\"]"

+0

Ahh alrighty然後合理。謝謝 – Clueless

+0

使用.size,但我想它的工作方式,但@correct_tries只是重新啓動自己並不會打破循環? – Clueless