Skip to content

Instantly share code, notes, and snippets.

@MaggieMoss
Created January 12, 2015 02:23
Show Gist options
  • Save MaggieMoss/9b0e5438eb6b654ef492 to your computer and use it in GitHub Desktop.
Save MaggieMoss/9b0e5438eb6b654ef492 to your computer and use it in GitHub Desktop.
Two Player Math problem solution - using ruby oop
require './Questions_Math_OOP.rb'
require './Players_Math_OOP.rb'
require 'colorize'
# get players name/info
puts "Player 1, what is your name?".colorize(:purple)
@name = gets.chomp.to_s
player1 = Players.new(@name)
puts player1.print_greeting
puts "Player 2, what is your name?".colorize(:yellow)
@name2 = gets.chomp.to_s
player2 = Players.new(@name2)
puts player2.print_greeting
#ask user the question
def turn(player_number)
math_problem = Question.new
puts math_problem.to_string
@answer = gets.chomp.to_i
if math_problem.check_answer(@answer)
puts "Correct!".colorize(:green)
puts "You have #{player_number.lives} lives left!"
else
puts "Sorry, wrong answer.".colorize(:red)
player_number.lose_lives
puts "You have #{player_number.lives} lives left!"
end
end
@counter = 1
#continue game until one player has 0 lives
def begin_game(player_one, player_two)
until player_one.lives == 0 || player_two.lives == 0
@counter += 1
if @counter.even?
puts "#{player_one.player_name}, it's your turn!".colorize(:purple)
turn(player_one)
else
puts "#{player_two.player_name} it's your turn!".colorize(:yellow)
turn(player_two)
end
end
end
puts begin_game(player1, player2)
if player1.lives == 0
puts "#{player2.player_name}, you win!"
else
puts "#{player1.player_name}, you win!"
end
player1.lives = 3
player2.lives = 3
puts "Do you want to play again? Yes or no?"
@response = gets.chomp.downcase.to_s
if @response == "yes"
puts "Let's go!"
puts player1.lives
begin_game(player1, player2)
else
puts "Good Bye!"
end
class Players
attr_accessor :lives
attr_reader :player_name
def initialize (player_name)
@player_name = player_name
@lives = 3
end
def print_greeting
return "Hello, #{@player_name}, let's play!"
end
def lose_lives
@lives -= 1
end
end
class Question
def initialize
@num1 = 1 + rand(20)
@num2 = 1 + rand(20)
#@answer = answer
end
def to_string
return "What is #{@num1} + #{@num2}?"
end
def check_answer(answer)
answer == (@num1 + @num2)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment