(local {: game} (require :src.game)) (local { : str : tbl } (require :lib.index)) (local const (require :lib.constants)) (game:init) ; Print! That! Board! (fn print-board [board moves] (var index 1) (each [_ row (ipairs board)] (let [(row-template slots) (string.gsub row "x" "%%d")] (if (> slots 0) (do (let [offset (+ index slots) myslice (tbl.slice moves index offset)] (print (string.format row-template (table.unpack myslice))) (set index offset))) (print row)))) (print (.. "Stage: " (str.capitalize (. (tbl.flip const.stages) game.stage)))) (print (.. "Player " game.player "'s turn:"))) (local with-board (partial print-board const.board)) ; get player input (fn get-move [] (io.read)) (fn main [] ;; game loop (while (not (= game.stage const.stages.complete)) (with-board game.moves) (var is-valid false) (var move "") ;; validation loop (while (not is-valid) (set move (get-move)) (case (pcall game.validate-move move) (false msg) (do (let [(i j) (string.find msg ": ") key (string.sub msg (+ 1 j))] (print (. const.errors key))) (print "Try again.")) ok (do (set is-valid true) (print (string.format "Turn %d: You chose %s" game.turns move)) (game:update move))))) ;; game is complete (print "Congratulations!") (print (string.format "Player %d is the winner!" game.player))) (main)