/
ttg.py
323 lines (256 loc) · 7.56 KB
/
ttg.py
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
# import libraries
import wiringpi2 as wiringpi
import led
# global constants
LEFT_BUTTON_PIN = 18
RIGHT_BUTTON_PIN = 19
EMPTY = ' '
RED = 'Red'
GREEN = 'Green'
UP = 0
DOWN = 1
NOT_PRESSED = 0
PRESSED = 1
PLAYING = 0
READY = 1
ANIMATING = 2
GAME_OVER = 3
READY_FLASHES = 100
FLASH_ON = 20000
FLASH_OFF = 10000
OFF = -1
END = -2
ANIMATION = [
[0],[3],[6],[OFF,0],[OFF,3],[OFF,6], # left drop
[1],[4],[7],[OFF,1],[OFF,4],[OFF,7], # mid drop
[2],[5],[8],[OFF,2],[OFF,5],[OFF,8], # right drop
[0],[1],[2],[5],[4],[3],[6],[7],[8], # snake on
[OFF,0],[OFF,1],[OFF,2],[OFF,5],[OFF,4],[OFF,3],[OFF,6],[OFF,7],[OFF,8], # snake off
[0,1,2], [3,4,5], [6,7,8], [OFF,0,1,2], [OFF,3,4,5], [OFF,6,7,8], # top down wipe
[0,3,6], [1,4,7], [2,5,8], [OFF,0,3,6], [OFF,1,4,7], [OFF,2,5,8], # left right wipe
[0], [1,3], [2,4,6], [5,7], [8], # diag on
[OFF,0], [OFF,1,3], [OFF,2,4,6], [OFF,5,7], [OFF,8], # diag off
[END] ]
ANIMATION_SPEED = 6000
SHORT_PAUSE = 150
MED_PAUSE = 350
LONG_PAUSE = 500
# global variables
Turn = GREEN
Board = []
#####
def winningAnimation( winningLine ):
# convert line to pins
winningPins = winningLine
if Turn == GREEN:
for i in range( 0, 3 ):
winningPins[i] = winningLine[i] + 9
wiringpi.delay( LONG_PAUSE )
led.ledsOff( winningPins, MED_PAUSE )
# flash all three
for i in range ( 0, 2 ):
led.ledsOnOff( winningPins, MED_PAUSE )
led.ledsOn( winningPins, LONG_PAUSE )
# flash one at a time
for i in range ( 0, 2 ):
led.ledsOff( winningPins, SHORT_PAUSE )
for j in range ( 0, 3 ):
led.ledOn( winningPins[ j ], MED_PAUSE )
wiringpi.delay( SHORT_PAUSE )
led.ledsOff( winningPins, SHORT_PAUSE )
# flash all three again
for i in range ( 0, 2 ):
led.ledsOnOff( winningPins, SHORT_PAUSE )
# turn back on for final board
led.ledsOn( winningPins )
def gameTiedAnimation():
wiringpi.delay( LONG_PAUSE )
# flip the colors back and forth
for i in range( 0, 3 ):
drawBoard( True )
wiringpi.delay( MED_PAUSE )
drawBoard()
wiringpi.delay( MED_PAUSE )
def resetAnimation():
# flash existing board
for i in range( 0, 2 ):
led.allOff()
led.wiringpi.delay( SHORT_PAUSE )
drawBoard()
led.wiringpi.delay( SHORT_PAUSE )
led.allOff()
led.wiringpi.delay( LONG_PAUSE )
def drawBoard( invert=False):
# handle board inversion
redOffset = 9 if invert else 0
greenOffset = 0 if invert else 9
led.allOff()
for i in range( 0, 9 ):
if Board[i] == RED:
led.ledOn( i + redOffset )
elif Board[i] == GREEN:
led.ledOn( i + greenOffset )
def checkForWin():
winLines = [
[ 0, 1, 2 ], # top row
[ 3, 4, 5 ], # middle row
[ 6, 7, 8,], # bottom row
[ 0, 3, 6,], # left col
[ 1, 4, 7,], # middle col
[ 2, 5 ,8,], # right col
[ 0, 4, 8,], # diagonal 1
[ 2, 4, 6,] # diagonal 2
]
for line in winLines :
if Board[ line[0] ] == Turn and \
Board[ line[1] ] == Turn and \
Board[ line[2] ] == Turn:
print " + " + Turn + " wins!"
return line
return False
def nextTurn( printTurn=True ):
global Turn
if Turn == RED:
Turn = GREEN
else:
Turn = RED
if printTurn:
print Turn + "'s Turn..."
def squareToPin( square ):
if Turn == RED:
return square
else:
return square + 9
def nextEmpty( square ):
count = 0
while count < 9:
square += 1
if square > 8:
square = 0
if Board[ square ] == EMPTY:
return square, squareToPin( square )
count += 1
# no empties: a full board
return -1, -1
def playGame():
# set initial conditions
global Turn, Board
Board = [EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY]
moveButtonState = selectButtonState = NOT_PRESSED
nextTurn()
GameState = READY
curSquare = flashCount = readyCount = 0
curPin = squareToPin( curSquare )
led.allOff()
# the game loop
while GameState != GAME_OVER:
flashCount += 1 # increment the flashing led "timer"
if GameState == ANIMATING:
# every READY_ANIMATION_SPEED flashCounts, draw one frame of READY_ANIMATION
if flashCount % ANIMATION_SPEED == 0:
curLeds = ANIMATION[( flashCount / ANIMATION_SPEED ) - 1]
if curLeds[0] == END:
# last frame of ANIMATION: reset and back to READY
flashCount = 0
led.allOff()
GameState = READY
wiringpi.delay( MED_PAUSE )
else:
if curLeds[0] == OFF:
# turn these leds off
led.ledsOff( curLeds[1:] )
else:
# turn these leds on
led.ledsOn( curLeds)
else:
# flash current square
if flashCount == 1:
led.ledOn( curPin )
elif flashCount == FLASH_ON:
led.ledOff( curPin )
elif flashCount > FLASH_ON + FLASH_OFF:
flashCount = 0
if GameState == READY:
# each flash brings us closer to ANIMATION
readyCount += 1
# get button inputs
moveButton = wiringpi.digitalRead( LEFT_BUTTON_PIN )
selectButton = wiringpi.digitalRead( RIGHT_BUTTON_PIN )
# --- handle select button ---
if selectButton == DOWN:
if selectButtonState == NOT_PRESSED:
# act right away on button press
selectButtonState = PRESSED
flashCount = readyCount = 0
if GameState == ANIMATING:
# stop the animation
GameState = READY
led.allOff()
else:
GameState = PLAYING
# flash selected square and turn it on
for i in range( 0, 3 ):
led.flashOne( curPin, SHORT_PAUSE )
led.ledOn( curPin )
# update Board and check for game over
Board[ curSquare ] = Turn
winningLine = checkForWin()
if winningLine:
# we have a winner!
GameState = GAME_OVER
# flash winning line
winningAnimation( winningLine )
else:
# no winner, continue on
nextTurn()
curSquare, curPin = nextEmpty( 8 )
if curSquare < 0:
# no more empty squares? it's a tie!
GameState = GAME_OVER
print " + It's a tie!"
gameTiedAnimation()
nextTurn( False ) # flip first player on ties
# clear the other button's state
moveButton = UP
moveButtonState = NOT_PRESSED
else:
if selectButtonState == PRESSED:
selectButtonState = NOT_PRESSED
# pause to avoid button flooding
wiringpi.delay( SHORT_PAUSE )
# --- handle move button ---
if moveButton == DOWN:
if moveButtonState == NOT_PRESSED:
# act right away on button press
moveButtonState = PRESSED
flashCount = readyCount = 0
if GameState == ANIMATING:
# stop the animation
GameState = READY
led.allOff()
else:
# advance the flashing led to the next empty square
led.ledOff( squareToPin( curSquare ) )
curSquare, curPin = nextEmpty( curSquare )
# clear the other button's state
selectButtonState = NOT_PRESSED
else:
if moveButtonState == PRESSED:
moveButtonState = NOT_PRESSED
# pause to avoid button flooding
wiringpi.delay( SHORT_PAUSE )
if GameState == READY and readyCount > READY_FLASHES:
# too long in READY state? play the ANIMATION for attention!
GameState = ANIMATING
readyCount = 0
# GAME_OVER
led.wiringpi.delay( LONG_PAUSE * 8 )
resetAnimation()
#####
def main():
led.init()
print " ++ Tic-Tac-GPIO! ++"
while 2+2 == 4:
playGame()
if __name__ == "__main__":
main()