Python纸牌游戏错误
好吧,脚本运行了,但我没有收到任何错误,而且它无法正常工作。更具体地说,该程序应该发牌,然后制作一个字典,根据等级和花色为每张牌赋予一个值。牌被洗牌、发牌,然后排序。谁拥有3C,谁先开始。计算机如何挑选牌是它遍历其牌列表,如果它发现一张牌的价值高于前一张牌,它就会播放它。好吧,有时会起作用,但如果我打出一张牌(通常是 T(10) 或更高),它就不起作用,计算机会打出一张价值较低的牌。感谢您的阅读:D
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()
Well the script runs but I'm not getting any error's and it's not working right. More specificly, the program is supposed to deal out cards, then make a dictionary that gives each card a value depending on rank and suit. The cards are shuffled, dealt and then sorted. Who ever has the 3C starts first. How the computer is supossed to pick card is it goes through its list of cards and if It finds one that has a higher value then the previous card, it plays it. Well that works sometimes but If I play a card (usually T(10) or higher) it doesn't work, the computer plays a card that is worth less. Thanks for reading :D
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()
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
为了确定计算机是否应该打牌,您可以使用
“3C”和“TD”等简单的字符串。所以当你使用“>”时比较,它将它们作为字符串进行比较。但作为字符串 'TD' > “QD”等。您可能应该编写一个小函数来确定卡的价值并使用其结果。
PS:你能看到下面还有其他问题吗?
To determine whether the computer should play a card, you use
but your cards are simply strings like "3C" and "TD". So when you use the ">" comparison, it compares them as strings. But as a string 'TD' > 'QD', etc. You should probably write a small function to determine the value of a card and use the results of that.
PS: Can you see other problems below?