我有卡片遊戲的戰爭,我有一個問題,它讀取每個卡片的數值是正確的,除了10.除了10之外,它會輸給遊戲中的所有東西,但是遊戲會選擇一個贏取,而我不會不知道爲什麼。我必須在下面編寫代碼,並且引用它一次,但也許它在代碼中的其他位置。戰爭牌遊戲值錯誤。 Python
import random, csv
global wars
global counter
wars = 0
counter= 0
def buildDeck():#builds deck
suits=[]
ranks=[]
ranks = [str(x) for x in range(2, 11)] + ['J', 'Q', 'K', 'A']
suits = ['D', 'H', 'S', 'C']
deck = [rank + ' of ' + suit for rank in ranks for suit in suits]
random.shuffle(deck)
deck = [rank + ' of ' + suit for rank in ranks for suit in suits]
random.shuffle(deck)
return deck
def values(card):#gives cards value
carddict={'2':2, '3':3, '4':4, '5':5, '6':6, '7':7, '8':8, '9':9, '10':10, 'J':11, 'Q':12, 'K':13, 'A':14}
return (card)
def Choice(pile):
if pile[-1] == "":
return random.choice(pile[-2])
if pile[-2] == "":
return random.choice(pile[-3])
if pile[-3] == "":
return random.choice(pile[-4])
else:
return pile[-1]
def mixdecks(deck1, pileOne, pileTwo):# takes pile mixes them together and adds to bottom of winning deck
pileOne.extend(pileTwo)
random.shuffle(pileOne)
deck1.extend(pileOne)
def playGame():
deck = buildDeck()#makes deck
deckOne = [deck[2 * i + 1] for i in range(26)]#deals deck
deckTwo = [deck[2 * i] for i in range(26)]
while len(deckOne) != 0 or len(deckTwo) != 0:#ends when deck runs out
global wars
global counter
if len(deckOne)==0:#wins
print('Payer two wins.')
print('player one had', len(deckOne), 'player two had', len(deckTwo))
counter +=1
break
if len(deckTwo)==0:
print('Payer one wins.')
print('player one had', len(deckOne), 'player two had', len(deckTwo))
counter +=1
break
pileOne = [deckOne.pop(0)]#plays cards
pileTwo = [deckTwo.pop(0)]
print('Player one', len(deckOne), 'used', pileOne[0], 'Player two', len(deckTwo), 'used', pileTwo[0])
if values(pileOne[0]) > values(pileTwo[0]):#player one win condition
print("Player one wins with", pileOne[0])
mixdecks(deckOne, pileOne, pileTwo)
elif values(pileTwo[0]) > values(pileOne[0]):#player two win condition
print("Player two wins with", pileTwo[0])
mixdecks(deckTwo, pileOne, pileTwo)
elif values(pileTwo[0]) == values(pileOne[0]):
#war start
war = True
print('I Declare War')
while war==True:
for i in range(3):
if deckOne:
pileOne.append(deckOne.pop(0))
elif pileOne[-1]:
pileOne.append('')
if deckTwo:
pileTwo.append(deckTwo.pop(0))
elif pileTwo[-1]:
pileTwo.append('')
print('Player one', len(deckOne), 'used', pileOne,
'Player two', len(deckTwo), 'used', pileTwo)
if values(Choice(pileOne)) > values(Choice(pileTwo)):#player one win condition
print("Player one", len(deckOne), "wins the war with", pileOne)
mixdecks(deckOne, pileOne, pileTwo)
war = False
elif values(Choice(pileTwo)) > values(Choice(pileOne)):#player two win condition
print("Player two", len(deckTwo), "wins the war with", pileTwo)
mixdecks(deckTwo, pileOne, pileTwo)
war = False
wars += 1
x=str(input('Enter any Key To Run ("n" is to quit): ')) # start loop
if x != "n": #set value to true
play = True
while play == True:
playGame()
again=input('Enter any Key To Run again ("n" is quit): ') #asks to go again
if again == "n":
print("Thank you for playing.")
print(" You played", counter, "rounds.")
print(" You had", wars, "wars.")
play = False
x = "n"
else: # terminates inital go
print ("Thank you anyway.")
print (counter,wars)
歡迎來到堆棧溢出,請閱讀http://stackoverflow.com/help/how-to-ask並修改您的問題。簡單地傾銷您的代碼並詢問「爲什麼這不起作用?」不會得到任何迴應。你需要解釋你所嘗試過的,你有什麼錯誤信息等 – Mikkel