Skip to content

Instantly share code, notes, and snippets.

@Leejojo
Created June 24, 2016 21:57
Show Gist options
  • Save Leejojo/736a5b017c21dca413ebed70c5de78b4 to your computer and use it in GitHub Desktop.
Save Leejojo/736a5b017c21dca413ebed70c5de78b4 to your computer and use it in GitHub Desktop.
Two Player Game
require "pry"
@player1_points = 3
@player2_points = 3
@players = {player1: @player1_points, player2: @player2_points}.cycle
def whos_playing
puts "#{@current_player[0]}, your turn"
end
def question_generator
@x, @y = [rand(1...20), rand(1...20)]
puts "What does #{@x} plus #{@y} equal?"
@player_answer = gets.chomp.to_i
end
def verify_answer
right_answer = @x + @y
if right_answer == @player_answer
puts "Correct! You still have #{@current_player[1]} lives."
else
puts "#{@player_answer} is incorrect."
@current_player[1] -= 1
puts "You now have #{@current_player[1]} lives left."
end
@current_player[1]
end
def winner
puts "The Winner is #{@current_player[0].next}!!"
end
loop do
@current_player = @players.next
whos_playing
question_generator
verify_answer
break if @current_player[1] == 0
end
winner
puts "GAME OVER!!"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment