Last active
April 26, 2022 09:59
-
-
Save phunanon/aa90672fed7e5d961d1a54f10aeb624b to your computer and use it in GitHub Desktop.
Insitux 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
(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