Ruby 红宝石猜字游戏

Ruby 红宝石猜字游戏,ruby,class,methods,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_

我是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 
  • 与此相反,空格不在正确的位置,看起来是这样的(运行我的代码的实际结果):

  • 当用户猜到这个词时,它不会放上我的“祝贺”语句,从而结束游戏

    我还坚持我的“输”方法。我不知道如何修复该方法,以便在用户尝试用尽并打印“丢失”语句时游戏结束


    谢谢你的帮助

    对于第一个问题,您的
    @display\u word
    如下所示:

    [0] = '_' # For h
    [1] = ' '
    [2] = '_' # For e
    [3] = ' '
    ...
    
    例如,当您猜测
    'e'
    时,您会:

    @display_word[index] = word_guess
    
    其中,
    索引
    等于1,
    “hello”
    中的第二个字符,因此您可以看到它不会写入
    @display\u word
    中的
    'e'
    索引

    对于第二个问题,有很多方法可以解决。例如,我会使用
    @attempts\u resisting
    从10左右的值开始,然后使用现有代码:

    if !@word.include? (word_guess)
      @attempts_remaining -= 1  # Count failure to guess
      puts "Nope, guess again..."
    end
    
    然后:

    最后,调整
    直到
    循环终止条件:

    until game.win? or game.lose?
    

    可以删除对
    win?
    lose?
    的现有调用。

    我认为您使输出过于复杂。我会在数组中跟踪单词和猜测。我不想使用
    display\u 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类处理。

    (WORD MISSING GAME)

    放置“” “总回合” 放置“” 放置“=>[第一轮,第二轮,第三轮,第四轮]” 放置“” 放置“=>[总共5次尝试]” 放置“” 一 两个=“” 三个=“” 四个=“” 放置“” puts”--第一轮按--=>(1) 一=到达 放入“==============================” 提出“问题:=>(K?N G)” 放置“” c=5 5.5倍 string1='i' stringone=“I” 放置“回答:=>尝试否:{c}” string2=get.chomp if(string1==string2) 将“良好的工作纠正垃圾” 打破 elsif(stringone==string2) 将“良好的工作纠正垃圾” 打破 其他的 将“-Worng spaling…” 结束 c-=1 结束 把“四舍五入”
    真是太感谢你了!我确实得到了成功的方法。。。然而,即使它结束了,我仍然会收到错误消息“unexpected return”,这是您输入的最后一段代码。另外,在更改我的当前代码时@包括?部分当我运行代码时,如果我输入了一个“不正确”的字母,“nope guess Area”会打印5次,它会立即以“you lose”结束游戏。我不知道为什么会发生这样的事。忽略我最后的评论!我在do块内定义了它,一旦我在do块外定义了它,它就会停止打印很多次,让我继续猜测。第一条评论的问题我还没有弄清楚。@brittloops,我的错误!我已经更改了答案,以获得更好的设置
    ,直到
    条件。
    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 game.win? or game.lose?
    
    class WordGame
      def initialize(word)
        @word = word.downcase.chars
        @guesses = ["_"] * @word.size
      end
    
      def to_s
        @guesses.join " "
      end
    
    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**"