那麼腳本運行,但我沒有得到任何錯誤,它不能正常工作。更具體地說,該程序應該髮卡,然後製作一個字典,根據等級和花色給每張卡片一個值。牌洗牌,處理,然後排序。誰曾經從3C開始。計算機如何選擇卡片是通過它的卡片列表,如果它找到一個具有較高值的卡片,則它將播放它。那麼有時候有效,但如果我玩一張卡(通常是T(10)或更高),它不起作用,電腦會播放一張價值較低的卡。感謝您的閱讀:dPython卡片遊戲錯誤
import random
class player():
def __init__(self, hand, name):
self.hand = hand
self.passed = False
self.name = name
#computer ai for playing
def complay(player):
if '3C' in player.hand:
playCard(player, '3C')
return
else:
print previous[-1]
for i in player.hand:
if i > previous[-1]:
playCard(player, i)
return
else:
print 'Nothing higher', i, key[i]
#human options for playing a card
def humplay(player):
if preplayer[-1] == 'player1':
print 'hi'
return
else:
done = False
low = 0
while done == False:
print player.hand
if low > 1:
choose = raw_input('That card is to low, what card do you want to play?\n')
else:
choose = raw_input('What card do you want to play?\n')
if choose not in player.hand:
print 'not in'
continue
if key[choose] > key[previous[-1]]:
playCard(player, choose)
done = True
else:
low == 1
#Function for playing a card
def playCard(player, card):
place = player.hand.index(card)
playcard = player.hand.pop(place)
print player.name, 'has played the', playcard
previous.append(playcard)
preplayer.append(player.name)
#sorts the cards in hand
def sort(player):
for n in range(len(player.hand)):
for i in player.hand:
place = player.hand.index(i)
try:
if key[i] > key[player.hand[place + 1]]:
player.hand.insert(place + 1, player.hand.pop(place))
except IndexError:
pass
def main():
global previous
global preplayer
global key
suits = 'CSHD'
mark = '3456789TJQKA2'
deck = []
players = []
previous = ['3l']
preplayer = []
for card in mark:
for suit in suits:
deck.append(card+suit)
key = {}
for i in range(len(deck)):
key[deck[i]] = i
key['3l'] = -1
for i in deck:
print i, '\t\t', key[i]
random.shuffle(deck)
player1 = player(deck[0::2], 'player1')
player2 = player(deck[1::2], 'player2')
player3 = player(deck[2::4], 'player2')
player4 = player(deck[3::4], 'player3')
players = [player1, player2, player3, player4]
sort(player1)
sort(player2)
sort(player3)
sort(player4)
if '3C' in player1.hand:
preplayer.append(player2.name)
if '3C' in player2.hand:
preplayer.append(player1.name)
while len((player1.hand or player2.hand or player3.hand or player4.hand)) != 0:
if preplayer[-1] == 'player1':
complay(player2)
if preplayer[-1] == 'player2':
humplay(player1)
main()
縮進代碼,你可以選擇區域和打大括號鍵「{}」(或控制類型-K)。 – DSM 2012-01-28 21:39:45
您可以隨時在文本編輯器和標籤所做的一切輸入您的密碼,給它是正確的格式。 – Makoto 2012-01-28 21:40:46
它必須是一個文本編輯器,它可以將製表符轉換爲空格,例如emacs – 2012-01-28 22:08:56