以下是我正在处理的问题:编写一个函数war(),模拟一场战争游戏,并返回一个包含战斗,战争和两轮战争的元组。
对我来说,写这个功能最困难的部分是连续战争。这是我搞砸的地方。
这是我的代码:
import random
def shuffleDeck():
suits = {'\u2660', '\u2661', '\u2662', '\u2663'}
ranks = {'2', '3', '4', '5', '6', '7', '8' '9', '10', 'J', 'Q', 'K', 'A'}
deck = []
for suit in suits:
for rank in ranks:
deck.append(rank + ' ' + suit)
random.shuffle(deck)
return deck
def splitDeck(deck):
splitdeck = []
for i in range(0, len(deck)//2):
splitdeck.append(deck[i])
deck.remove(deck[i])
return splitdeck
def dealCard(deck):
if deck != []:
card = deck[0]
deck.remove(card)
return card
def battle(card1, deck1, card2, deck2):
values = {'A':1, '2':2, '3':3, '4':4, '5':5, '6':6, '7':7, '8':8, '9':9, '1':10,
'J':11, 'Q':12, 'K':13}
if values[card1[0]] > values[card2[0]]:
deck1.append(card1)
deck1.append(card2)
elif values[card1[0]] < values[card2[0]]:
deck2.append(card2)
deck2.append(card1)
def tiebreaker(card1, deck1, card2, deck2):
values = {'A':1, '2':2, '3':3, '4':4, '5':5, '6':6, '7':7, '8':8, '9':9, '1':10,
'J':11, 'Q':12, 'K':13}
FDdeck1 = [card1]
FDdeck2 = [card2
while len(deck1) > 2 and len(deck2) > 2:
for i in range(0, 3):
FDdeck1.append(deck1[i])
FDdeck2.append(deck2[i])
for i in range(-3, 0):
deck1.remove(FDdeck1[i])
deck2.remove(FDdeck2[i])
i = random.randrange(len(FDdeck1)-3, len(FDdeck1))
j = random.randrange(len(FDdeck2)-3, len(FDdeck2))
newcard1 = FDdeck1[i]
newcard2 = FDdeck2[j]
if values[newcard1[0]] > values[newcard2[0]]:
for card in FDdeck2:
deck1.append(card)
break
elif values[newcard1[0]] < values[newcard2[0]]:
for card in FDdeck1:
deck2.append(card)
break
elif values[newcard1[0]] == values[newcard2[0]]:
continue
if len(deck1) < 2:
for card in deck1:
FDdeck1.append(card)
deck1 = []
for i in range(0, 3):
FDdeck2.append(deck2[i])
for i in range(-3, 0):
deck2.remove(FDdeck2[i])
i = random.randrange(len(FDdeck2)-3, len(FDdeck2))
newcard1 = FDdeck1[len(deck1)]
newcard2 = FDdeck2[i]
if values[newcard1[0]] > values[newcard2[0]]:
for card in FDdeck2:
deck1.append(card)
elif values[newcard1[0]] < values[newcard2[0]]:
for card in FDdeck1:
deck2.append(card)
while values[newcard1[0]] == values[newcard2[0]]:
for i in range(0, 3):
FDdeck2.append(deck2[i])
for i in range(-3, 0):
deck2.remove(FDdeck2[i])
j = random.randrange(len(FDdeck2)-3, len(FDdeck2))
newCard2 = FDdeck2[j]
if values[newcard1[0]] > values[newcard2[0]]:
for card in FDdeck2:
deck1.append(card)
break
elif values[newcard1[0]] < values[newcard2[0]]:
for card in FDdeck1:
deck2.append(card)
break
elif values[card1[0]] == values[card2[0]]:
continue
if len(deck2) < 2:
for card in deck2:
FDdeck2.append(card)
deck2 = []
for i in range(0, 3):
FDdeck1.append(deck1[i])
for i in range(-3, 0):
deck1.remove(FDdeck1[i])
i = random.randrange(len(FDdeck2)-3, len(FDdeck2))
newcard1 = FDdeck1[i]
newcard2 = FDdeck2[len(deck2)]
if values[newcard1[0]] > values[newcard2[0]]:
for card in FDdeck2:
deck1.append(card)
elif values[newcard1[0]] < values[newcard2[0]]:
for card in FDdeck1:
deck2.append(card)
while values[newcard1[0]] == values[newcard2[0]]:
for i in range(0, 3):
FDdeck1.append(deck1[i]) # I get an error index out of range
for i in range(-3, 0):
deck1.remove(FDdeck1[i])
j = random.randrange(len(FDdeck2)-3, len(FDdeck2))
newCard2 = FDdeck2[j]
if values[newcard1[0]] > values[newcard2[0]]:
for card in FDdeck2:
deck1.append(card)
break
elif values[newcard1[0]] < values[newcard2[0]]:
for card in FDdeck1:
deck2.append(card)
break
def war():
values = {'A':1, '2':2, '3':3, '4':4, '5':5, '6':6, '7':7, '8':8, '9':9, '1':10,
'J':11, 'Q':12, 'K':13}
deck = shuffleDeck()
countbattle = 0
counttiebreaker = 0
playerA = splitDeck(deck)
playerB = deck
while playerA != [] and playerB != []:
cardA = dealCard(playerA)
cardB = dealCard(playerB)
if values[cardA[0]] != values[cardB[0]]:
countbattle += 1
battle(cardA, playerA, cardB, playerB)
elif values[cardA[0]] == values[cardB[0]]:
counttiebreaker += 1
tiebreaker(cardA, playerA, cardB, playerB)
if playerA != []:
print('PlayerA wins')
elif playerB != []:
print('PlayerB wins')
return (countbattle, counttiebreaker)
我注意到我收到错误的行。它是功能决胜局。我不明白为什么我得到错误列表索引超出范围,因为deck1应该至少有50张牌,因为deck2的长度等于或小于2张牌。
我对该功能的另一个问题是,当我运行它时,我有时什么也得不到。我跑完后得到一个空白区域。例如,
战()
此外,这两个挫折在代码中还有其他问题。我查了一下,一切似乎都很好,但可能有一些我忽略的部分。谢谢。
答案 0 :(得分:-1)
我的评论还有很多错误:
import random
def shuffleDeck():
suits = {'\u2660', '\u2661', '\u2662', '\u2663'}
ranks = {'2', '3', '4', '5', '6', '7', '8' '9', '10', 'J', 'Q', 'K', 'A'}
deck = []
for suit in suits:
for rank in ranks:
deck.append(rank + ' ' + suit)
random.shuffle(deck)
return deck
def splitDeck(deck):
splitdeck = []
for i in range(0, len(deck)//2):
splitdeck.append(deck[i])
deck.remove(deck[i])
return splitdeck
def dealCard(deck):
if deck != []:
card = deck[0]
deck.remove(card)
return card
def battle(card1, deck1, card2, deck2):
values = {'A':1, '2':2, '3':3, '4':4, '5':5, '6':6, '7':7, '8':8, '9':9, '1':10,
'J':11, 'Q':12, 'K':13}
if values[card1[0]] > values[card2[0]]:
deck1.append(card1)
deck1.append(card2)
elif values[card1[0]] < values[card2[0]]:
deck2.append(card2)
deck2.append(card1)
def tiebreaker(card1, deck1, card2, deck2):
values = {'A':1, '2':2, '3':3, '4':4, '5':5, '6':6, '7':7, '8':8, '9':9, '1':10,
'J':11, 'Q':12, 'K':13}
FDdeck1 = [card1]
FDdeck2 = [card2] # here you missed the second square bracket
while len(deck1) > 2 and len(deck2) > 2:
for i in range(0, 3):
FDdeck1.append(deck1[i])
FDdeck2.append(deck2[i])
for i in range(-3, 0):
deck1.remove(FDdeck1[i])
deck2.remove(FDdeck2[i])
i = random.randrange(len(FDdeck1)-3, len(FDdeck1))
j = random.randrange(len(FDdeck2)-3, len(FDdeck2))
newcard1 = FDdeck1[i]
newcard2 = FDdeck2[j]
if values[newcard1[0]] > values[newcard2[0]]:
for card in FDdeck2:
deck1.append(card) # this line
break # and this line should be indented further
elif values[newcard1[0]] < values[newcard2[0]]:
for card in FDdeck1:
deck2.append(card)
break
elif values[newcard1[0]] == values[newcard2[0]]:
continue
if len(deck1) < 2:
for card in deck1:
FDdeck1.append(card)
deck1 = []
for i in range(0, 3):
FDdeck2.append(deck2[i])
for i in range(-3, 0):
deck2.remove(FDdeck2[i])
i = random.randrange(len(FDdeck2)-3, len(FDdeck2))
newcard1 = FDdeck1[len(deck1)]
newcard2 = FDdeck2[i]
if values[newcard1[0]] > values[newcard2[0]]:
for card in FDdeck2:
deck1.append(card)
elif values[newcard1[0]] < values[newcard2[0]]:
for card in FDdeck1:
deck2.append(card)
while values[newcard1[0]] == values[newcard2[0]]:
for i in range(0, 3):
FDdeck2.append(deck2[i])
for i in range(-3, 0):
deck2.remove(FDdeck2[i])
j = random.randrange(len(FDdeck2)-3, len(FDdeck2))
newCard2 = FDdeck2[j]
if values[newcard1[0]] > values[newcard2[0]]:
for card in FDdeck2:
deck1.append(card) # this line
break # and this line should be indented further
elif values[newcard1[0]] < values[newcard2[0]]:
for card in FDdeck1:
deck2.append(card)
break
elif values[card1[0]] == values[card2[0]]:
continue
if len(deck2) < 2:
for card in deck2:
FDdeck2.append(card)
deck2 = []
for i in range(0, 3):
FDdeck1.append(deck1[i])
for i in range(-3, 0):
deck1.remove(FDdeck1[i])
i = random.randrange(len(FDdeck2)-3, len(FDdeck2))
newcard1 = FDdeck1[i]
newcard2 = FDdeck2[len(deck2)]
if values[newcard1[0]] > values[newcard2[0]]:
for card in FDdeck2:
deck1.append(card)
elif values[newcard1[0]] < values[newcard2[0]]:
for card in FDdeck1:
deck2.append(card)
while values[newcard1[0]] == values[newcard2[0]]:
for i in range(0, 3):
FDdeck1.append(deck1[i]) # I get an error index out of range
for i in range(-3, 0):
deck1.remove(FDdeck1[i])
j = random.randrange(len(FDdeck2)-3, len(FDdeck2))
newCard2 = FDdeck2[j]
if values[newcard1[0]] > values[newcard2[0]]:
for card in FDdeck2:
deck1.append(card)
break
elif values[newcard1[0]] < values[newcard2[0]]:
for card in FDdeck1:
deck2.append(card)
break
def war():
values = {'A':1, '2':2, '3':3, '4':4, '5':5, '6':6, '7':7, '8':8, '9':9, '1':10,
'J':11, 'Q':12, 'K':13}
deck = shuffleDeck()
countbattle = 0
counttiebreaker = 0
playerA = splitDeck(deck)
playerB = deck
while playerA != [] and playerB != []:
cardA = dealCard(playerA)
cardB = dealCard(playerB)
if values[cardA[0]] != values[cardB[0]]:
countbattle += 1
battle(cardA, playerA, cardB, playerB)
elif values[cardA[0]] == values[cardB[0]]:
counttiebreaker += 1
tiebreaker(cardA, playerA, cardB, playerB)
if playerA != []:
print('PlayerA wins')
elif playerB != []:
print('PlayerB wins')
return (countbattle, counttiebreaker)
最后必须有一些你没有测试的条件允许程序在战斗和交易卡之间不断进入一个巨大的循环。