-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtic-tac-toe.clib
121 lines (113 loc) · 3.03 KB
/
tic-tac-toe.clib
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
(
(defconstant EMPTY ' ')
(defconstant MIN_POS 0)
(defconstant MAX_POS 8)
; o | o | x
; ---+---+---
; x | x | x
; ---+---+---
; o | x | o
(defun get-new-board-rec (board v count pos)
(if (l board)
(c
(if (= count pos)
(if (= EMPTY (f board))
v
(x pos "position already occupied")
)
(f board)
)
(get-new-board-rec
(r board) v (+ count 1) pos
)
)
()
)
)
; (
; 0 1 2
; 3 4 5
; 6 7 8
; )
(defun get-new-board (board v pos)
(if (any
; valid position is 0-8
(> MIN_POS pos)
(> pos MAX_POS)
)
(x pos "position invalid")
(get-new-board-rec
board v 0 pos
)
)
)
(defun-inline check-line (v0 v1 v2 v)
(if
(all
(= v0 v)
(= v1 v)
(= v2 v)
)
1 ; all position matched
0
)
)
(defun check-playable-board ((p0 p1 p2 p3 p4 p5 p6 p7 p8))
(if
; if there is any playable position left
(any
(= p0 EMPTY)
(= p1 EMPTY)
(= p2 EMPTY)
(= p3 EMPTY)
(= p4 EMPTY)
(= p5 EMPTY)
(= p6 EMPTY)
(= p7 EMPTY)
(= p8 EMPTY)
)
0 ; one or more positions left
-1 ; unplayable board, tie game
)
)
(defun check-winning-board ((p0 p1 p2 p3 p4 p5 p6 p7 p8) v)
(if
(any
; horizontal
(check-line p0 p1 p2 v)
(check-line p3 p4 p5 v)
(check-line p6 p7 p8 v)
; vertical
(check-line p0 p3 p6 v)
(check-line p1 p4 p7 v)
(check-line p2 p5 p8 v)
; diaganol
(check-line p0 p4 p8 v)
(check-line p2 p4 p6 v)
)
1 ; v won
0
)
)
; if v won return v
; else if board is still playable return 0 (playable)
; else return -1 (not playable)
(defun check-board (board v)
(if (= (check-winning-board board v) 1)
v ; v won
(check-playable-board board) ; 0 or -1
)
)
(defun-inline get-player-from-curried-tic-tac-toe-puzzle (curried_puzzle)
(r (f (r (f (r (r (f (r (r curried_puzzle)))))))))
)
(defun-inline get-next-player-from-curried-tic-tac-toe-puzzle (curried_puzzle)
(if (= (get-player-from-curried-tic-tac-toe-puzzle curried_puzzle) 'x')
'o'
'x'
)
)
(defun-inline get-uncurried-tic-tac-toe-puzzle (curried_puzzle)
(r (f (r curried_puzzle)))
)
)