Last active
August 29, 2015 14:16
-
-
Save rockymadden/f0279dfdc24e0dfa5af1 to your computer and use it in GitHub Desktop.
CSP based pong in ES6 JavaScript.
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
"use strict"; | |
const csp = require("js-csp"); | |
const setup = (table) => { | |
const player = function*(name) { | |
let ball; | |
while((ball = yield csp.take(table)) !== csp.CLOSED) { | |
ball.hits += 1; | |
console.log(name + " " + ball.hits); | |
yield csp.timeout(100); | |
yield csp.put(table, ball); | |
} | |
}; | |
const game = function*() { | |
csp.go(player, ["ping"]); | |
csp.go(player, ["pong"]); | |
yield csp.put(table, {hits: 0}); | |
yield csp.timeout(1000); | |
table.close(); | |
}; | |
return {play: () => csp.go(game)}; | |
}; | |
setup(csp.chan()).play(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment