Ruby脚本崩溃

Ruby脚本崩溃,ruby,Ruby,我有下面的代码,我正试图从这本Ruby书中学习,它不断崩溃,我花了几个小时试图修复。如果有人知道为什么会这样,请告诉我。我不喜欢Ruby #定义自定义类--------------------------------------------------- #Define a class representing the console window class Screen def cls #Define a method that clears the display area

我有下面的代码,我正试图从这本Ruby书中学习,它不断崩溃,我花了几个小时试图修复。如果有人知道为什么会这样,请告诉我。我不喜欢Ruby

#定义自定义类---------------------------------------------------

#Define a class representing the console window
class Screen

  def cls  #Define a method that clears the display area
    puts ("\n" * 25)  #Scroll the screen 25 times
    puts "\a"  #Make a little noise to get the player's attention
  end

  def pause    #Define a method that pauses the display area
    STDIN.gets  #Execute the STDIN class's gets method to pause script
                #execution until the player presses the enter key
  end

end

#Define a class representing the Ruby Number Guessing Game
class Game

  #This method displays the game's opening screen
  def display_greeting

    Console_Screen.cls  #Clear the display area

    #Display welcome message
    print "\t\t  Welcome to the Ruby Number Guessing Game!" +
    "\n\n\n\n\n\n\n\n\n\n\n\n\n\nPress Enter to " +
               "continue."

  Console_Screen.pause       #Pause the game

  end

  #Define a method to be used to present game instructions
  def display_instructions

    Console_Screen.cls       #Clear the display area
    puts "INSTRUCTIONS:\n\n"  #Display a heading

    #Display the game's instructions
    puts "This game randomly generates a number from 1 to 100 and" 
    puts "challenges you to identify it in as few guesses as possible."
    puts "After each guess, the game will analyze your input and provide"
    puts "you with feedback. You may take as many turns as you need in" 
    puts "order to guess the game's secret number.\n\n"
    puts "Game will stop if you have guessed 10 times.\n\n\n"
    puts "Good luck!\n\n\n\n\n\n\n\n\n"
    print "Press Enter to continue."

    Console_Screen.pause       #Pause the game

  end

  #Define a method that generates the game's secret number
  def generate_number

    #Generate and return a random number between 1 and 100
    return randomNo = 1 + rand(1000)

  end

  #Define a method to be used control game play
  def play_game



    #Call on the generate_number method in order to get a random number
    number = generate_number  

    #Loop until the player inputs a valid answer
    loop do

      Console_Screen.cls       #Clear the display area

        if answer == "c"

            print "Game count : #{$gameCount}"

        end
      #Prompt the player to make a guess
      print "\nEnter your guess and press the Enter key: "

      reply = STDIN.gets  #Collect the player's answer
      reply.chop!         #Remove the end of line character
      reply = reply.to_i  #Convert the player's guess to an integer


          #Validate the player's input only allowing guesses between 1 and 100
          if reply < 1 or reply > 1000 then
            redo  #Redo the current iteration of the loop
          end

          #Analyze the player's guess to determine if it is correct
          if reply == number then    #The player's guess was correct
            Console_Screen.cls        #Clear the display area
            print "You have guessed the number! Press enter to continue."
            Console_Screen.pause      #Pause the game
            break  #Exit loop
          elsif reply < number then  #The player's guess was too low
            Console_Screen.cls        #Clear the display area
            print "Your guess is too low! ( valid range: 1 - 1000) Press Enter to continue."
            Console_Screen.pause      #Pause the game
          elsif reply > number then  #The player's guess was too high
            Console_Screen.cls        #Clear the display area
            print "Your guess is too high! ( valid range: 1 - 1000) Press Enter to continue."
            Console_Screen.pause      #Pause the game
          end
          $noOfGuesses +=1

      break if $noOfGuesses > 10

    end

  end

  #This method displays the information about the Ruby Number Guessing Game
  def display_credits

    Console_Screen.cls  #Clear the display area

    #Thank the player and display game information
    puts "\t\tThank you playing the Ruby Number Guessing Game.\n\n\n\n"
    puts "\n\t\t\t Developed by Jerry Lee Ford, Jr.\n\n"
    puts "\t\t\t\t  Copyright 2010\n\n"
    puts "\t\t\tURL: http://www.tech-publishing.com\n\n\n\n\n\n\n\n\n\n"

  end

