-
-
Save vpereira/16b06728102df5c7338cea60921533ab 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 Ball | |
property :hits | |
def initialize | |
@hits = 0 | |
end | |
end | |
def player(name : String, table : Channel) | |
loop do | |
ball = table.receive | |
ball.hits += 1 | |
puts "#{name}: #{ball.hits}" | |
sleep 1 | |
table.send ball | |
end | |
end | |
table = Channel(Ball).new | |
spawn do | |
player("foo", table) | |
end | |
spawn do | |
player("bar", table) | |
end | |
table.send Ball.new # game on; toss the ball | |
sleep 5 # increasing it, the game goes longer | |
table.receive | |
table.close |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment