Skip to content

Instantly share code, notes, and snippets.

@j0ni
Last active March 5, 2016 22:09
Show Gist options
  • Save j0ni/262c708fb1e87ee13309 to your computer and use it in GitHub Desktop.
Save j0ni/262c708fb1e87ee13309 to your computer and use it in GitHub Desktop.
class Player
attr_accessor :name, :score, :lives
def initialize(name)
@lives = 3
@score = 0
@name = name
end
def lose_life
self.lives -= 1
end
def gain_point
self.score += 1
end
end
class Problem
attr_accessor :number1, :number2
def initialize
self.number1 = rand(1..20)
self.number2 = rand(1..20)
end
def answer
number1 + number2
end
def answer_correct?(test_number)
answer == test_number
end
def to_s
"What's #{number1} + #{number2}?"
end
end
class Game
attr_accessor :player1, :player2
def get_input(prompt)
puts prompt
gets.chomp
end
def players_have_lives
player1.lives > 0 && player2.lives > 0
end
def game_loop
while players_have_lives
problem = Problem.new
end
end
def setup
self.player1 = Player.new(get_input("Player 1, enter your name:"))
self.player2 = Player.new(get_input("Player 2, enter your name:"))
end
class << self
def run
game = Game.new
game.setup
game.game_loop
game.print_scores
end
end
end
Game.run
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment