Skip to content

Instantly share code, notes, and snippets.

@cjsim89
Created March 27, 2023 15:07
Show Gist options
  • Save cjsim89/84b51df74d3299d61bb00278d0349386 to your computer and use it in GitHub Desktop.
Save cjsim89/84b51df74d3299d61bb00278d0349386 to your computer and use it in GitHub Desktop.
Class vs. Instance Methods M2 - game example
require "pry"
class Game #This game is called "guess what number the computer is thinking of"
   attr_reader :player_name, :computer_choice, :game_over

   def self.start
      game = Game.new
      game.get_players
      game.start_computer
      until game.game_over == true
         game.take_turn
      end
      puts "Game over! You win."

   end

   def initialize
      @player_name = nil
      @computer_choice = nil
      @game_over = false
   end

   def get_players
      puts "What's your name?"
      @player_name = gets.chomp      
   end

   def start_computer
      puts "Computer is choosing..."
      @computer_choice = rand(5)
   end

   def take_turn
      puts "What number did the computer choose? (#{@computer_choice})"
      choice = gets.chomp
      if choice.to_s == @computer_choice.to_s
         @game_over = true
      end
   end

end

Game.start
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment