猜猜游戏Ruby

时间:2013-09-03 00:26:20

标签: ruby arrays sorting multidimensional-array

所以我最近一直在学习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运行相同的场景时,它完全正常。

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

1 个答案:

答案 0 :(得分:0)

方法定义

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

有自己的范围和局部变量

guess = Array.new
您在其外部定义的

在方法定义中无法访问。 guess未在方法定义中定义。您可以更改代码,以便该方法将其作为参数,并且可以访问它。