colkat406
colkat406

Reputation: 191

scoring problem with Two player dice game

This is a game for two users who roll 2 dice 5 times. If the total of dice is even the player gains 10 points; if it is odd, they lose 5.

total_score2 = 0
total_score1 = 0
rounds = 0
playerOnePoints = 0
playerTwoPoints = 0

total_score2 = total_score2 + playerTwoPoints
total_score1 = total_score1 + playerOnePoints
rounds = rounds + 1
number = random.randint(1,6)
number2 = random.randint(1,6)
playerOnePoints = number + number2
print("-------------------------------------------")
print("Round",rounds)
print("-------------------------------------------")
print("Player 1's turn    Type 'roll' to roll the dice")
userOneInput = input(">>> ")
if userOneInput == "roll":
    time.sleep(0.5)
    print("Player 1's first roll is", number)
print("Player 1's second roll    Type 'roll' to roll the dice")
userOneInput = input(">>> ")
if userOneInput == "roll":
    time.sleep(0.5)
    print("player 1's second roll is", number2)
if playerOnePoints % 2 == 0:
    playerOnePoints = playerOnePoints + 10
    print("Player 1's total is even so + 10 points")
    print("-------------------------------------------")
    print("Player 1 has",playerOnePoints, "points")
else:
    playerOnePoints = playerOnePoints - 5
    print("player 1's total is odd so -5 points")
    print("-------------------------------------------")
    print("Player 1 has",playerOnePoints, "points")
number = random.randint(1,6)
number2 = random.randint(1,6)
playerTwoPoints = number + number2
print("-------------------------------------------")
print("Player 2's turn    Type 'roll' to roll the dice")
userTwoInput = input(">>> ")
if userTwoInput == "roll":
    time.sleep(0.5)
    print("Player 2's first roll is", number)
print("Player 2's second roll    Type 'roll' to roll the dice")
userTwoInput = input(">>> ")
if userTwoInput == "roll":
    time.sleep(0.5)
    print("player 2's second roll is", number2)
if playerTwoPoints % 2 == 0:
    playerTwoPoints = playerTwoPoints + 10
    print("Player 2's total is even so + 10 points")
    print("-------------------------------------------")
    print("Player 2 has",playerTwoPoints, "points")
else:
    playerTwoPoints = playerTwoPoints - 5
    print("player 2's total is odd so -5 points")
    print("-------------------------------------------")
    print("Player 2 has",playerTwoPoints, "points")

The thing that is wrong with this is that say a user rolls 1 and 2 which add up to 3 which is an odd number the game will -5 from the 3 which makes the total -2 but I don't want it to go into minus, I want it so that if they do get mius points it sayes that they get 0 points instead of minus points

Upvotes: 2

Views: 1058

Answers (2)

datadaveshin
datadaveshin

Reputation: 166

An alternative to PSM's answer would be to use if statements for simplicity:

playerOnePoints = playerOnePoints - 5
if playerOnePoints < 0:
    playerOnePoints = 0

or while less lines, may not read as simple:

# playerOnePoints = playerOnePoints - 5   # Delete this line and just use below
if playerOnePoints >= 5:
    playerOnePoints = playerOnePoints - 5

Upvotes: 1

PSM
PSM

Reputation: 439

After substracting the points from the player you can use max():

playerOnePoints = playerOnePoints - 5
playerOnePoints = max(0, playerOnePoints)

This will give you 0 if playerOnePoints is negative or playerOnePoints if its positive.

Also you could use abs() to do it.

def x(number):
    return (abs(number)+number)/2

x(-2) # This would return 0
x(2) # This would return 2

Upvotes: 1

Related Questions