Skip to content

Commit 758280b

Browse files
author
Kevin J Walters
committed
Changing evaluateRound() to return value to a form of enumerated value based on const() numbers for WIN, DRAW, LOSE, INVALID.
Maintains the use of booleans on the call to rps_display.showPlayerVPlayer(). adafruit#1185
1 parent ea82f2b commit 758280b

File tree

1 file changed

+31
-31
lines changed

1 file changed

+31
-31
lines changed

CLUE_Rock_Paper_Scissors/advanced/clue-multi-rpsgame.py

Lines changed: 31 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
import os
3434
import random
3535

36+
from micropython import const
3637
import board
3738
import digitalio
3839

@@ -244,38 +245,34 @@ def tftGizmoPresent():
244245
KEY_ENLARGE = 256 // KEY_SIZE // 8
245246

246247

248+
WIN = const(1)
249+
DRAW = const(2) # AKA tie
250+
LOSE = const(3)
251+
INVALID = const(4)
252+
247253
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
251257
try:
252258
mine_lc = mine.lower()
253259
yours_lc = yours.lower()
254260
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
256265

257-
r_win = r_draw = r_void = False
266+
# Both inputs are valid choices if we got this far
258267
# 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
277274

278-
return (r_win, r_draw, r_void)
275+
return LOSE
279276

280277

281278
rps_display.playerListScreen()
@@ -508,12 +505,13 @@ def addPlayer(name, addr_text, address, ad):
508505
for p_idx0, (p0_name, _) in enumerate(players[:len(players) - 1]):
509506
for p_idx1, (p1_name, _) in enumerate(players[p_idx0 + 1:], p_idx0 + 1):
510507
# 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])
513510

514511
# this_player is used to control incrementing the summary
515512
# for the tally for this local player
516513
this_player = 0
514+
void = False
517515
if p_idx0 == 0:
518516
this_player = 1
519517
p0_ch_idx = None
@@ -529,15 +527,17 @@ def addPlayer(name, addr_text, address, ad):
529527
# showPlayerVPlayer takes int index values for RPS
530528
rps_display.showPlayerVPlayer(p0_name, p1_name, p_idx1,
531529
p0_ch_idx, p1_ch_idx,
532-
win, draw, void)
530+
result == WIN,
531+
result == DRAW,
532+
result == INVALID or void)
533533

534-
if void:
534+
if result == INVALID or void:
535535
voids += this_player
536-
elif draw:
536+
elif result == DRAW:
537537
draws += this_player
538538
scores[p_idx0] += 1
539539
scores[p_idx1] += 1
540-
elif win:
540+
elif result == WIN:
541541
wins += this_player
542542
scores[p_idx0] += 2
543543
else:
@@ -547,7 +547,7 @@ def addPlayer(name, addr_text, address, ad):
547547
d_print(2,
548548
p0_name, player_choices[p_idx0], "vs",
549549
p1_name, player_choices[p_idx1],
550-
"win", win, "draw", draw, "void", void)
550+
"result", result)
551551

552552
print("Game {:d}, round {:d}, wins {:d}, losses {:d}, draws {:d}, "
553553
"void {:d}".format(game_no, round_no, wins, losses, draws, voids))

0 commit comments

Comments
 (0)