|
| 1 | +^{:kindly/hide-code true |
| 2 | + :clay {:title "4Clojure puzzles with twotiles" |
| 3 | + :quarto {:author :kloimhardt |
| 4 | + :type :post |
| 5 | + :draft true |
| 6 | + :date "2025-09-25" |
| 7 | + :image "" |
| 8 | + :category :concepts |
| 9 | + :tags [:blockly :graphical-programming]}}} |
| 10 | + |
| 11 | +(ns games.twotiles-4clojure.clj |
| 12 | + (:require [scicloj.kindly.v4.api :as kindly] |
| 13 | + [scicloj.kindly.v4.kind :as kind])) |
| 14 | + |
| 15 | +^:kindly/hide-code |
| 16 | +(def md |
| 17 | + (comp kindly/hide-code kind/md)) |
| 18 | + |
| 19 | +^:kindly/hide-code |
| 20 | +(kind/hiccup |
| 21 | + [:div |
| 22 | + [:script {:src "https://unpkg.com/blockly/blockly_compressed.js"}] |
| 23 | + [:script {:src "https://unpkg.com/blockly/msg/en.js"}] |
| 24 | + [:script {:src "https://kloimhardt.github.io/twotiles/twotiles_core.js"}] |
| 25 | + [:script (str "var parse = scittle.core.eval_string(twotiles.parse_clj);" |
| 26 | + "var addBlocks = (ws, code) =>" |
| 27 | + "Blockly.Xml.appendDomToWorkspace(Blockly.utils.xml.textToDom(parse(code)), ws);")] |
| 28 | + [:script "Blockly.defineBlocksWithJsonArray(twotiles.blocks);"]]) |
| 29 | + |
| 30 | +^:kindly/hide-code |
| 31 | +(defn create-ws [name height] |
| 32 | + [:div |
| 33 | + [:div {:id name, :style {:height height}}] |
| 34 | + [:script (str "var " name " = Blockly.inject('" name "'," |
| 35 | + "{'sounds': false, 'scrollbars':false, 'trashcan':false})")]]) |
| 36 | + |
| 37 | +(md "## 4Clojure") |
| 38 | + |
| 39 | +(defn pronounce [s] |
| 40 | + (mapcat (juxt count first) |
| 41 | + (partition-by identity s))) |
| 42 | + |
| 43 | +(doall [(pronounce [1]) |
| 44 | + (pronounce [1 1]) |
| 45 | + (pronounce [2 1])]) |
| 46 | + |
| 47 | +^:kindly/hide-code |
| 48 | +(defn add-ws [ws] |
| 49 | + (fn [code] |
| 50 | + (kindly/hide-code |
| 51 | + (kind/scittle |
| 52 | + (list 'js/addBlocks |
| 53 | + ws |
| 54 | + (str code)))))) |
| 55 | + |
| 56 | +^:kindly/hide-code |
| 57 | +(def add-pr1 (add-ws 'js/pr1)) |
| 58 | +(add-pr1 's) |
| 59 | +(add-pr1 'identity) |
| 60 | +(add-pr1 '(partition-by :tiles/slot |
| 61 | + :tiles/slot)) |
| 62 | +(kind/hiccup (create-ws "pr1" "300px")) |
| 63 | + |
| 64 | +^:kindly/hide-code |
| 65 | +(def add-pr2 (add-ws 'js/pr2)) |
| 66 | +(add-pr2 '(partition-by identity s)) |
| 67 | +(add-pr2 'first) |
| 68 | +(add-pr2 'count) |
| 69 | +(add-pr2 '(juxt :tiles/slot :tiles/slot)) |
| 70 | +(kind/hiccup (create-ws "pr2" "300px")) |
| 71 | + |
| 72 | +^:kindly/hide-code |
| 73 | +(def add-pr3 (add-ws 'js/pr3)) |
| 74 | +(add-pr3 '(partition-by identity s)) |
| 75 | +(add-pr3 '(juxt count first)) |
| 76 | +(add-pr3 '(:tiles/vert |
| 77 | + (mapcat :tiles/slot |
| 78 | + :tiles/slot))) |
| 79 | +(kind/hiccup (create-ws "pr3" "300px")) |
| 80 | + |
| 81 | +^:kindly/hide-code |
| 82 | +(def add-pr4 (add-ws 'js/pr4)) |
| 83 | +(add-pr4 |
| 84 | + '(:tiles/vert |
| 85 | + (mapcat (juxt count first) |
| 86 | + (partition-by identity s)))) |
| 87 | +(add-pr4 '[s]) |
| 88 | +(add-pr4 'pronounce) |
| 89 | +(add-pr4 '(defn :tiles/slot |
| 90 | + :tiles/slot |
| 91 | + :tiles/slot)) |
| 92 | +(kind/hiccup (create-ws "pr4" "300px")) |
| 93 | + |
| 94 | +((add-ws 'js/pr5) |
| 95 | + '(defn pronounce [s] |
| 96 | + (:tiles/vert |
| 97 | + (mapcat (juxt count first) |
| 98 | + (partition-by identity |
| 99 | + s))))) |
| 100 | + |
| 101 | +(kind/hiccup (create-ws "pr5" "300px")) |
| 102 | + |
| 103 | +(kind/scittle |
| 104 | + '(defn add-blocks [ws code] |
| 105 | + (js/addBlocks ws (str code)))) |
| 106 | + |
| 107 | +(kind/scittle '(add-blocks js/prend '(8 9 10))) |
| 108 | + |
| 109 | +(kind/hiccup (create-ws "prend" "300px")) |
0 commit comments