SHARE
TWEET

Untitled

a guest Mar 21st, 2017 69 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #---------
  2. # name: NumberGuess.rb
  3. # Solution A
  4. #
  5. #----------------
  6. class Screen
  7.  
  8.  def cls
  9.    puts ("\n" * 30)
  10.    puts "\a"
  11.  end
  12.  def pause
  13.     STDIN.gets
  14.  end
  15. end
  16.  
  17. class Game
  18.     $gameNumber = 0 #class variable to store the no. of games
  19.    
  20.     def display_instruction
  21.         puts "Instructions:\n\n"
  22.         puts "This game randomly generates a number from 1 to 1000 and "
  23.         puts "challenges you to identify it in as few guesses and possible "
  24.         puts "Numbers between 1 and 1000 are valid inputs only"
  25.     end
  26.    
  27.     def display_greeting
  28.         Console_Screen.cls
  29.         print "\t\t Welcome to the Ruby Number Guessing Game!"
  30.         print "\n\n\n\n\n\n\n\n\n\n\n\n\ Press Enter to "
  31.         print "continue."
  32.         Console_Screen.pause
  33.     end
  34.  
  35.     def generate_number
  36.         return randomNo = 1 + rand(1000)
  37.     end
  38.  
  39.     def play_game
  40.         number = generate_number
  41.          $gameNumber +=1 #Increment the variable by 1 everytime a no. is generated
  42.          $guesses = 0
  43.         loop do
  44.             #Console_Screen.cls
  45.             print "\nEnter your guess and press the enter key: "
  46.             $guesses +=1
  47.             reply = STDIN.gets
  48.             reply.chop!
  49.             reply = reply.to_i
  50.            
  51.             if reply < 1 or reply > 1000 then
  52.                 puts "\n\n\n\n\n\n\n"
  53.                 puts "Invalid Input"
  54.                 puts "\n\n\n\n"
  55.                 puts "Valid inputs are numbers between 1 and 1000 only"
  56.                 redo # redo the current iteration of the loop
  57.             end
  58.            
  59.             if reply == number then
  60.                 Console_Screen.cls
  61.                 print "You have guessed the number! Press Enter to continue."
  62.                 Console_Screen.pause
  63.                 break
  64.             elsif reply < number then
  65.                 Console_Screen.cls
  66.                 print "Your guess is too low! Press enter to continue."
  67.                 Console_Screen.pause
  68.             elsif reply > number then
  69.                 Console_Screen.cls
  70.                 print "Your guess is too high! Press enter to continue."
  71.                 Console_Screen.pause
  72.             end
  73.         end
  74.              
  75.     end
  76.  
  77.     def display_credits
  78.         Console_Screen.cls
  79.         puts "Thanks you for playing the Number Guessing Game #{$gameNumber} times with #{$guesses} guesses!!" # Display number of times game playedon the screen
  80.         puts "Average guesses per game #{$guesses.to_i/$gameNumber.to_i}"
  81.     end
  82.  
  83.     $noRight = 0
  84.  
  85.     Console_Screen = Screen.new
  86.  
  87.     SQ = Game.new
  88.  
  89.     SQ.display_greeting
  90.  
  91.     answer = ""
  92.  
  93.     loop do
  94.         Console_Screen.cls
  95.         print "Are you ready to play the Ruby Number Guessing Game? (y/n): "
  96.         answer = STDIN.gets
  97.         answer.chop!
  98.         break if answer == "y" || answer == "n"
  99.     end
  100.  
  101.     if answer == "n"
  102.         Console_Screen.cls
  103.         puts "Perhaps another time.\n\n"
  104.     else
  105.         Console_Screen.cls
  106.         SQ.display_instruction
  107.         print "\n\n\n\n\n"
  108.         Console_Screen.pause
  109.        
  110.         loop do
  111.             SQ.play_game
  112.  
  113.             Console_Screen.cls
  114.  
  115.             print "Would you like to play again? (y/n): "
  116.  
  117.             playAgain = STDIN.gets
  118.             playAgain.chop!
  119.  
  120.             break if playAgain == "n"
  121.         end
  122.  
  123.         SQ.display_credits
  124.        
  125.  
  126.     end
  127.   end
RAW Paste Data
Pastebin PRO Summer Special!
Get 40% OFF on Pastebin PRO accounts!
Top