2024-06-20 15:17:06 +00:00
|
|
|
(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
|
|
|
|
2024-06-07 04:49:52 +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)]
|
2024-06-03 20:41:37 +00:00
|
|
|
(print (string.format row-template (table.unpack myslice)))
|
|
|
|
(set index offset)))
|
2024-05-30 01:26:41 +00:00
|
|
|
(print row))))
|
2024-06-20 15:17:06 +00:00
|
|
|
(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
|
|
|
|
2024-06-20 15:17:06 +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
|
2024-06-20 15:17:06 +00:00
|
|
|
(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))
|
2024-06-20 15:17:06 +00:00
|
|
|
(set is-valid (game.validate-move move))
|
2024-06-07 04:49:52 +00:00
|
|
|
(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!")
|
2024-06-20 15:17:06 +00:00
|
|
|
(print (string.format "Player %d is the winner!" game.player)))
|
2024-05-28 21:04:00 +00:00
|
|
|
(main)
|