簡體   English   中英

兩人骰子游戲的得分問題

[英]scoring problem with Two player dice game

這是兩個用戶擲 2 個骰子 5 次的游戲。 如果骰子總數為偶數,則玩家獲得 10 分; 如果是奇數,則輸 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")

這樣做的錯誤在於,假設用戶擲出 1 和 2 加起來為 3,這是一個奇數,游戲將從 3 中得到 -5,這使得總數為 -2,但我不希望它進入減去,我想要這樣,如果他們確實得到了 mius 積分,它會說他們得到 0 分而不是減分

從玩家中減去點數后,您可以使用 max():

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

如果 playerOnePoints 為負,這將為您提供 0,如果為正,則為 playerOnePoints。

你也可以使用 abs() 來做到這一點。

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

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

為簡單起見,PSM 答案的替代方法是使用if語句:

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

或者雖然行數較少,但讀起來可能不那么簡單:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM