This repository was archived by the owner on Jun 22, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIO.lisp
More file actions
273 lines (179 loc) · 7.29 KB
/
IO.lisp
File metadata and controls
273 lines (179 loc) · 7.29 KB
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
#|*************************************************************
Input/Output
Description: Defines functions to display and retrieve
data to and from users.
**************************************************************|#
#|******************************************************************************
GENERAL Functions
******************************************************************************|#
(defun index-to-coord ()
"Convert an index to corresponding standard tournament rules chess coordinate."
)
(defun coord-to-index (coord)
"Convert a chess coordinate to corresponding index."
(let ((file (subseq coord 0 1))
(rank (parse-integer (subseq coord 1 2))))
(setf file (cond
((equal file "A") 0)
((equal file "B") 1)
((equal file "C") 2)
((equal file "D") 3)
((equal file "E") 4)
((equal file "F") 5)
((equal file "G") 6)
((equal file "H") 7)))
(+ (- (* 8 rank) 8) file)))
(defun piecename-to-index (input)
"Accepts a list element, input, (ex. Wp Bk...) and returns a number that corrosponds to the piece entered by the user"
(cond
((or (eq input 'bk)
(eq input 'wk)
(eql input 'k)) 0)
((or (eq input 'bq)
(eq input 'wq)
(eql input 'q)) 1)
((or (eq input 'br)
(eq input 'wr)
(eql input 'r)) 2)
((or (eq input 'bb)
(eq input 'wb)
(eql input 'b)) 3)
((or (eq input 'bn)
(eq input 'wn)
(eql input 'n)) 4)
((or (eq input 'bp)
(eq input 'wp)
(eql input 'p)) 5)
(t (progn
(format t "ERROR: Invalid piece ~s" input)
-1))))
(defun parse-move (input)
"Parses user input (stored as list) to a list in the form of (piecesIndex startIndex endIndex)"
(let ((result (list (piecename-to-index (car input)))))
(if (not (typep (second input) 'integer))
(setf result (append result (list (coord-to-index (string (second input))))))
(setf result (append result (list (second input)))))
(if (not (typep (third input) 'integer))
(setf result (append result (list (coord-to-index (string (third input))))))
(setf result (append result (list (third input)))))
(print result)
result))
(defun parse-user-command (input)
"Takes a string as an argument and executes the corresponding command"
(let ((command (car input)))
(cond
((eql command '/help) (print-help)) ;; Prints a list of available commands.
((eql command '/restart-game) (new-game)) ;; Starts a new game.
((eql command '/quit-game) (quit)) ;; Quits current game.
((eql command '/print-game-state) (print-game-state))) ;; Prints the current game state.
(get-user-input)))
#|******************************************************************************
INPUT Functions
******************************************************************************|#
(defun get-user-input()
"Promts the user for a movement input and returns a string form of the users input"
(let ((input nil))
(format t "~%>")
(setf input (read-line))
;; Check for blank input
(if (equal input "")
(return-from get-user-input (get-user-input)))
(let ((stream (make-string-input-stream input))
(next-tok nil)
(result nil))
;;convert user input string to list
(loop
(setf next-tok (read stream nil 'eos))
(if (equal next-tok 'eos)
(return)
(setf result (append result (list next-tok)))))
;; If the input begins with a "/" pass the list to parse user command.
(if (equal (subseq (string (car result)) 0 1) "/")
(parse-user-command result)
(parse-move result)))))
(defun perform-user-move ()
"Calls 'get-user-input' to get a list as users move command and performs the move if posible.
If not possible throws an error, and intructs the user to enter another input."
(let ((input nil)
(piece-reference-num 0))
(setf input (get-user-input))
;;;; EXECUTE MOVE ;;;;
;;sends input to determine piece # prints error if one occurs
(setf piece-reference-num (car input))
(if (or (eq piece-reference-num -1) (find (car input) (if (= (mod (turn_count current_game_state) 2) 0)
;;then
'(bp bn bb br bq bk)
;;else
'(wp wn wb wr wq wk))))
;;then
(progn
(print "ERROR: Move is not for valid piece or correct player")
(setf input nil)
(terpri)
(perform-user-move))
;;else
;;moves piece or prints error if one occurs
(if (not (move-piece (nth piece-reference-num
(pieces (if (= (mod (turn_count current_game_state) 2) 0)
;;then
(player1 current_game_state)
;;else
(player2 current_game_state))))
(second input) (third input)))
;;then
(progn
(print "ERROR: Move is not for valid piece or correct player")
(terpri)
(perform-user-move))))))
#|******************************************************************************
OUTPUT Functions
******************************************************************************|#
(defun new-game-comments ()
"Trash talk/ new game comments, just added for fun."
(let ((what-to-say (random 5)))
(cond
((eq what-to-say 0) "Tis but a flesh wound!!")
((eq what-to-say 1) "Well, thats like.. your opinion... man""YOU SHALL NOT PASSS!!!")
((eq what-to-say 2) "Are you ready for this?")
((eq what-to-say 3) "Well, thats like.. your opinion... man")
((eq what-to-say 4) "If a fat girl falls in the woods.. do the trees laugh?"))))
(defun print-help ()
"Prints a list of availble commands while in the game loop."
(let ((commands (list
"/help - Prints a list of available commands"
"/restart-game - Starts a new game."
"/quit-game - Quits current game and brings user to main menu."
"/print-game-state - Prints the current game state to the console.")))
(loop for i from 0 to (- (list-length commands) 1) do
(print (nth i commands)))))
(defun print-index-bb ()
"Prints a bit board labled with the indexs from 0 to 64"
(terpri)
(format t "~% A B C D E F G H")
(format t "~% -----------------------------------------")
(loop for i from 0 to 7 do
(let ((x))
(loop for j from 0 to 7 do
(setf x (cons (- 63 (+ j (* i 8))) x)))
(format t "~% ~D | ~{~2,'0d |~^ ~} ~D" (- 8 i) x (- 8 i)))
(format t "~% -----------------------------------------"))
(format t "~% A B C D E F G H")
(terpri))
(defun terprix (x)
"Prints x new lines on the console."
(loop for i from 0 to x do
(terpri)))
(defun terprix20 ()
"Prints 20 new lines on the console."
(loop for i from 0 to 19 do
(terpri)))
#|******************************************************************************
DEBUG Functions
******************************************************************************|#
(defun print-index-bb ()
"Prints a bit board labled with the indexs from 0 to 64"
(loop for i from 0 to 7 do
(let ((x))
(loop for j from 0 to 7 do
(setf x (cons (- 63 (+ j (* i 8))) x)))
(format t "~{ ~2,'0d~^ ~}~%~%" x))))