Last active
October 7, 2018 15:27
-
-
Save scientific-coder/175556a3205f5d7e6105605994024687 to your computer and use it in GitHub Desktop.
Parsing some scala code to svg for Kingdomino deck updated for formatting, thx!
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 kingdomino.core | |
(:require [instaparse.core :as insta]) | |
(:gen-class)) | |
(def size 100) | |
(def delta-sides 5) | |
(def ncols 2) | |
(def delta-dominos 10) | |
(def colors {:clay "#cc6600" :field "#ffff66" :water "#33ccff" :pasture "#00cc00" :forest "#669900" :mine "#5c5c3d"}) | |
(def domino-parser | |
(instaparse.core/parser | |
"DECK= ((DOMINO | <LINE_COMMENT> | <BLOCK_COMMENT>) / <USELESS>)* | |
LINE_COMMENT= '//' #'[^\n]*' | |
BLOCK_COMMENT= '/*' INSIDE_COMMENT* '*/' | |
INSIDE_COMMENT= !( '*/' | '/*' ) #'.'| '\n' | BLOCK_COMMENT | |
DOMINO= <'Domino'> <SPACES>? <'('> <SPACES>? DOMINO_NUMBER <SPACES>? <','> <SPACES>? TILE <SPACES>? <','> <SPACES>? TILE <SPACES>? <')'> | |
DOMINO_NUMBER= NUMBER | |
TILE= <'Tile'> <SPACES>? <'('> <SPACES>? TERRAIN <SPACES>? ( <','> <SPACES>? CASTLES_NUMBER <SPACES>?)? <')'> | |
CASTLES_NUMBER= NUMBER | |
TERRAIN= 'Forest' | 'Water' |'Pasture'|'Clay'|'Field'|'Mine' | |
<USELESS>= #'.' | '\n' | |
NUMBER= #'-?[0-9]+' | |
<SPACES> = #'\\s+'")) | |
(defn tile | |
([type] (tile type "")) | |
([type castles] | |
(str "<path d=\"m" size " 0 l-" size " 0 l0 " size " l" size " 0 \" fill=\"" (type colors) "\"/>\n" | |
castles))) | |
(defn translate [[x y] s] | |
(str "<svg x=\"" x "\" y=\"" y "\">\n" s "</svg>\n")) | |
(defn back [id] | |
(let [length (* 2 size)] | |
(str "<path d=\"m" length " 0 l-" length " 0 l0 " size " l" length | |
" 0 \" fill=\"black\"/>\n" "<text x=\"" size "\" y=\"" (/ size 2) | |
"\" fill=\"white\" stroke=\"grey\" text-anchor= \"middle\"" | |
" font-size= \"48\">" id "</text>\n"))) | |
(defn rotate [a [x y]] | |
(let [[c s] [(Math/cos a) (Math/sin a)]] | |
[(- (* c x) (* s y)) (+ (* s x) (* c y))])) | |
(defn castles [n] | |
(let [c (/ size 2) | |
r (/ size 10) | |
castle (str "<circle cx=\"" c "\" cy=\"" c "\" r=\"" r "\" stroke=\"black\" " | |
"stroke-width=\"3\" fill=\"red\" />") | |
v [(/ size 5) 0]] | |
(->> (range n) | |
(map #(translate (rotate (/ (* 2 Math/PI %) n) v) | |
castle)) | |
(apply str)))) | |
(defn domino [back tile1 tile2] | |
(str back | |
(translate [(+ (* 2 size) delta-sides) 0] | |
(str tile1 (translate [size 0] tile2))))) | |
(defn deck [& dominos] | |
(let [dx (+ delta-dominos (* 4 size) delta-sides) | |
dy (+ delta-dominos size) | |
grid-placement (fn [i d] | |
(let [x (rem i ncols) | |
y (quot i ncols)] | |
(translate [(* x dx) (* y dy)] d))) | |
dominos (->> dominos (map-indexed grid-placement) (apply str)) | |
w (* dx ncols) | |
h (* dy (quot (count dominos) ncols))] | |
(str "<svg version=\"1.1\" baseProfile=\"full\" xmlns=\"http://www.w3.org/2000/svg\">\n" | |
"<svg height=\"" h "\" width=\"" w "\">" | |
dominos | |
"</svg>\n" | |
"</svg>\n"))) | |
(defn -main | |
"reads scala code constructing a deck of dominos and output the svg" | |
[& [input output]] | |
(->> (or input *in*) slurp (insta/parse domino-parser) | |
(insta/transform {:NUMBER #(Long/parseLong %) | |
:CASTLES_NUMBER castles | |
:TILE tile | |
:DOMINO_NUMBER back | |
:DOMINO domino | |
:TERRAIN (comp keyword clojure.string/lower-case) | |
:DECK deck}) | |
(spit (or output *out*)))) | |
;; lein uberjar | |
;; java -jar target/uberjar/kingdomino-0.1.0-SNAPSHOT-standalone.jar https://raw.githubusercontent.com/mratin/kdom/master/src/main/scala/se/apogo/kdom/Domino.scala output.svg |
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
package se.apogo.kdom | |
case class Domino(number: Int, tile1: Tile, tile2: Tile) { | |
} | |
// Domino(300, Tile(Field), Tile(Field)), | |
// Domino(330, Tile(Field), Tile(Field)),Domino(330, Tile(Field), Tile(Field)), | |
/* | |
Domino(200, Tile(Field), Tile(Field)), | |
Domino(220, Tile(Field), Tile(Field)), Domino(220, Tile(Field), Tile(Field)), | |
*/ | |
object Domino { | |
val allDominoes: Seq[Domino] = Seq( | |
Domino(1, | |
Tile(Field), | |
Tile(Field)), | |
Domino(2, Tile(Field), Tile(Field)), | |
Domino(3, Tile(Forest), Tile(Forest)), | |
Domino(4, Tile(Forest), Tile(Forest)), | |
Domino(5, Tile(Forest), Tile(Forest)), | |
Domino(6, | |
Tile(Forest), | |
Tile(Forest)), | |
Domino(7, Tile(Water), Tile(Water)), | |
Domino(8, | |
Tile(Water), Tile(Water)), | |
Domino(9, Tile(Water), Tile(Water)), | |
Domino(10, Tile(Pasture), Tile(Pasture)), | |
Domino(11, Tile(Pasture), Tile(Pasture)), | |
Domino(12, Tile(Clay), Tile(Clay)), | |
Domino(13, Tile(Field), Tile(Forest)), | |
Domino(14, Tile(Field), Tile(Water)), | |
Domino(15, Tile(Field), Tile(Pasture)), | |
Domino(16, Tile(Field), Tile(Clay)), | |
Domino(17, Tile(Forest), Tile(Water)), | |
Domino(18, Tile(Forest), Tile(Pasture)), | |
Domino(19, Tile(Field, 1), Tile(Forest)), | |
Domino(20, Tile(Field, 1), Tile(Water)), | |
Domino(21, Tile(Field, 1), Tile(Pasture)), | |
Domino(22, Tile(Field, 1), Tile(Clay)), | |
Domino(23, Tile(Field, 1), Tile(Mine)), | |
Domino(24, Tile(Forest, 1), Tile(Field)), | |
Domino(25, Tile(Forest, 1), Tile(Field)), | |
Domino(26, Tile(Forest, 1), Tile(Field)), // Domino(400, Tile(Field), Tile(Field)), | |
Domino(27, Tile( | |
Forest | |
, | |
1 | |
) , | |
Tile | |
( | |
Field | |
) | |
) | |
, | |
Domino(28, Tile(Forest, 1), Tile(Water)), | |
Domino(29, Tile(Forest, 1), Tile(Pasture)), | |
Domino(30, Tile(Water, 1), Tile(Field)), | |
Domino(31, Tile(Water, 1), Tile(Field)), | |
Domino(32, Tile(Water, 1), Tile(Forest)), | |
Domino(33, Tile(Water, 1), Tile(Forest)), | |
Domino(34, Tile(Water, 1), Tile(Forest)), | |
Domino(35, Tile(Water, 1), Tile(Forest)), | |
Domino(36, Tile(Field), Tile(Pasture, 1)), | |
Domino(37, Tile(Water), Tile(Pasture, 1)), | |
Domino(38, Tile(Field), Tile(Clay, 1)), | |
Domino(39, Tile(Pasture), Tile(Clay, 1)), | |
Domino(40, Tile(Mine, 1), Tile(Field)), | |
Domino(41, Tile(Field), Tile(Pasture, 2)), | |
Domino(42, Tile(Water), Tile(Pasture, 2)), | |
Domino(43, Tile(Field), Tile(Clay, 2)), | |
Domino(44, Tile(Pasture), Tile(Clay, 2)), | |
Domino(45, Tile(Mine, 2), Tile(Field)), | |
Domino(46, Tile(Clay), Tile(Mine, 2)), | |
Domino(47, Tile(Clay), Tile(Mine, 2)),/* Domino(500, Tile(Field), Tile(Field)), */ | |
Domino(48, Tile(Field), Tile(Mine, 3)) | |
) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment