Created
July 6, 2016 10:38
-
-
Save jmglov/8b89fe00aefe3f94f3b904eae9db32c1 to your computer and use it in GitHub Desktop.
Ping-pong game with core.async
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
(ns ping-pong | |
(:require [clojure.core.async :as async :refer [<!! >!!]])) | |
(defn ping [ch] | |
(println "Ping") | |
(>!! ch :ping) | |
(<!! (async/timeout 500))) | |
(defn pong [ch] | |
(println "Pong") | |
(>!! ch :pong) | |
(<!! (async/timeout 500))) | |
(defn return [ch] | |
(let [ball (<!! ch)] | |
(if (= ball :ping) | |
(pong ch) | |
(ping ch)))) | |
(defn play [] | |
(let [ch (async/chan 1)] | |
(loop [cnt 0] | |
(if (= cnt 0) | |
(ping ch) | |
(return ch)) | |
(if (< cnt 10) | |
(recur (inc cnt)) | |
(println "Game over!"))))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment