blob: c205454c61e0802ca8131ac8e4aba2974c45bade (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
|
(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)
;; 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
(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)
|