Ruby - 循环没有破坏

时间:2015-05-22 12:07:12

标签: ruby

我正在尝试创建一个秘密数字游戏:

  1. 随机选择1到10之间的数字
  2. 玩家有3次尝试猜测密码
  3. 如果玩家在所有尝试中猜到错误,或猜测正确的号码,他们会被提示是否要重播游戏。
  4. 我的问题是:

    1. 当playerInput == secret_number时,“尝试”循环不会中断。它只会在使用所有3次尝试时中断。
    2. 当所有3次猜测都用尽时,无法打印消息“抱歉,但您没有更多尝试”。
    3. 我认为我的数学也是假的......尝试
    4. 代码如下。先谢谢你们!

      puts "Welcome to the Secret Number Game! Please tell me your name"
      player_name = gets.strip
      
      puts "Welcome #{player_name}!"
      
      puts "Guess a number between 1 - 10. You only have 3 attempts!"
      
      restart = true
      
      def guess_check( playerInput, secret_number, attempts )
          if playerInput > secret_number
              puts "Too high! try again!"
          elsif playerInput < secret_number
              puts "Too low! try again!"
          elsif
              playerInput == secret_number
              puts "Congratulations, you guessed the secret number! [#{secret_number}]"
          elsif 
              attempts == 0 
              puts "Sorry, you're out of guesses, the secret number is [#{secret_number}]"
          else
              puts secret_number
          end
      end
      
      
      while restart
      
          guesses = []
          attempts = 3
          secret_number = 1 + rand(10)
      
          while attempts
      
              attempts = attempts - 1
              puts "Guess the secret number, you have #{attempts} tries left"
              playerInput = gets.to_i
              guesses.push( playerInput )
              guess_check( playerInput, secret_number, attempts )
              puts "You've guessed #{guesses}"
              break if playerInput == secret_number || break if attempts == 0
      
          end
      
          puts "Do you want to play again? (y/n)"
          answer = gets.strip
          restart = false if answer == "n"
      
      end
      

3 个答案:

答案 0 :(得分:3)

这不像你想要的那样工作:

break if playerInput == secret_number || break if attempts == 0

如果你突破它,它的内容如下:

if attempts == 0
  if playerInput == secret_number or break
    break
  end
end

只有break if attempts == 0,并且只有因为嵌套if语句中的第一个条件失败或者它传递了它。这应该使它像你想要的那样工作:

break if playerInput == secret_number || attempts == 0

它不打印您用完的猜测消息,因为首先执行了正确或错误答案的条件。要解决此问题,您的条件需要按以下顺序进行:

def guess_check( playerInput, secret_number, attempts )
  if playerInput == secret_number
    puts "Congratulations, you guessed the secret number! [#{secret_number}]"
  if attempts == 0 
    puts "Sorry, you're out of guesses, the secret number is [#{secret_number}]"
  elsif playerInput > secret_number
    puts "Too high! try again!"
  elsif playerInput < secret_number
    puts "Too low! try again!"
  #else   <= this can actually be removed, the if statement will never get this far
    #puts secret_number
  end
end

我确信有更好的方式来表达它,但如果首先评估语句,您希望最不可能/最高优先级。在这种情况下,用户的猜测保证至少大于或小于秘密数字,因此首先评估这些,并且它永远不会真正下降到最后。你想评估正确的猜测(1/10),然后猜测(3/10),然后大于(1/2)/小于(1/2)。 else永远不会触发,因为上述内容涵盖了所有可能性。

答案 1 :(得分:2)

这里只是一个简单的改变:

break if playerInput == secret_number || attempts == 0

您的||条件是有效的语法,但会导致不必要的行为。

答案 2 :(得分:0)

我重构你的代码。

puts "Welcome to the Secret Number Game! Please tell me your name"
player_name = gets.strip
puts "Welcome #{player_name}!"
restart = true

def guess_check( playerInput, secret_number)
    if playerInput > secret_number
        puts "Too high! try again!"
    elsif playerInput < secret_number
        puts "Too low! try again!"
    end
end


while restart
    guesses = []
    attempts = 3
    secret_number = 1 + rand(10)

    puts "Guess a number between 1 - 10. You only have 3 attempts!"
    while attempts

        playerInput = gets.to_i
        guesses.push( playerInput )
        # once guessed, should exit current loop.
        if playerInput == secret_number
           puts "Congratulations, you guessed the secret number! [#{secret_number}]"
           break;
        end
        puts "You've guessed #{guesses}"
        #if not guessed, then should check the number is low or high.
        guess_check(playerInput, secret_number)
        attempts = attempts - 1
        #if out of guesses, exit from current loop
        if attempts == 0
           puts "Sorry, you're out of guesses, the secret number is [#{secret_number}]"
           break;
        else
           puts "Guess the secret number, you have #{attempts} tries left"
        end
    end

    puts "Do you want to play again? (y/n)"
    answer = gets.strip
    restart = false if answer == "n"

end