-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.scpt
299 lines (288 loc) · 9.75 KB
/
main.scpt
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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
--Chess Helper app main script
if the length of (serialport list) is less than 4 then
beep
display dialog "No available port" with icon caution
error number -128
else if the length of (serialport list) is greater than 4 then
set theport to (choose from list (serialport list) with title "Chess Helper" with prompt "Please select port:")
if theport is false then error number -128
else
set theport to item 4 of (serialport list)
end if
set counter to 0
set onMode to false
repeat until onMode is equal to true
set counter to counter + 1
set arduino to serialport open theport bps rate 9600 data bits 8 parity 0 stop bits 1 handshake 0
if arduino is equal to -1 then
serialport close theport
if counter is greater than 15 then
beep
display dialog "Unable to open port" with icon caution
error number -128
end if
else
set onMode to true
end if
delay 0.1
end repeat
set turn to 1
repeat
set inText to ""
set stringComplete to false
repeat while stringComplete is equal to false
if (serialport bytes available arduino) is greater than 0 then
set inChar to (serialport read arduino for 1)
set inText to (inText & inChar)
if inChar is equal to (ASCII character 10) then
set inText to (text 1 thru ((offset of (ASCII character 10) in inText) - 2) of inText)
set stringComplete to true
--say inText
end if
end if
end repeat
if inText is equal to "S" then
tell application "Terminal" to quit
display dialog "Start a new chess game?" with title "Chess Helper" buttons {"Quit", "Yes"} default button "Yes" cancel button "Quit" with icon file ((path to desktop as text) & "Chess Helper.app:Contents:Resources:chess game.icns")
set gameModes to {"Human vs Human", "Human vs Computer", "Computer vs Human", "Computer vs Computer"}
set answer to (choose from list gameModes with title "Chess Helper" with prompt "Please select game mode:" default items "Human vs Computer")
if answer is false then error number -128
repeat with i from 1 to 4
if answer as string is equal to (item i of gameModes) then
set mode to i as number
end if
end repeat
if mode is not 1 then
set difficulty to (choose from list {"Auto", 1, 2, 3, 4, 5, 6} with title "Chess Helper" with prompt "Please select difficulty level:" default items "Auto")
if difficulty is false then error number -128
set difficulty to difficulty as text
if difficulty is not "Auto" then
set difficulty to difficulty as number
if difficulty is less than 4 then
set depth to (difficulty * 4 - 3)
else
set depth to (difficulty + 7)
end if
end if
end if
serialport write mode to arduino
else if inText is equal to "I" then
say "Incorrect chess position" without waiting until completion
else if inText is equal to "R" then
say "Ready to start" with stopping current speech
else if inText is equal to "T" then
say "Please enter the time limit"
display dialog "Enter the time limit in minutes:" with title "Chess Helper" default answer "" buttons {"Cancel", "Start"} default button "Start" cancel button "Cancel" with icon file ((path to desktop as text) & "Chess Helper.app:Contents:Resources:chess clock.icns")
set timeLimit to text returned of result as number
serialport write timeLimit to arduino
tell application "Terminal"
activate
do script "/Applications/Chess.app/Contents/Resources/sjeng.ChessEngine | tee ~/Desktop/Chess/log.txt" in window 1
delay 0.1
end tell
if mode is equal to 1 then
tell application "Terminal" to do script "force" in window 1
else
set totalMoves to 40
set totalCalcTime to round (timeLimit - totalMoves * 0.2)
set calcTime to round (timeLimit * 60 / totalMoves - 12)
if calcTime is equal to 0 then set calcTime to -1
if difficulty is less than 4 and calcTime is greater than 1 then set calcTime to 1
if difficulty is 4 and calcTime is greater than 4 then set calcTime to 4
if difficulty is 5 and calcTime is greater than 8 then set calcTime to 8
if difficulty is 6 and calcTime is greater than 16 then set calcTime to 16
tell application "Terminal"
if difficulty is "Auto" then
do script "level " & totalMoves & " " & totalCalcTime & " 0" in window 1
else
--do script "sd " & 1 in window 1
do script "sd " & depth in window 1
do script "st " & calcTime in window 1
end if
delay 0.1
end tell
if mode is equal to 2 then
set checkmate1 to "White Mates"
set checkmate2 to "Black Mates"
else if mode is 3 or mode is 4 then
set checkmate1 to "Black Mates"
set checkmate2 to "White Mates"
tell application "Terminal" to do script "go" in window 1
delay 0.01
set calcTime to readCalcTime()
delay (calcTime + 0.1)
checkExtension(calcTime)
set comMove to readComMove(804)
serialport write comMove to arduino
--set turn to (turn + 1)
end if
end if
else if (inText is "F1" or inText is "F2") and mode is 4 then
tell application "Terminal" to do script "go" in window 1
delay 0.01
set calcTime to readCalcTime()
delay (calcTime + 0.1)
checkExtension(calcTime)
set comText to readLog(823, 27)
if comText contains "White Mates" or comText contains "Black Mates" then
if comText contains "White Mates" then
set checkmate to "White Mates"
else
set checkmate to "Black Mates"
end if
set comMove to readComMove(822)
serialport write comMove to arduino
delay 0.1
serialport write (text 1 through 4 of checkmate) to arduino
else if comText contains "3 fold repetition" then
set comMove to readComMove(832)
serialport write comMove to arduino
delay 0.1
serialport write "Draw" to arduino
else if comText contains "Draw" then
serialport write "Draw" to arduino
else
set comMove to readComMove(804)
serialport write comMove to arduino
end if
do shell script "echo " & comMove & " >> ~/Desktop/Chess/history.log"
else if inText is equal to "C" then
say "Checkmate"
end if
set validity to true
set fl to {"1", "2", "3", "4", "5", "6", "7", "8"}
set rk to {"a", "b", "c", "d", "e", "f", "g", "h"}
if (length of inText) is not 4 and (length of inText) is not 5 then
set validity to false
else
repeat with i from 1 to 4
if i = 2 or i = 4 then
if character i of inText is not in fl then
set validity to false
end if
else
if character i of inText is not in rk then
set validity to false
end if
end if
end repeat
end if
if validity is equal to true then
--if turn is 2 then tell application "Terminal" to do script "sd " & depth in window 1
set myMove to inText
tell application "Terminal" to do script myMove in window 1
if mode is equal to 1 then
delay 0.01
set comText to readLog(818, 12)
if comText is equal to "Illegal move" then
serialport write "Ille" to arduino
delay 0.1
say "Invalid Move"
else
serialport write "Lega" to arduino
end if
else if mode is 2 or mode is 3 then
delay 0.01
set calcTime to readCalcTime()
delay 0.01
set comText1 to readLog(818, 12)
--set comText2 to readLog(812, 11)
if comText1 is equal to "Illegal move" then
serialport write "Ille" to arduino
delay 0.1
say "Invalid Move"
else
serialport write "Lega" to arduino
(*
if comText2 is checkmate1 then
delay 0.1
serialport write (text 1 through 4 of comText2) to arduino
delay 0.1
say "Checkmate"
else
*)
delay (calcTime + 0.1)
checkExtension(calcTime)
set comText2 to readLog(823, 27)
if comText2 contains checkmate1 then
serialport write (text 1 through 4 of checkmate1) to arduino
delay 0.1
say "Checkmate"
else if comText2 contains checkmate2 then
set comMove to readComMove(822)
serialport write comMove to arduino
delay 0.1
serialport write (text 1 through 4 of checkmate2) to arduino
else if comText2 contains "3 fold repetition" then
set comMove to readComMove(832)
serialport write comMove to arduino
delay 0.1
serialport write "Draw" to arduino
else if comText2 contains "Draw" then
serialport write "Draw" to arduino
else
set comMove to readComMove(804)
serialport write comMove to arduino
end if
do shell script "echo " & comMove & " >> ~/Desktop/Chess/history.log"
--end if
end if
end if
--set turn to (turn + 1)
end if
end repeat
on readLog(x, y)
set fp to open for access file ((path to desktop as text) & "Chess:log.txt")
set logLength to get eof fp
set logText to read fp from (logLength - x) for y
close access fp
return logText
end readLog
on readComMove(p)
set comMove to readLog(p, 4)
if character 4 of comMove is "q" then set comMove to readLog(p + 1, 4)
return comMove
end readComMove
on readCalcTime()
set calcTime to 0
set logText to readLog(400, 400)
if logText contains "Time for move" then
set ind to ((offset of "Time for move" in logText) + 16)
if character ind of logText is "-" then return 0
set numText to ""
repeat with i from ind to (ind + 4)
set digit to character i of logText
try
digit as number
set numText to (numText & digit)
end try
end repeat
set calcTime to numText as number
set calcTime to (calcTime / 100)
end if
do shell script "echo " & calcTime & " >> ~/Desktop/Chess/history.log"
return calcTime
end readCalcTime
on checkExtension(calcTime)
set logText to readLog(100, 100)
if logText contains "Extended" then
set ind to ((offset of "to" in logText) + 3)
set numText to ""
repeat with i from ind to (ind + 4)
set digit to character i of logText
try
digit as number
set numText to (numText & digit)
end try
end repeat
set newCalcTime to numText as number
set newCalcTime to (newCalcTime / 100)
set extraTime to (newCalcTime - calcTime)
delay extraTime
end if
end checkExtension
on quit
tell application "Terminal" to quit
serialport close theport
continue quit
end quit