Skip to content

Instantly share code, notes, and snippets.

@phunanon
Last active April 26, 2022 09:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save phunanon/aa90672fed7e5d961d1a54f10aeb624b to your computer and use it in GitHub Desktop.
Save phunanon/aa90672fed7e5d961d1a54f10aeb624b to your computer and use it in GitHub Desktop.
Insitux tic-tac-toe
(function check
(let [[a _ x] [_ b] [z _ c]] board)
;Check diagonals
(when (or (= a b c) (= x b z)) (return b))
;Check horizontals and verticals
(let board+T (into board (.. map vec board)))
(when (let [w] (find (.. =) board+T)) w))
(function place n xo
(var! board (set-at [(// n 3) (rem n 3)] xo)))
(function handler input
;Parse the input
(let [n xo] input)
;Initialise if not already done or requested
(if (or (! ((symbols) "board")) (= input "new"))
(var board (partition 3 (range 9))))
;Print instructions on bad input
(if! (["o" "x"] xo)
(print "Send e.g. \"!ttt 5x\" or \"!ttt 4o\" or \"!ttt new\""))
;Place the character
(when (and n (let n (to-num n)) xo)
(place n xo))
;Check if there was a winner
(when (let winner (check))
(print winner " wins!"))
;Render the board
(join "\n" (map (join " ") board)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment