-
Notifications
You must be signed in to change notification settings - Fork 0
/
tictactoe.lisp
166 lines (147 loc) · 4.36 KB
/
tictactoe.lisp
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
(defvar *board* (make-array '(3 3)))
(defun tile-char (tile)
(ecase tile
(0 #\-) ;; 0 is empty
(1 #\O) ;; 1 is nought
(2 #\X) ;; 2 is cross
)
)
(defun print-board ()
(dotimes (i (array-dimension *board* 0))
(dotimes (j (array-dimension *board* 1))
(format t "~A " (tile-char (aref *board* j i)))
)
(format t "~%")
)
)
(defun check-win ()
(let ((win-tile 0))
;; check horizontal
(dotimes (i (array-dimension *board* 1))
(if (and
(= (aref *board* 0 i) (aref *board* 1 i))
(= (aref *board* 0 i) (aref *board* 2 i)))
(let ((tile 0))
(setf tile (aref *board* 0 i))
(if (not (= tile 0))
(progn
(setf win-tile tile)
(return)
)
)
)
)
)
;; check vertical
(if (= win-tile 0)
(dotimes (i (array-dimension *board* 0))
(if (and
(= (aref *board* i 0) (aref *board* i 1))
(= (aref *board* i 0) (aref *board* i 2)))
(let ((tile 0))
(setf tile (aref *board* i 0))
(if (not (= tile 0))
(progn
(setf win-tile tile)
(return)
)
)
)
)
)
)
;; check diagonal
(if (= win-tile 0)
(if (and
(= (aref *board* 0 0) (aref *board* 1 1))
(= (aref *board* 0 0) (aref *board* 2 2)))
(let ((tile 0))
(setf tile (aref *board* 0 0))
(if (not (= tile 0))
(setf win-tile tile)
)
)
(if (and
(= (aref *board* 2 0) (aref *board* 1 1))
(= (aref *board* 2 0) (aref *board* 0 2)))
(let ((tile 0))
(setf tile (aref *board* 2 0))
(if (not (= tile 0))
(setf win-tile tile)
)
)
)
)
)
(if (not (= win-tile 0))
(progn
(print-board)
(format t "~%~A wins!~%" (tile-char win-tile))
(exit)
)
)
)
)
(defun computer-move ()
(let (
(x 0)
(y 0)
)
(loop
(setf x (random (array-dimension *board* 0)))
(setf y (random (array-dimension *board* 1)))
(if (= (aref *board* x y) 0)
(progn
(setf (aref *board* x y) 1)
(return)
)
)
)
)
)
(let (
(x 0)
(y 0)
(ch #\X)
(win-tile 0)
)
;; init random state
(setf *random-state* (make-random-state t))
(format t "LispTicTacToe~%")
(loop
(print-board)
;; get X position
(loop
(format *query-io* "Enter tile X (1-3): ")
(finish-output *query-io*)
(setq x (parse-integer (read-line *query-io*)))
;; check if x is valid
(if (or (< x 1) (> x 3))
(format t "Invalid number~%")
(return) ;; x is valid
)
)
;; get Y position
(loop
(format *query-io* "Enter tile Y (1-3): ")
(finish-output *query-io*)
(setq y (parse-integer (read-line *query-io*)))
;; check if y is valid
(if (or (< x 1) (> x 3))
(format t "Invalid number~%")
(return) ;; y is valid
)
)
;; adjust x and y
(decf x)
(decf y)
;; set tile
(setf (aref *board* x y) 2)
;; check win
(check-win)
;; now its time for the computer to make a move
(computer-move)
;; check win
(check-win)
)
)