9mm/main.fnl

52 lines
1.3 KiB
Plaintext
Raw 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
;; validation loop
(var is-valid false)
(var move "")
(while (not is-valid)
(set move (get-move))
(set is-valid (game.validate-move move))
(if (not is-valid)
(print "Try again.")
(do
2024-06-16 03:15:59 +00:00
(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)))
2024-05-28 21:04:00 +00:00
(main)