9mm/main.fnl

58 lines
1.5 KiB
Plaintext
Raw Permalink Normal View History

(local {: game} (require :src.game))
2024-05-28 21:04:00 +00:00
(local {
2024-06-16 03:15:59 +00:00
: str
: tbl
2024-05-28 21:04:00 +00:00
} (require :lib.index))
2024-05-30 01:26:41 +00:00
(local const (require :lib.constants))
2024-05-28 21:04:00 +00:00
(game:init)
2024-05-28 21:04:00 +00:00
; Print! That! Board!
(fn print-board [board moves]
2024-05-30 01:26:41 +00:00
(var index 1)
2024-05-28 21:04:00 +00:00
(each [_ row (ipairs board)]
2024-05-30 01:26:41 +00:00
(let [(row-template slots) (string.gsub row "x" "%%d")]
(if (> slots 0)
2024-05-28 21:04:00 +00:00
(do
2024-05-30 01:26:41 +00:00
(let [offset (+ index slots)
2024-06-16 03:15:59 +00:00
myslice (tbl.slice moves index offset)]
(print (string.format row-template (table.unpack myslice)))
(set index offset)))
2024-05-30 01:26:41 +00:00
(print row))))
(print (.. "Stage: " (str.capitalize (. (tbl.flip const.stages) game.stage))))
2024-05-30 01:26:41 +00:00
(print (.. "Player " game.player "'s turn:")))
2024-05-28 21:04:00 +00:00
(local with-board (partial print-board const.board))
2024-05-28 21:04:00 +00:00
; get player input
(fn get-move []
(io.read))
(fn main []
;; game loop
(while (not (= game.stage const.stages.complete))
2024-06-16 03:15:59 +00:00
(with-board game.moves)
2024-05-28 21:04:00 +00:00
(var is-valid false)
(var move "")
;; validation loop
2024-05-28 21:04:00 +00:00
(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)))))
2024-06-16 03:15:59 +00:00
;; game is complete
(print "Congratulations!")
(print (string.format "Player %d is the winner!" game.player)))
2024-05-28 21:04:00 +00:00
(main)