33
33
import os
34
34
import random
35
35
36
+ from micropython import const
36
37
import board
37
38
import digitalio
38
39
@@ -244,38 +245,34 @@ def tftGizmoPresent():
244
245
KEY_ENLARGE = 256 // KEY_SIZE // 8
245
246
246
247
248
+ WIN = const (1 )
249
+ DRAW = const (2 ) # AKA tie
250
+ LOSE = const (3 )
251
+ INVALID = const (4 )
252
+
247
253
def evaluateRound (mine , yours ):
248
- """Determine who won the game based on the two strings mine and yours.
249
- Returns three booleans (win, draw, void) ."""
250
- # Return with void at True if any input is None
254
+ """Determine who won the round in this game based on the two strings mine and yours.
255
+ Returns WIN, DRAW, LOSE or INVALID for bad input ."""
256
+ # Return INVALID if any input is None
251
257
try :
252
258
mine_lc = mine .lower ()
253
259
yours_lc = yours .lower ()
254
260
except AttributeError :
255
- return (False , False , True )
261
+ return INVALID
262
+
263
+ if mine_lc not in CHOICES or yours_lc not in CHOICES :
264
+ return INVALID
256
265
257
- r_win = r_draw = r_void = False
266
+ # Both inputs are valid choices if we got this far
258
267
# pylint: disable=too-many-boolean-expressions
259
- if (mine_lc == "rock" and yours_lc == "rock"
260
- or mine_lc == "paper" and yours_lc == "paper"
261
- or mine_lc == "scissors" and yours_lc == "scissors" ):
262
- r_draw = True
263
- elif (mine_lc == "rock" and yours_lc == "paper" ):
264
- pass # r_win default is False
265
- elif (mine_lc == "rock" and yours_lc == "scissors" ):
266
- r_win = True
267
- elif (mine_lc == "paper" and yours_lc == "rock" ):
268
- r_win = True
269
- elif (mine_lc == "paper" and yours_lc == "scissors" ):
270
- pass # r_win default is False
271
- elif (mine_lc == "scissors" and yours_lc == "rock" ):
272
- pass # r_win default is False
273
- elif (mine_lc == "scissors" and yours_lc == "paper" ):
274
- r_win = True
275
- else :
276
- r_void = True
268
+ if mine_lc == yours_lc :
269
+ return DRAW
270
+ elif (mine_lc == "rock" and yours_lc == "scissors"
271
+ or mine_lc == "paper" and yours_lc == "rock"
272
+ or mine_lc == "scissors" and yours_lc == "paper" ):
273
+ return WIN
277
274
278
- return ( r_win , r_draw , r_void )
275
+ return LOSE
279
276
280
277
281
278
rps_display .playerListScreen ()
@@ -508,12 +505,13 @@ def addPlayer(name, addr_text, address, ad):
508
505
for p_idx0 , (p0_name , _ ) in enumerate (players [:len (players ) - 1 ]):
509
506
for p_idx1 , (p1_name , _ ) in enumerate (players [p_idx0 + 1 :], p_idx0 + 1 ):
510
507
# evaluateRound takes text strings for RPS
511
- ( win , draw , void ) = evaluateRound (player_choices [p_idx0 ],
512
- player_choices [p_idx1 ])
508
+ result = evaluateRound (player_choices [p_idx0 ],
509
+ player_choices [p_idx1 ])
513
510
514
511
# this_player is used to control incrementing the summary
515
512
# for the tally for this local player
516
513
this_player = 0
514
+ void = False
517
515
if p_idx0 == 0 :
518
516
this_player = 1
519
517
p0_ch_idx = None
@@ -529,15 +527,17 @@ def addPlayer(name, addr_text, address, ad):
529
527
# showPlayerVPlayer takes int index values for RPS
530
528
rps_display .showPlayerVPlayer (p0_name , p1_name , p_idx1 ,
531
529
p0_ch_idx , p1_ch_idx ,
532
- win , draw , void )
530
+ result == WIN ,
531
+ result == DRAW ,
532
+ result == INVALID or void )
533
533
534
- if void :
534
+ if result == INVALID or void :
535
535
voids += this_player
536
- elif draw :
536
+ elif result == DRAW :
537
537
draws += this_player
538
538
scores [p_idx0 ] += 1
539
539
scores [p_idx1 ] += 1
540
- elif win :
540
+ elif result == WIN :
541
541
wins += this_player
542
542
scores [p_idx0 ] += 2
543
543
else :
@@ -547,7 +547,7 @@ def addPlayer(name, addr_text, address, ad):
547
547
d_print (2 ,
548
548
p0_name , player_choices [p_idx0 ], "vs" ,
549
549
p1_name , player_choices [p_idx1 ],
550
- "win " , win , "draw" , draw , "void" , void )
550
+ "result " , result )
551
551
552
552
print ("Game {:d}, round {:d}, wins {:d}, losses {:d}, draws {:d}, "
553
553
"void {:d}" .format (game_no , round_no , wins , losses , draws , voids ))
0 commit comments