Created
November 6, 2013 14:53
-
-
Save plexus/7337298 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Shoot | |
def initialize | |
@kick = 1 + rand(6) | |
end | |
def result | |
@kick * 0.2 | |
end | |
def goal? | |
result >= 1 | |
end | |
end | |
class Player < Struct.new(:number, :name, :score) | |
def self.create_from_user_input(number) | |
puts "Player #{number}'s name is:" | |
name = gets.chomp | |
new(number, name, 0) | |
end | |
def shoot! | |
puts "#{name}, please take your kick (ENTER)." | |
gets | |
puts "#{name} takes the shot, and it's looking good..." | |
puts "(ENTER)" | |
gets | |
if Shoot.new.goal? | |
puts "...And it's a goal!!!" | |
self.score += 1 | |
else | |
puts "...Oh! So close, but it's no good!!!" | |
end | |
end | |
def print_score! | |
puts "#{name}: #{score}" | |
end | |
def print_victory! | |
puts "#{name}, you're the winner! Congratulations!" | |
end | |
end | |
begin | |
players = [1,2].map{|i| Player.create_from_user_input(i) } | |
puts players.map(&:name).join(' and ') + ', are you ready to play sudden death (ENTER)?!' | |
gets.chomp | |
loop do | |
players.each(&:shoot!) | |
players.each(&:print_score!) | |
if players[0].score == players[1].score | |
puts "Looks like we've gotta go another round!" | |
else | |
winner = players.max_by(&:score) | |
winner.print_victory! | |
break | |
end | |
end | |
puts "Have another shoot-out? Put Y or N." | |
end until gets.chomp == 'N' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment