-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsolitaire.py
More file actions
executable file
·365 lines (321 loc) · 12.5 KB
/
solitaire.py
File metadata and controls
executable file
·365 lines (321 loc) · 12.5 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
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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
__version__ = "$Revision: 417 $, $Date: 2011-03-13 17:14:04 +0100 (zo, 13 mrt 2011) $, $Author: quintijn $"
# (unimacro - natlink macro wrapper/extensions)
# (c) copyright 2003 Quintijn Hoogenboom (quintijn@users.sourceforge.net)
# Ben Staniford (ben_staniford@users.sourceforge.net)
# Bart Jan van Os (bjvo@users.sourceforge.net)
#
# This file is part of a SourceForge project called "unimacro" see
# http://unimacro.SourceForge.net).
#
# "unimacro" is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License, see:
# http://www.gnu.org/licenses/gpl.txt
#
# "unimacro" is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; See the GNU General Public License details.
#
# "unimacro" makes use of another SourceForge project "natlink",
# which has the following copyright notice:
#
# Python Macro Language for Dragon NaturallySpeaking
# (c) Copyright 1999 by Joel Gould
# Portions (c) Copyright 1999 by Dragon Systems, Inc.
#
# solitaire.py
#
# Written by: Quintijn Hoogenboom (QH softwaretraining &
# advies),2002, revised October 2003
# march 2011: change sol to solitaire
"""Grammar for playing solitaire (patience) hands-free
Extensive use is made of mouse (dragging) routines.
"""
import natlink, win32gui, types, time, os, os.path, win32api
natqh = __import__('natlinkutilsqh')
natut = __import__('natlinkutils')
natbj = __import__('natlinkutilsbj')
from actions import doAction as action
from actions import doKeystroke as keystroke
#aantal carden en aantal stapelplaatsen:
cnum = range(1,8)
snum = range(1,5)
#card sizes and spacing:
#kwidth, kheight = 72, 96
kwidth, kheight = 93, 121
hspace, vspace = 10,6
cardhspace, cardvspace = 5, 6 # space in stack (horizontal) and on stacks (vertical)
# for mouse lower or higher:
deltaY = 14
# waiting times:
pauzesDelta = 0.1
minPause = 0.5
language = natqh.getLanguage()
ancestor = natbj.DocstringGrammar
class ThisGrammar(ancestor):
name = 'solitaire'
iniIgnoreGrammarLists = ['snum', 'cnum'] # are set in this module
#gramSpec = """ # in the rules docstrings!
# """
def initialize(self):
global language
if self.load(self.gramSpec):
self.setNumbersList("cnum", cnum)
self.setNumbersList("snum", snum)
print 'grammar solitaire (%s) active'% self.name
self.prevHandle = -1
self.stackAuto = 0
self.pause = 1
self.inTimer = 0
self.pauseTime = 4
else:
language = ""
def cancelMode(self):
#self.setExclusive(0)
if self.inTimer:
print 'cancel timer'
natlink.setTimerCallback(None,0)
self.inTimer = 0
def onTimer(self):
if natlink.getMicState() != 'on':
print 'mic switched off, cancel timer'
self.cancelMode()
return
modInfo = natlink.getCurrentModule()
if modInfo[2] != self.prevHandle:
print 'window handle changed, cancel timer'
self.cancelMode()
return
print 'in onTimer: %.1f'% time.clock()
self.rule_newcard([])
def gotBegin(self,moduleInfo):
print 'gotbegin, cancelmode'
self.cancelMode()
winHandle = moduleInfo[2]
if self.prevHandle == winHandle: return
self.prevHandle = winHandle
if moduleInfo[0].lower().find('solitaire.exe') > 0 and natqh.isTopWindow(moduleInfo[2]):
if self.checkForChanges:
print 'grammar solitaire (%s) checking the inifile'% self.name
self.checkInifile()
print 'self.mayBeSwitchedOn: %s'% self.mayBeSwitchedOn
exclusive = self.mayBeSwitchedOn == 'exclusive'
self.activateAll(exclusive=exclusive)
else:
self.deactivateAll()
# Bij het initialiseren wordt de horizontale spatiejering uitgerekend.
# Er wordt van uitgegaan dat het venster niet te smal staat.
def gotResultsInit(self,words,fullResults):
"""initialize screen sizes (guesses)
"""
global hspace, vspace, cardhspace, cardvspace
clrect = win32gui.GetClientRect(self.prevHandle)
xMax, yMax = clrect[2]-1, clrect[3]-1
hspace = (xMax - 7*kwidth)/8
cardhspace = int(hspace/2)
cardvspace = vspace
## print 'hSpace: %s, xMax: %s, yMax: %s'% (hspace, xMax, yMax)
def rule_auto(self, words):
"stack automatically"
self.stackAuto = 1
def rule_exclusive(self, words):
"solitaire [non] exclusive"
#switches on auto stacking, {ctrl+a} after each move
self.prevHandle = None # to reactivate in gotBegin
self.cancelMode()
if self.hasCommon(words, 'non'):
self.mayBeSwitchedOn = 1
else:
self.mayBeSwitchedOn = 'exclusive'
# say stop, or even undo the last action
def rule_stop_undo(self, words):
"'hold on'|stop|undo"
self.cancelMode()
if self.hasCommon(words, 'undo'):
keystroke('{ctrl+z}')
# Go to a card and click on it
def rule_card(self, words):
"card {cnum}"
k = self.getNumberFromSpoken(words[-1])
self.moveTo(cardpos(k))
natqh.buttonClick()
# Draw a new card and position on the last opened card
def rule_newcard(self, words):
"'new card'|next|continue"
self.moveTo(firstrowpos(1))
natqh.buttonClick()
self.moveTo(firstrowpos(2))
if self.hasCommon(words, 'continue'):
timeEachMilliseconds = max(1, self.pauseTime)*500
print 'set the timer to %s'% timeEachMilliseconds
natlink.setTimerCallback(self.onTimer, timeEachMilliseconds)
self.inTimer = 1
# Sometimes the mouse is a bit too high (if the stack grows longer)
# with this command you position the mouse a bit higher or lower
def rule_lower(self,words):
"mouse (lower|higher) [{cnum}]"
n = self.getNumberFromSpoken(words[-1]) or 1
d = n*deltaY
if self.hasCommon(words, 'lower'):
pass
elif self.hasCommon(words, 'higher'):
d = -d
natqh.doMouse(0,2,0,d,0)
# Deze grammatica regel stelt de waittijd na elk (deel) van een
# commando in. Zie "pauzes" bovenin dit standpunt
def rule_wait(self, words):
"pauses (0 |{cnum})"
i = int(words[-1])
self.pause = i
# Drag a card from the current position to one of the card columns or
# to one of the 4 stacks
def rule_to(self, words):
"to ((stack {snum})|{cnum})"
to = self.getNumberFromSpoken(words[-1])
natqh.rememberMouse()
if self.hasCommon(words, 'stack'):
to = firstrowpos(to+3)
else:
to = cardpos(to)
self.dragTo(to)
natqh.cancelMouse()
natqh.buttonClick()
def rule_testposition(self, words):
"test position ((stack {snum})|{cnum})"
#test the stack and piles positions
#lefttop = (0,0)
#natqh.doMouse(0, 5, 0,0, 'move')
#x, y = natqh.getMousePosition(1)
#print 'screen x,y: %s, %s'% (x,y)
#x, y = natqh.getMousePosition(5)
#print 'client: x,y: %s, %s'% (x,y)
#time.sleep(2)
to = self.getNumberFromSpoken(words[-1])
natqh.rememberMouse()
if self.hasCommon(words, 'stack'):
to = firstrowpos(to+3)
else:
to = cardpos(to)
print 'position: %s'% repr(to)
self.moveTo(to)
def rule_cardnumto(self, words):
"[card] {cnum} to ((stack {snum})|{cnum})"
#card with number to a stack or another card column
#
#the first word is optional,and is recognised with the function
#self.hasCommon, which can handle translations or synonyms
print 'cardnumto: %s'% words
natqh.rememberMouse()
if self.hasCommon(words[0],['card']):
ww = words[1:]
else:
ww = words[:]
# go to numbered card with mouse:
From = self.getNumberFromSpoken(words[0])
self.moveTo(cardpos(From))
natqh.rememberMouse()
to = self.getNumberFromSpoken(words[-1])
# check if you go to a stack or another card column:
if self.hasCommon(words, 'stack'):
to = firstrowpos(to+3)
else:
to = cardpos(to)
self.dragTo(to)
natqh.cancelMouse()
natqh.buttonClick()
def rule_cardto(self, words):
"card to ((stack {snum})|{cnum})"
#drag the last drawn card to a stack or to a pile
print 'cardto: %s'% words
natqh.rememberMouse()
self.moveTo(firstrowpos(2))
to = self.getNumberFromSpoken(words[-1])
if self.hasCommon(words, 'stack'):
to = firstrowpos(to+3)
else:
to = cardpos(to)
self.dragTo(to)
natqh.cancelMouse()
natqh.buttonClick()
def gotResults(self,words,fullResults):
"""if stack auto, do after each move a {ctrl+a}"""
if self.stackAuto:
keystroke('{ctrl+a}')
# paused a given number of milliseconds
# if this variable = 0 no pausing is done.
# The length of the pauses can be given with
# command "pauzes (0 | 1 | 2 | 3 | 4 | 5 | 6 | 7)".
# At the top of this file the increment of the pausing is given.
def Wait(self, t=None):
if not t:
natqh.Wait(self.pause*pauzesDelta+minPause)
else:
natqh.Wait(t)
# move to the given position and have a short wait:
def moveTo(self, pos):
natqh.doMouse(0,5, pos[0], pos[1], 'move')
self.Wait()
# drag from current position to the new position
# Pause a few times, dependent on the pause state.
def dragTo(self, pos):
xold,yold = natqh.getMousePosition(5)
print 'hold down: %s, %s'% (xold, yold)
natqh.doMouse(0,5,xold, yold, 'down')
xyincr = 50
nstepsx = int(abs(pos[0]-xold)/xyincr)
nstepsy = int(abs(pos[1]-yold)/xyincr)
nsteps = max(nstepsx, nstepsy)
print 'nstepsx: %s, nstepsy: %s, nsteps: %s'% (nstepsx, nstepsy, nsteps)
ysteps = int((pos[1]-yold)/nsteps)
xsteps = int((pos[0]-xold)/nsteps)
x, y = xold, yold
for i in range(nsteps):
x += xsteps
y += ysteps
natqh.doMouse(0,5, x, y, 'move')
self.Wait(0.01)
if x != pos[0] or y != pos[1]:
print 'final move: %s, %s, %s, %s'% (x, pos[0], y, pos[1])
natqh.doMouse(0,5, pos[0], pos[1], 'move')
self.Wait(0.01)
natqh.doMouse(0,5, pos[0], pos[1], 'move')
self.Wait(0.01)
natqh.releaseMouse()
self.Wait()
# Het berekenen van de coordinaten van cardnummer,i kan liggen
# tussen 1 en
def cardpos(i):
x = i*(hspace+kwidth)-0.5*kwidth
y = int(2*vspace) + int(2.1*kheight)
return x,y
# Het berekenen van een positie van een card op de eerste rij.
# Positie 1 is de stapel waaruit gedeeld wordt,
# positie 2 is het stapeltje gedeelde carden,
# en positie i + 3 is de positie van een stapel (i tussen 1 en 4)
def firstrowpos(i):
"""calculate the positions of the first row
1 is pile of closed cards (to click on for new card)
2 is last cards drawn
i+3 is stack (stapel) 1,2,3,4
"""
x = i*(hspace+kwidth)-0.5*kwidth
if i == 2:
x += cardhspace
y = vspace + cardvspace + int(0.5*kheight)
return x,y
# standard stuff Joel (adapted for possible empty gramSpec, QH, unimacro)
thisGrammar = ThisGrammar()
if thisGrammar.gramSpec:
thisGrammar.initialize()
else:
thisGrammar = None
def unload():
global thisGrammar
if thisGrammar: thisGrammar.unload()
thisGrammar = None
def changeCallback(type,args):
# not active without special version of natlinkmain,
# call the cancelMode, to switch off exclusive mode when mic toggles:
if ((type == 'mic') and (args=='on')):
return # check WAS in natlinkmain...
if thisGrammar:
print 'cancel'
thisGrammar.cancelMode()