Last active
January 22, 2021 13:23
-
-
Save neeasade/6a44094e7cc98125d94a83a0e3b8da0d to your computer and use it in GitHub Desktop.
netcat http keyval memory store || clojure tic tac toe
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
#!/usr/bin/env bash | |
# a nc server thing that I was kinda curious if it would work | |
# depends on: nc (netcat), bash 4.0(hashmaps/assoc arrays) | |
port=4000 | |
declare -A items | |
items[test]="woooooo" | |
# quick refresher: | |
# get: ${items[foo]} | |
# set: items[foo]=bar | |
# all the keys: "${!items[@]}" | |
fifo="$(mktemp -u)" | |
mkfifo "$fifo" | |
while true; do | |
while read -r line; do | |
# echo "$line" | |
http_resp() { | |
code=$1 | |
content=$2 | |
cat<<EOF > "$fifo" | |
HTTP/1.1 ${code} OK | |
Content-Type: text/html; charset=UTF-8 | |
Server: netcaterino | |
$content | |
EOF | |
} | |
case "$line" in | |
"GET /get?"*) | |
# eg GET /get?key=test HTTP/1.1 | |
echo "$line" | |
key="$(echo "$line" | sed -E 's#.*key=(.*) HTTP.*#\1#')" | |
echo "got request for key $key" | |
http_resp 200 "${items[$key]}" | |
;; | |
"GET /set?"*) | |
# eg GET /set?key=test HTTP/1.1 | |
echo "$line" | |
key="$(echo "$line" | sed -E 's#.*set\?(.*)=.*#\1#')" | |
val="$(echo "$line" | sed -E 's#.*=(.*) HTTP.*#\1#')" | |
items[$key]=$val | |
http_resp 200 "key '${key}' set to '${val}'" | |
;; | |
"GET "*) | |
echo "$line" | |
http_resp 404 "not found -- did you ask for a key?" | |
;; | |
esac | |
# the -N is what allows this to work: | |
# -N: shutdown(2) the network socket after EOF on the input. Some servers require this to | |
# finish their work. | |
done < <(cat "$fifo" | nc -N -l $port) | |
done |
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
#!/usr/bin/env clojure | |
;; oof | |
(defn has-winner [board] | |
;; (map ;; for testing | |
(some | |
(fn [checks] | |
;; return the value when we find they all equal | |
(when (apply = (map #(nth board %) checks)) | |
(nth board (first checks)))) | |
;; assuming 3x3, vec | |
;; 0 1 2 | |
;; 3 4 5 | |
;; 6 7 8 | |
[ | |
[0 1 2] | |
[3 4 5] | |
[6 7 8] | |
[0 3 6] | |
[1 4 7] | |
[2 5 8] | |
[0 4 8] | |
[2 4 6] | |
] | |
)) | |
(defn make-move [board player] | |
(println (format "-------------")) | |
(defn render-row [a b c] | |
(println (format "| %s | %s | %s |" a b c)) | |
(println (format "-------------"))) | |
(mapv #(apply render-row %) (partition 3 board)) | |
(println (format "enter move for player %s: " player)) | |
(assoc board (read-string (read-line)) player) | |
) | |
(loop [board (vec (take 9 (range))) | |
player "x"] | |
(if (has-winner board) | |
(println (format "congrats player %s!" (has-winner board))) | |
(recur (make-move board player) | |
(if (= player "x") "o" "x") | |
))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment