0

所以我最近一直在学习 ruby​​,我正在为练习目的编写这段代码,但我似乎无法解决这个问题,任何帮助将不胜感激。

这是我遵循的准则:

clear the screen
greet the player
explain the rules of the game to the player
generate a random number between 0 and x (x being a variable that can be set to any integer)
allow the player n number of guesses (n being a variable)
keep track of the guess history
don't count repeated guesses against the player
congratulate the player when he/she guesses correctly
alert the player when there is only one guess remaining
print the guess history at the end of the game
count number of games won IN A ROW
count number of games won in total
congratulate the play on 3 games won IN A ROW
ask if the player wants to play again
thank the player for playing games if the number of games played is greater than 2

请告诉我这是正在进行的工作,我还没有完成所有指南,但是我的问题是其中的一个特定部分。

这是代码:

guess = Array.new
puts guess.class
def ask()
      puts "Please answer in a 'y' or 'n' only!"
      puts "Would like to play again?"
end


def guess_check_against()
       g = guess.last
unless guess.include?(g) != guess
       count+=1
else
     puts "sorry you have guessed that number before, Guess Again: "
       guess << gets.to_i
       count+=1
end
end

puts "what is your name?"
user= gets.chomp

puts "Hello #{user}!!"
max_user_attempts = 4
@attempt_counter = 0
directions = "\nLets play a guessing game! You have
#{max_user_attempts.to_s} guesses before you lose."
print directions

g = guess.last
win = 0
count = 0
play = true

while play == true

  puts "Please tell me the max value of the random number: "
    max= gets.to_i
    num= rand(max)
  puts "Ok. The random number is generated between 1 and " + max.to_s + "."
  puts "Make your guess: "
      guess << gets.to_i
          guess_check_against()
          @attempt_counter+=1
    while guess != num && play != false

  if g > num && @attempt_counter < max_user_attempts
    print "That's too high. Guess again: "
    guess << gets.to_i
    guess_check_against()
    @attempt_counter+=1
  elsif g < num && @attempt_counter < max_user_attempts
    print "That's too low. Guess again: "
    guess << gets.to_i
    guess_check_against()
    count+=1
    @attempt_counter+=1
  else
    break
  end

end
 if @attempts_counter >= max_user_attemtps
    puts "Sorry! you lost! Try Again"
      break
 else @attempts_counter <= max_user_attempts      
    puts "Correct! You guessed the answer in " + count.to_s + " tries!"
      win+=1
  end
    if win >= 3
      puts "Congratulation! you have #{win} number of  games in a row"
        ask()
        answer = gets.chomp!

    elsif win < 3
        ask()
        answer = gets.chomp!
    else
        break
      end                   

    if answer == 'n'
            play = false
       break
    end

    if  answer == 'y'
            play = true
            count = 0
    end
 end

 puts "Ok. Goodbye!!"

这是我尝试运行此程序时不断收到的错误:

guessing_game.rb:12:in `guess_check_against': undefined local variable or method `guess' for main:Object (NameError)
    from guessing_game.rb:45:in `<main>'

当我尝试使用 irb 运行相同的场景时,它完全可以正常工作。

我无法弄清楚我做错了什么,请帮忙!

4

1 回答 1

0

方法定义

def guess_check_against()
   g = guess.last
  ...
end

有自己的作用域和局部变量

guess = Array.new

您在它之外定义的,在方法定义中是不可访问的。guess未在方法定义中定义。您可以更改代码,以便该方法将其作为参数,并且它将变得可访问。

于 2013-09-03T00:38:05.920 回答