关于python:两个玩家骰子游戏的得分问题

scoring problem with Two player dice game

这是一个游戏,两个用户掷2个骰子5次。如果骰子的总数是偶数,则玩家获得10分;如果是奇数,则输掉5分。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
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")

有问题的是,如果一个用户滚动1和2,加起来是3,这是一个奇数,游戏将-5从3,这使总数-2,但我不希望它进入负,我希望它这样,如果他们确实得到mius分,它说他们得到0分而不是负分。


为了简单起见,PSM的另一个答案是使用if语句:

1
2
3
playerOnePoints = playerOnePoints - 5
if playerOnePoints < 0:
    playerOnePoints = 0

或者,虽然行数较少,但读起来可能并不简单:

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

减去玩家的点数后,可以使用max():

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

如果playerepoints为阴性,则该值为0;如果playerepoints为阳性,则该值为0。

也可以使用abs()来完成。

1
2
3
4
5
def x(number):
    return (abs(number)+number)/2

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