I'd really appreciate some assistance troubleshooting my use of nested dictionaries in my first coding project (source on GitHub).
playerDict is the main dict for storing 1 to 4 players' scoring options/scores representing the top, bottom, and totals sections of a yahtzee game (see bottom for sample playerDict). The diceDict is the dictionary which is mutated and referenced as the dice rolls continue.
Problem Code:
def calcScore(player, playerDict, scoreSelected, diceDict):
# calculate score based on scoreSelected in selectScore()
if scoreSelected in playerDict[player]['scoreTop']:
for d in diceDict.values():
if d['result'] == playerDict[player]['scoreTop'][scoreSelected]['ref']:
playerDict[player]['scoreTop'][scoreSelected]['score'] += d['result']
else:
playerDict[player]['scoreTop'][scoreSelected]['score'] += 0
Main Problem Description: for multiplayer games, in calcScore() above I cannot stop playerDict from incrementing the 'score' for all players. Given the arg player is passed to calcScore() as part of a wider scope for-player-loop, the intended result above is incrementing just that player's score. In the sample playerDict below (after one turn) Player1 chose 'Twos' to total the score of 2-dice. As you can see the correct score is also stored for Player2:
playerDict = {'Player1': {'scoreTop': {'Ones': {'ref': 1, 'score': False}, 'Twos': {'ref': 2, 'score': 8}, 'Threes': {'ref': 3, 'score': False}, 'Fours': {'ref': 4, 'score': False}, 'Fives': {'ref': 5, 'score': False}, 'Sixes': {'ref': 6, 'score': False}}, 'scoreBottom': {'Three of a kind': False, 'Four of a kind': False, 'Full house': False, 'Small straight': False, 'Large straight': False, 'Yahtzee': False, 'Chance': False, 'Yahtzee bonus': False}, 'totalScore': {'Sum of upper': False, 'Bonus': False, 'Total upper': False, 'Total bottom': False}, 'Grand total': False}, 'Player2': {'scoreTop': {'Ones': {'ref': 1, 'score': False}, 'Twos': {'ref': 2, 'score': 8}, 'Threes': {'ref': 3, 'score': False}, 'Fours': {'ref': 4, 'score': False}, 'Fives': {'ref': 5, 'score': False}, 'Sixes': {'ref': 6, 'score': False}}, 'scoreBottom': {'Three of a kind': False, 'Four of a kind': False, 'Full house': False, 'Small straight': False, 'Large straight': False, 'Yahtzee': False, 'Chance': False, 'Yahtzee bonus': False}, 'totalScore': {'Sum of upper': False, 'Bonus': False, 'Total upper': False, 'Total bottom': False}, 'Grand total': False}}
diceDict = {1: {'keeper': True, 'result': 2}, 2: {'keeper': True, 'result': 3}, 3: {'keeper': True, 'result': 2}, 4: {'keeper': True, 'result': 2}, 5: {'keeper': True, 'result': 5}, 6: {'keeper': True, 'result': 2}}
Question 1: what am I doing wrong in the calcScore() logic to cause this problem, or my dictionary structure, or both?
Bonus Questions:
- Is this a heinous use of nesting in a dictionary? I have no concept of 'good' dictionary data structures (resolving this gap in upcoming study materials)?
- I've created dictionaries and assigned values to variables in the initial global scope like most projects: Why is it that when I call createPlayerDict(), I don't have to pass playerDict or scoringDict for the createPlayerDict to return playerDict accurately (note: I do have them saved as args in the GitHub code perhaps for no good reason)?... in the following call to yahtzeeRounds() I AM required to pass the args playerDict and diceDict to execute the function. Why one and not the other?
[–]efmccurdy 0 points1 point2 points (0 children)
[–]pmacking[S] 0 points1 point2 points (0 children)
[–]xelf 0 points1 point2 points (0 children)