简体   繁体   中英

I've overcomplicated adding variables for my dice roll game

I'm doing a dice roll assignment. The rules are:

  • The points rolled on each player's dice are added to their score.
  • If the total is an even number, an additional 10 points are added to their score.
  • If the total is an odd number, 5 points are subtracted from their score.
  • If they roll a double, they get to roll one extra die and get the number of points rolled added to their score.
  • The score of a player cannot go below 0 at any point.
  • The person with the highest score at the end of the 5 rounds wins.

Basically, what I've done is created variables of what 2 separate players roll in 2 rounds of the game, now I'm trying to add these variables together so that the player can have their total for round 1 and 2 combined. My teacher had mentioned something about a while function but that was all he was allowed to say, that plus I'm not sure how to do that.

I feel like what I've done is overly complicated, plus the fact it doesn't even work.

if (round2scoreP1 % 2) == 0 + (round1scoreP1 % 2) == 0:
    addedscoreround2P1even=(totalround1scoreP1even)+(totalround2scoreP1even)
    print(username1,"'s total for round 1 and 2 is",addedscoreround2P1even,".")
elif (round1scoreP1 % 2) != 0 + (round2scoreP1 % 2) != 0:
    addedscoreround2P1odd=(totalround1scoreodd)+(totalround2scoreodd)
    print(username1,"'s total for round 1 and 2 is",addedscoreround2P1odd,".")
elif (round1scoreP1 % 2) == 0 + (round2scoreP1 % 2) != 0:
    addedscoreround2P1evenodd=(totalround1scoreP1even)+(totalround1scoreP1odd)
    print(username1,"'s total for round 1 and 2 is",addedscoreround2P1evenodd,".")
elif (round1scoreP1 % 2) != 0 + (round2scoreP1 % 2) == 0:
    addedscoreround2P1oddeven=(totalround1scoreP1odd)+(totalround1scoreP1even)
    print(username1," obtained",addedscoreround2P1oddeven,".")

I'm not going to write you the whole assignment, but you could consider using a Player-class to store the rolls and total scores in:

class Player():
    
    def __init__(self):
        self.rolls = []
        self.total_score = 0

    def roll_dice():
        self.rolls.append(random.randint(1, 6))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM