2017-08-27 6 views
-1

私はRubyを初めて使用し、このハングマンスタイルの単語推測ゲームで作業しています。私は2つの主な問題があります。ここで私は今で働いているものです:Ruby Word Guessing Game

class Word_game 

    def initialize(word) 
    @word = word.downcase 
    @display_word = "_ " * word.length 
    end 

    def guess_the_word(word_guess) 
    word_guess.downcase 
    @word.split("").each_with_index do |word_letter, index| 
     if word_guess == word_letter 
      @display_word[index] = word_guess 
      p @display_word 
      puts "You're getting somewhere! Keep trying!" 
     end 
     end 
     if [email protected]? (word_guess) 
      puts "Nope, guess again..." 
     end 


    def win? 
    if @word == @display_word 
     puts "Congratulations you won!!! You are the word master!!!" 
     true 
    else 
     false 
    end 
    end 

    def lose? 
    if @attempts == 0 
     puts "You lose!!" 
     true 
    end 
    end 

puts "Welcome to the Word Guessing Game! Let's see if YOU have what it TAKES!!!" 
puts "This is a 2 player game. " 
puts "Player 1... please enter a word for Player 2 to guess!" 
puts ">>" 

game_word = gets.chomp 
game = Word_game.new(game_word) 

attempts = 0 
guessed_letters = [] 

    until @attempts == game_word.length 
    puts "Ok Player 2, Guess a letter! GO!!!" 
    letter_guess = gets.chomp 
     if guessed_letters.include? letter_guess 
      puts "You already guessed that letter! Enter a new one." 
      letter_guess = gets.chomp 
     end 
    guessed_letters << letter_guess 
    game.guess_the_word(letter_guess) 
    if game.win? 
     attempts += 1 
    else game.lose? 
    end 
    end 
end 
  • 最初の単語はハローであれば、単語の進捗状況は次のようになります。これに代えて

    h _ e _ _ o 
    
  • 、スペースはありません適切な場所にあり、このようになります(コード実行の実際の結果)。

Ok Player 2, Guess a letter! GO!!! 
h 
"h _ _ _ _ " 
You're getting somewhere! Keep trying! 
Ok Player 2, Guess a letter! GO!!! 
o 
"h _ o _ _ " 
You're getting somewhere! Keep trying! 
Ok Player 2, Guess a letter! GO!!! 
e 
"he_ o _ _ " 
You're getting somewhere! Keep trying! 
Ok Player 2, Guess a letter! GO!!! 
l 
"hel o _ _ " 
You're getting somewhere! Keep trying! 
"hello _ _ " 

ユーザーが単語を推測するとき、私は「congrats」ステートメントを入れずにゲームを終了します。

私はまた、私の '失う'方法に固執しています。私は、メソッドを修正して、ユーザーが試合を使い果たして "失った"ステートメントを印刷したときにゲームが終了するようにはわかりません。

ありがとうございました!あなたの最初の問題のために

答えて

0

、あなたの@display_word始まり、次のように:

[0] = '_' # For h 
[1] = ' ' 
[2] = '_' # For e 
[3] = ' ' 
... 

をあなたは'e'を推測すると、たとえば、あなたがやる:indexが1に等しい

@display_word[index] = word_guess 

"hello"の2番目の文字をあなたが見ることができるように、それは@display_word'e'インデックスに書き込まれません。

2番目の問題には、それを修正する方法がいくつかあります。例えば、私は、既存のコードを使用して、その後、10かそこらの値から始まる@attempts_remainingを使用してのような何かをするだろう:

if [email protected]? (word_guess) 
    @attempts_remaining -= 1 # Count failure to guess 
    puts "Nope, guess again..." 
end 

その後:

def win? 
    # If you've guessed all the letters, there's no '_' left in the display word 
    if [email protected]_word.include? ('_') 
    puts "Congratulations you won!!! You are the word master!!!" 
    true 
    else 
    false 
    end 
end 

def lose? 
    if @attempts_remaining == 0 
    puts "You lose!!" 
    true 
    end 
end 

最後に、untilループ終了条件を微調整: win?lose?

until game.win? or game.lose? 

既存のコールを削除することができます。

+0

ありがとうございました!私はうまくいく方法を手に入れます...しかし、たとえそれが終わっても、あなたが最後に書いたコードのために、「予期しない復帰」というエラーメッセージが出ます。 –

+0

また、私の現在の[email protected]を変更するには?私がコードを実行するときに、「間違った」文字を入力すると「もう一度推測する」と5回印刷され、ただちに「失う」とゲームが終了します。なぜこれが起こったのか分かりません。 –

+0

最後のコメントを無視してください!私はそれをdoブロックの中に定義しておき、いったんそれを外に定義すると、それは何度も印刷を中止し、私は推測を続けることができました。最初のコメントの問題はまだ分かっていません。 –

1

出力が複雑すぎると思います。私は単語と推測を配列で追跡します。 display_word変数の代わりに、私はそれをメソッド、おそらく "to_s"にします。

ところで、Rubyの規約ではCamelCaseクラス名を使用しています。

class WordGame 
    def initialize(word) 
    @word = word.downcase.chars 
    @guesses = ["_"] * @word.size 
    end 

    def to_s 
    @guesses.join " " 
    end 

これはスペースの問題を修正するはずです。これにより、推測も簡単になります。

また、既に使用しているかどうかを確認するには、おそらくWordGameクラスで処理する必要があります。