end


# Main Script Logic -------------------------------------------------------

Console_Screen = Screen.new  #Instantiate a new Screen object
SQ = Game.new                #Instantiate a new Game object

#Execute the Game class's display_greeting method
SQ.display_greeting

answer = ""

$gameCount = 0
$noOfGuesses = 0
$totalNoOfGuesses = 0
$avgNoOfGuesses = 0


#Loop until the player enters y or n and do not accept any other input
loop do

  Console_Screen.cls  #Clear the display area

  #Prompt the player for permission to start the game
  print "Are you ready to play the Ruby Number Guessing Game? (y/n): "

  answer = STDIN.gets  #Collect the player's response
  answer.chop!  #Remove any extra characters appended to the string

  #Terminate the loop if valid input was provided
  break if answer == "y" || answer == "n" || answer == "c"  #Exit loop

end 

#Analyze the player's input
if answer == "n"  #See if the player elected not to take the game

  Console_Screen.cls  #Clear the display area

  #Invite the player to return and play the game some other time
  puts "Okay, perhaps another time.\n\n"

else  #The player wants to play the game

    #Execute the Game class's display_instructions method
    SQ.display_instructions

  loop do

    $gameCount+=1

    #Execute the Game class's play_game method 
    SQ.play_game

    $totalNoOfGuesses = $noOfGuesses * $gameCount

    $avgNoOfGuesses = $totalNoOfGuesses / $noOfGuesses
    print "The total number of guesses was  #{$totalNoOfGuesses}"
    print "The average number of guesses was  #{$avgNoOfGuesses}"
    Console_Screen.pause      #Pause the game
    print "Press Enter to continue"

    Console_Screen.cls  #Clear the display area

    #Prompt the player for permission start a new round of play
    print "Would you like to play again? (y/n): "

    playAgain = STDIN.gets  #Collect the player's response
    playAgain.chop!  #Remove any extra characters appended to the string

    break if playAgain == "n"  #Exit loop

  end

  #Call upon the Game class's determine_credits method in order to thank
  #the player for playing the game and to display game information
  SQ.display_credits

