Ruby词猜猜游戏

时间:2017-08-27 23:35:12

标签: ruby class methods

我是Ruby的新手并且使用这个刽子手风格的单词猜谜游戏。我有两个主要问题。以下是我现在正在使用的内容:

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 !@word.include? (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 
  • 首先,如果单词是hello,单词progress应该如下所示:

     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 _ _ "

当用户猜到这个词时,它不会发出我的“恭喜”声明并结束游戏。

我也坚持我的“失败”方法。我不确定如何修复该方法,以便当用户用完计算并打印“丢失”语句时游戏结束。

感谢您的帮助!

3 个答案:

答案 0 :(得分: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类处理。

答案 1 :(得分:0)

对于您的第一个问题,您的@display_word开头如下:

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

例如,当你猜'e'时,你会这样做:

@display_word[index] = word_guess

index等于1,"hello"中的第二个字符,因此您可以看到它不会写入'e'中的@display_word索引。

对于您的第二个问题,有很多方法可以解决它。例如,我会做一些事情,比如从值10开始使用@attempts_remaining,然后使用现有代码:

if !@word.include? (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 !@display_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循环终止条件:

until game.win? or game.lose?

可以删除对win?lose?的现有来电。

答案 2 :(得分:-2)

(缺字游戏)

放入“” 放置“ 总回合” 放入“” 放置“ => [第一轮,第二轮,第三轮三轮]]” 放入“” 放置“ => [共5次尝试]” 放入“” 一个=“” two =“” three =“” four =“” 放入“” 把“ --ROUND一按-=>(1)” 一个= gets.to_i 放置'===================' 放入“问题:=>(K?N G)” 放入“” c = 5 5次 string1 ='i' stringone =“ I” 将“答案:=>尝试否:#{c} ” string2 = gets.chomp 如果(string1 == string2) 放置“ 正确的正确工作” 打破 elsif(stringone == string2) 放置“ 正确的正确工作” 打破 其他 放置“ -Worng spaling-” 结束 c-= 1 结束 将“ 环绕” 如果c <1 放置“ 在游戏结束时尝试” 出口 结束 放置'===================' 把“ --ROUND两次按下-=>(2)” 两个= gets.to_i 放置'===================' 放置“问题:=>(P L?Y)” 5次 string1 ='a' stringone =“ A” 放置“答案:=>尝试否:#{c} ” string2 = gets.chomp 如果(string1 == string2) 放置“ 正确的正确工作” 打破 elsif(stringone == string2) 放置“ 正确的正确工作” 打破 其他 放入“ -Worng spaling-” 结束 c-= 1 如果c <1 放置“ 在游戏结束时尝试” 出口 结束 结束 将“ Round Over

puts '==================='
puts "--ROUND Three press-- => (3)"
three=gets.to_i
puts '==================='
puts "Question:=>  ( S P ? T )"
5.times do
string1 = 'o' 
stringone= 'O'
puts "Answer:=>  Try NO:#{c}*"
string2 = gets.chomp
if (string1==string2)
puts "_Good Work correct spaling_"
 break
 elsif (stringone == string2)
puts "_Good Work correct spaling_"
break
else
 puts "-Worng spaling-"
end
c -=1
if c<1
puts " *Tries Over Game End* "
exit
end
end
puts "   *Round Over*  "
puts '==================='
puts "--ROUND Four press-- => (4)"
four=gets.to_i
puts '==================='
puts "Question:=>  ( G ? M E )"
5.times do
string1 = 'a' 
stringone = "A"
puts "Answer:=>  Try NO:#{c}*"
string2 = gets.chomp
if (string1==string2)
puts "_Good Work correct spaling_"
 break
 elsif (stringone == string2)
puts "_Good Work correct spaling_"
  break
else
 puts "-Worng spaling-"
end
c -=1
if c<1
puts " *Tries Over Game End* "
exit
end
end
puts "**Yahoo Congragualtion complete All Round**"