家族で卓球のスコアを記録するプログラムを作っています。スコア制限に達すると、ゲームが終了し、誰が勝ったかが表示されます。これはあるプレーヤーでは機能しますが、他のプレーヤーでは機能しません。その理由はわかりません。P2 のスコアが 11 に達すると、P2 が勝利して再開すると表示されますが、P1 のスコアが 11 に達すると、そのまま続行されます。私はこれが初めてであることを覚えておいてください。コードは次のとおりです。
import sys
def defaults():
global p1sc
global p2sc
global p1adv
global p2adv
global p1name
global p2name
global sclimit
p1sc = 0
p2sc = 0
p1adv = 0
p2adv = 0
p1name = "Player 1"
p2name = "Player 2"
sclimit = 11
def checkwin():
global p1sc
global p2sc
global p1name
global p2name
global sclimit
if p2sc < sclimit:
program()
if p1sc < sclimit:
program()
if p2sc <= sclimit:
print(p2name + " wins!")
defaults()
print("New game!")
program()
if p1sc <= sclimit:
print(p1name + " wins!")
defaults()
print("New game!")
program()
elif p1sc == sclimit - 1 and p2sc == sclimit - 1:
print("Sudden Death!")
suddendeath()
else:
print("DEBUG: CHECKWIN FAILED")
def displayscore():
global p1name
global p2name
global p1sc
global p2sc
print(p1name + ": " + str(p1sc))
print(p2name + ": " + str(p2sc))
def program():
global p1sc
global p2sc
global p1name
global p2name
global sclimit
cmdinput = str(raw_input("Command: "))
if cmdinput == "1":
p1sc = p1sc + 1
displayscore()
checkwin()
elif cmdinput == "2":
p2sc = p2sc + 1
displayscore()
checkwin()
elif cmdinput == "q":
p1sc = p1sc - 1
displayscore()
checkwin()
elif cmdinput == "w":
p2sc = p2sc - 1
displayscore()
checkwin()
elif cmdinput == "name":
p1name = str(raw_input("Player 1 name: "))
print("Player 1 name has been set to " + p1name)
p2name = str(raw_input("Player 2 name: "))
print("Player 2 name has been set to " + p2name)
program()
elif cmdinput == "limit":
sclimit = raw_input("Score limit: ")
print("Score limit has been set to " + str(sclimit))
program()
elif cmdinput == "exit":
sys.exit()
else:
print("Command not recognised")
program()
def suddendeath():
global p1name
global p2name
global p1adv
global p2adv
cmdinput = str(raw_input("Command: "))
if cmdinput == "1":
if p2adv == 0:
p1adv = 1
elif p2adv == 1:
p2adv = 0
elif p1adv == 1:
print(p1name + " wins!")
defaults()
print("New game!")
program()
elif cmdinput == "2":
if p1adv == 0:
p2adv = 1
elif p1adv == 1:
p1adv = 0
elif p2adv == 1:
print(p2name + " wins!")
defaults()
print("New game!")
program()
defaults()
program()