end
#定义一个表示控制台窗口的类
类屏幕
def cls#定义一种清除显示区域的方法
放置(“\n”*25)#滚动屏幕25次
放置“\a”#发出一点声音以引起玩家的注意
结束
def pause#定义暂停显示区域的方法
STDIN.gets#执行STDIN类的get方法以暂停脚本
#执行,直到玩家按下回车键
结束
结束
#定义一个表示Ruby数字猜测游戏的类
班级游戏
#此方法显示游戏的开始屏幕
显示问候语
Console_Screen.cls#清除显示区域
#显示欢迎信息
打印“\t\t欢迎参加Ruby数字猜测游戏!”+
“\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n按回车键”+
“继续。”
控制台_屏幕。暂停#暂停游戏
结束
#定义用于呈现游戏说明的方法
def显示单元说明
Console_Screen.cls#清除显示区域
放置“说明:\n\n”#显示标题
#显示游戏说明
放置“此游戏随机生成一个从1到100的数字并”
将“挑战您在尽可能少的猜测中识别它。”
放置“每次猜测后,游戏将分析您的输入并提供”
将“给你反馈。你可以根据需要轮流参加”
放置“猜测游戏秘密号码的顺序。\n\n”
puts“如果您猜了10次,游戏将停止。\n\n\n”
放入“好运!\n\n\n\n\n\n\n\n\n\n\n”
打印“按回车键继续”
控制台_屏幕。暂停#暂停游戏
结束
#定义生成游戏秘密号码的方法
def生成_编号
#生成并返回一个介于1和100之间的随机数
返回随机数=1+兰德(1000)
结束
#定义用于控制游戏的方法
玩游戏
#调用generate_number方法以获得一个随机数
编号=生成编号
#循环直到玩家输入一个有效的答案
环道
Console_Screen.cls#清除显示区域
如果答案==“c”
打印“游戏计数:#{$gameCount}”
结束
#提示玩家进行猜测
打印“\n输入您的猜测并按Enter键:”
reply=STDIN.get#收集玩家的答案
回答,砍#删除行尾字符
reply=reply.to#将玩家的猜测转换为整数
#验证玩家的输入,只允许在1到100之间进行猜测
如果回复<1或回复>1000,则
重做#重做循环的当前迭代
结束
#分析玩家的猜测以确定其是否正确
如果reply==number,那么#玩家的猜测是正确的
Console_Screen.cls#清除显示区域
打印“您已经猜到了数字!按enter键继续。”
控制台_屏幕。暂停#暂停游戏
中断#退出循环
elsif回复<数字那么#玩家的猜测太低了
Console_Screen.cls#清除显示区域
打印“您的猜测太低!(有效范围:1-1000)按Enter键继续。”
控制台_屏幕。暂停#暂停游戏
埃尔西夫回答>数字然后#玩家的猜测太高了
Console_Screen.cls#清除显示区域
打印“您的猜测太高了!(有效范围:1-1000)按Enter键继续。”
控制台_屏幕。暂停#暂停游戏
结束
$noofguesss+=1
如果$noofguesss>10,则中断
结束
结束
#此方法显示有关Ruby数字猜测游戏的信息
显示积分
Console_Screen.cls#清除显示区域
#感谢玩家并显示游戏信息
放置“\t\t感谢您玩Ruby数字猜测游戏。\n\n\n\n”
放置“\n\t\t\t由Jerry Lee Ford,Jr.开发。\n\n”
放置“\t\t\t\t版权2010\n\n”
放置“\t\t\tURL:http://www.tech-publishing.com\n\n\n\n\n\n\n\n\n\n\n“
结束
结束
#主脚本逻辑-------------------------------------------------------
Console_Screen=Screen.new#实例化一个新的Screen对象
SQ=Game.new#实例化一个新的游戏对象
#执行游戏类的display_问候方法
SQ.display_问候语
答案=”“
$gameCount=0
$noOfGuesses=0
$TotalNoofguesss=0
$avgnoofguesss=0
#循环,直到玩家输入y或n,并且不接受任何其他输入
环道
Console_Screen.cls#清除显示区域
#提示玩家允许开始游戏
打印“您准备好玩Ruby数字猜测游戏了吗?(y/n):”
answer=STDIN.get#收集玩家的响应
回答,砍#删除附加到字符串的所有额外字符
#如果提供了有效的输入,则终止循环
如果答案==“y”|答案==“n”|答案==“c”#退出循环,则中断
结束
#分析玩家的输入
如果答案==“n”#看看玩家是否选择不参加比赛
Console_Screen.cls#清除显示区域
#邀请玩家下次再来玩游戏
放入“好的,也许下次吧。\n\n”
否则,玩家想玩这个游戏
#执行游戏类的display_instructions方法
SQ.display_指令
环道
$gameCount+=1
#执行Game类的play_Game方法
玩游戏
$TotalNoofguesss=$Noofguesss*$gameCount
$avgnofguesss=$totalnoofguesss/$noofguesss
打印“猜测总数为#{$totalNoofguesss}”
打印“平均猜测次数为#{$avgnoofguesss}”
安慰
script-not-working.rb:74:in `block in play_game': undefined local variable or method `answer' for #<Game:0x0000000180f9c0> (NameError)
    from script-not-working.rb:70:in `loop'
    from script-not-working.rb:70:in `play_game'
    from script-not-working.rb:181:in `block in <main>'
    from script-not-working.rb:176:in `loop'
    from script-not-working.rb:176:in `<main>'
#Define a class representing the console window
class Screen

  def cls  #Define a method that clears the display area
    puts ("\n" * 25)  #Scroll the screen 25 times
    puts "\a"  #Make a little noise to get the player's attention
  end

  def pause    #Define a method that pauses the display area
    STDIN.gets  #Execute the STDIN class's gets method to pause script
                #execution until the player presses the enter key
  end

end

#Define a class representing the Ruby Number Guessing Game
class Game

  #This method displays the game's opening screen
  def display_greeting

    Console_Screen.cls  #Clear the display area

    #Display welcome message
    print "\t\t  Welcome to the Ruby Number Guessing Game!" +
    "\n\n\n\n\n\n\n\n\n\n\n\n\n\nPress Enter to " +
               "continue."

  Console_Screen.pause       #Pause the game

  end

  #Define a method to be used to present game instructions
  def display_instructions

    Console_Screen.cls       #Clear the display area
    puts "INSTRUCTIONS:\n\n"  #Display a heading

    #Display the game's instructions
    puts "This game randomly generates a number from 1 to 100 and" 
    puts "challenges you to identify it in as few guesses as possible."
    puts "After each guess, the game will analyze your input and provide"
    puts "you with feedback. You may take as many turns as you need in" 
    puts "order to guess the game's secret number.\n\n"
    puts "Game will stop if you have guessed 10 times.\n\n\n"
    puts "Good luck!\n\n\n\n\n\n\n\n\n"
    print "Press Enter to continue."

    Console_Screen.pause       #Pause the game

  end

  #Define a method that generates the game's secret number
  def generate_number

    #Generate and return a random number between 1 and 100
    return randomNo = 1 + rand(1000)

  end

  #Define a method to be used control game play
  def play_game



    #Call on the generate_number method in order to get a random number
    number = generate_number  

    #Loop until the player inputs a valid answer
    loop do

      Console_Screen.cls       #Clear the display area

        if $answer == "c"

            print "Game count : #{$gameCount}"

        end
      #Prompt the player to make a guess
      print "\nEnter your guess and press the Enter key: "

      reply = STDIN.gets  #Collect the player's answer
      reply.chop!         #Remove the end of line character
      reply = reply.to_i  #Convert the player's guess to an integer


          #Validate the player's input only allowing guesses between 1 and 100
          if reply < 1 or reply > 1000 then
            redo  #Redo the current iteration of the loop
          end

          #Analyze the player's guess to determine if it is correct
          if reply == number then    #The player's guess was correct
            Console_Screen.cls        #Clear the display area
            print "You have guessed the number! Press enter to continue."
            Console_Screen.pause      #Pause the game
            break  #Exit loop
          elsif reply < number then  #The player's guess was too low
            Console_Screen.cls        #Clear the display area
            print "Your guess is too low! ( valid range: 1 - 1000) Press Enter to continue."
            Console_Screen.pause      #Pause the game
          elsif reply > number then  #The player's guess was too high
            Console_Screen.cls        #Clear the display area
            print "Your guess is too high! ( valid range: 1 - 1000) Press Enter to continue."
            Console_Screen.pause      #Pause the game
          end
          $noOfGuesses +=1

      break if $noOfGuesses > 10

    end

  end

  #This method displays the information about the Ruby Number Guessing Game
  def display_credits

    Console_Screen.cls  #Clear the display area

    #Thank the player and display game information
    puts "\t\tThank you playing the Ruby Number Guessing Game.\n\n\n\n"
    puts "\n\t\t\t Developed by Jerry Lee Ford, Jr.\n\n"
    puts "\t\t\t\t  Copyright 2010\n\n"
    puts "\t\t\tURL: http://www.tech-publishing.com\n\n\n\n\n\n\n\n\n\n"

  end

end


# Main Script Logic -------------------------------------------------------

Console_Screen = Screen.new  #Instantiate a new Screen object
SQ = Game.new                #Instantiate a new Game object

#Execute the Game class's display_greeting method
SQ.display_greeting

$answer = ""

$gameCount = 0
$noOfGuesses = 0
$totalNoOfGuesses = 0
$avgNoOfGuesses = 0


#Loop until the player enters y or n and do not accept any other input
loop do

  Console_Screen.cls  #Clear the display area

  #Prompt the player for permission to start the game
  print "Are you ready to play the Ruby Number Guessing Game? (y/n): "

  $answer = STDIN.gets  #Collect the player's response
  $answer.chop!  #Remove any extra characters appended to the string

  #Terminate the loop if valid input was provided
  break if $answer == "y" || $answer == "n" || $answer == "c"  #Exit loop

end 

#Analyze the player's input
if $answer == "n"  #See if the player elected not to take the game

  Console_Screen.cls  #Clear the display area

  #Invite the player to return and play the game some other time
  puts "Okay, perhaps another time.\n\n"

else  #The player wants to play the game

    #Execute the Game class's display_instructions method
    SQ.display_instructions

  loop do

    $gameCount+=1

    #Execute the Game class's play_game method 
    SQ.play_game

    $totalNoOfGuesses = $noOfGuesses * $gameCount

    $avgNoOfGuesses = $totalNoOfGuesses / $noOfGuesses
    print "The total number of guesses was  #{$totalNoOfGuesses}"
    print "The average number of guesses was  #{$avgNoOfGuesses}"
    Console_Screen.pause      #Pause the game
    print "Press Enter to continue"

    Console_Screen.cls  #Clear the display area

    #Prompt the player for permission start a new round of play
    print "Would you like to play again? (y/n): "

    playAgain = STDIN.gets  #Collect the player's response
    playAgain.chop!  #Remove any extra characters appended to the string

    break if playAgain == "n"  #Exit loop

  end

  #Call upon the Game class's determine_credits method in order to thank
  #the player for playing the game and to display game information
  SQ.display_credits

end
  SQ.play_game answer
  #Define a class representing the console window
   class Screen

   def cls  #Define a method that clears the display area
      puts ("\n" * 25)  #Scroll the screen 25 times
      puts "\a"  #Make a little noise to get the player's attention
   end

   def pause    #Define a method that pauses the display area
     STDIN.gets  #Execute the STDIN class's gets method to pause script
            #execution until the player presses the enter key
   end

end

#Define a class representing the Ruby Number Guessing Game
class Game

     #This method displays the game's opening screen
     def display_greeting

     Console_Screen.cls  #Clear the display area

     #Display welcome message
     print "\t\t  Welcome to the Ruby Number Guessing Game!" +
         "\n\n\n\n\n\n\n\n\n\n\n\n\n\nPress Enter to " +
           "continue."

     Console_Screen.pause       #Pause the game

  end

   #Define a method to be used to present game instructions
   def display_instructions

   Console_Screen.cls       #Clear the display area
    puts "INSTRUCTIONS:\n\n"  #Display a heading

   #Display the game's instructions
    puts "This game randomly generates a number from 1 to 100 and" 
    puts "challenges you to identify it in as few guesses as possible."
    puts "After each guess, the game will analyze your input and     provide"
    puts "you with feedback. You may take as many turns as you need in" 
    puts "order to guess the game's secret number.\n\n"
    puts "Game will stop if you have guessed 10 times.\n\n\n"
    puts "Good luck!\n\n\n\n\n\n\n\n\n"
    print "Press Enter to continue."

   Console_Screen.pause       #Pause the game

   end

  #Define a method that generates the game's secret number
  def generate_number

    #Generate and return a random number between 1 and 100
    return randomNo = 1 + rand(1000)

  end

   #Define a method to be used control game play
   def play_game answer



     #Call on the generate_number method in order to get a random number
     number = generate_number  

#Loop until the player inputs a valid answer
loop do

  Console_Screen.cls       #Clear the display area

    if answer == "c"

        print "Game count : #{$gameCount}"

    end
  #Prompt the player to make a guess
  print "\nEnter your guess and press the Enter key: "

  reply = STDIN.gets  #Collect the player's answer
  reply.chop!         #Remove the end of line character
  reply = reply.to_i  #Convert the player's guess to an integer


      #Validate the player's input only allowing guesses between 1 and 100
      if reply < 1 or reply > 1000 then
        redo  #Redo the current iteration of the loop
      end

      #Analyze the player's guess to determine if it is correct
      if reply == number then    #The player's guess was correct
        Console_Screen.cls        #Clear the display area
        print "You have guessed the number! Press enter to continue."
        Console_Screen.pause      #Pause the game
        break  #Exit loop
      elsif reply < number then  #The player's guess was too low
        Console_Screen.cls        #Clear the display area
        print "Your guess is too low! ( valid range: 1 - 1000) Press Enter to continue."
        Console_Screen.pause      #Pause the game
      elsif reply > number then  #The player's guess was too high
        Console_Screen.cls        #Clear the display area
        print "Your guess is too high! ( valid range: 1 - 1000) Press Enter to continue."
        Console_Screen.pause      #Pause the game
      end
      $noOfGuesses +=1

  break if $noOfGuesses > 10

    end

end

     #This method displays the information about the Ruby Number Guessing Game
    def display_credits

       Console_Screen.cls  #Clear the display area

     #Thank the player and display game information
      puts "\t\tThank you playing the Ruby Number Guessing Game.\n\n\n\n"
      puts "\n\t\t\t Developed by Jerry Lee Ford, Jr.\n\n"
      puts "\t\t\t\t  Copyright 2010\n\n"
      puts "\t\t\tURL: http://www.tech-publishing.com\n\n\n\n\n\n\n\n\n\n"

    end

  end


     # Main Script Logic -------------------------------------------------------

     Console_Screen = Screen.new  #Instantiate a new Screen object
      SQ = Game.new                #Instantiate a new Game object

       #Execute the Game class's display_greeting method
      SQ.display_greeting

      answer = ""

       $gameCount = 0
      $noOfGuesses = 0
       $totalNoOfGuesses = 0
       $avgNoOfGuesses = 0


    #Loop until the player enters y or n and do not accept any other input
  loop do

     Console_Screen.cls  #Clear the display area

      #Prompt the player for permission to start the game
    print "Are you ready to play the Ruby Number Guessing Game? (y/n): "

     answer = STDIN.gets  #Collect the player's response
     answer.chop!  #Remove any extra characters appended to the string

    #Terminate the loop if valid input was provided
    break if answer == "y" || answer == "n" || answer == "c"  #Exit loop

   end 

     #Analyze the player's input
     if answer == "n"  #See if the player elected not to take the game

    Console_Screen.cls  #Clear the display area

    #Invite the player to return and play the game some other time
   puts "Okay, perhaps another time.\n\n"

  else  #The player wants to play the game

  #Execute the Game class's display_instructions method
  SQ.display_instructions

    loop do

    $gameCount+=1

    #Execute the Game class's play_game method 
     SQ.play_game answer

      $totalNoOfGuesses = $noOfGuesses * $gameCount

       $avgNoOfGuesses = $totalNoOfGuesses / $noOfGuesses
      print "The total number of guesses was  #{$totalNoOfGuesses}"
      print "The average number of guesses was  #{$avgNoOfGuesses}"
       Console_Screen.pause      #Pause the game
      print "Press Enter to continue"

      Console_Screen.cls  #Clear the display area

     #Prompt the player for permission start a new round of play
      print "Would you like to play again? (y/n): "

      playAgain = STDIN.gets  #Collect the player's response
      playAgain.chop!  #Remove any extra characters appended to the string

       break if playAgain == "n"  #Exit loop
     end


    #Call upon the Game class's determine_credits method in order to thank
     #the player for playing the game and to display game information
     SQ.display_credits
  end