CPU和用户正在重复使用卡片。我已经使用了shuffle功能,以及pop。有没有办法防止用户和CPU重复卡。
这是编译程序的一个例子
这是你的卡片: 1)10个俱乐部 2)钻石4 3)钻石6 4)俱乐部的7个 5)10个俱乐部 要打牌,只需一次输入第一张牌。完成后,输入空白 2 1 3 4
你玩过: 钻石4 10个俱乐部 钻石6 俱乐部的7个
CPU播放: 黑桃杰克 黑桃杰克
正如您所看到的,用户被提示重复“随机”卡片并且CPU播放重复的“随机”卡片。
import random
import math
print("Gui-less poker sucks, but it sure is addicting probably")
if 1:
hcardss = [""]
hcardsc = [""]
hcardsh = [""]
ccardss = [""]
ccardsc = [""]
ccardsh = [""]
ingame = "true"
while (ingame == "true"):
undone = 5
while (undone > 0):
card = random.randint(1,52)
# print(card)
temp = card / 13
temp2 = card / 4
temp = math.floor(temp)
temp2 = math.floor(temp2)
temp = temp + 1
# temp2 = temp2 + 1
#print(temp)
#print(temp2)
# undone -= 1
hcardss.append(temp)
hcardsc.append(temp2)
if (temp == 1):
temp3 = " of Spades"
elif (temp == 2):
temp3 = " of Diamonds"
elif (temp == 3):
temp3 = " of Clubs"
else:
temp3 = " of Hearts"
if (temp2 == 10):
temp4 = "Jack"
elif (temp2 == 11):
temp4 = "Queen"
elif (temp2 == 12):
temp4 = "King"
elif (temp2 == 13):
temp4 = "Ace"
else:
temp4 = str(temp2 + 1)
# print("Your card was the " + temp4 + temp3)
hcardsh.append("The " + temp4 + temp3)
undone -= 1
undone = 5
while (undone > 0):
# THIS ONE IS THE COMPUTER
card = random.randint(1,52)
# print(card)
temp = card / 13
temp2 = card / 4
temp = math.floor(temp)
temp2 = math.floor(temp2)
temp = temp + 1
# temp2 = temp2 + 1
#print(temp)
#print(temp2)
# undone -= 1
ccardss.append(temp)
ccardsc.append(temp2)
if (temp == 1):
temp3 = " of Spades"
elif (temp == 2):
temp3 = " of Diamonds"
elif (temp == 3):
temp3 = " of Clubs"
else:
temp3 = " of Hearts"
if (temp2 == 10):
temp4 = "Jack"
elif (temp2 == 11):
temp4 = "Queen"
elif (temp2 == 12):
temp4 = "King"
elif (temp2 == 13):
temp4 = "Ace"
temp4 = str(temp2 + 1)
# print("Your card was the " + temp4 + temp3)
ccardsh.append("The " + temp4 + temp3)
undone -= 1
print()
print()
print()
print("Here are your cards:")
print("1) " + hcardsh[1])
print("2) " + hcardsh[2])
print("3) " + hcardsh[3])
print("4) " + hcardsh[4])
print("5) " + hcardsh[5])
print("To play cards, simply type their number one at a time. When done, input blank")
doneinput = "false"
hplay = [""]
while (doneinput == "false"):
latestinput = input("> ")
if (latestinput == ""):
doneinput = "true"
else:
if (int(latestinput) in hplay):
print("You already picked that one!")
else:
hplay.append(int(latestinput))
# print("The cards you played are " + str(hplay))
doneinput = "false"
cplay = [""]
while (doneinput == "false"):
latestinput = random.randint(1,5)
if (latestinput == ""):
doneinput = "true"
else:
if (int(latestinput) in cplay):
doneinput = "true"
else:
cplay.append(int(latestinput))
#print("So you played " + str(hplay))
#print("And the cpu played " + str(cplay))
#print("So you played the " + hcardsh[hplay[1]] + hcardsh[hplay[2]]
times = len(hplay)
# times = times - 1
hplayh = [""]
cplayh = [""]
sub = 1
print()
print()
print("You played:")
while (sub < times):
hplayh.append(hcardsh[hplay[sub]])
print(hcardsh[hplay[sub]])
sub += 1
sub = 1
times = len(cplay)
print()
print()
print("CPU played:")
while (sub < times):
cplayh.append(ccardsh[cplay[sub]])
print(ccardsh[cplay[sub]])
sub += 1
#print(str(hplayh))
#print(str(cplayh))
ingame = "false"
答案 0 :(得分:2)
根本不要使用randint()来挑选卡牌。构建一个&#34; deck&#34;,它只是一个卡片列表,使用shuffle()随机化它,pop()来处理一张卡片。此外,我建议使用数字而不是字符串来表示卡片。字符串适用于人类。数字将使代码的其余部分更简单,更快捷。只需在需要时翻译为用户的字符串。
类似的东西:
theDeck = [];
def shuffle():
theDeck = range(52)
random.shuffle(theDeck)
def dealCard():
return theDeck.pop()
def nameOfCard(c):
return [ "Deuce", "Trey", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Jack", "Queen", "King" ][c >> 2] + \
" of " + [ "Clubs", "Diamonds", "Hearts", "Spades" ][c & 3];
答案 1 :(得分:0)
将已播放的卡片添加到列表中。检查此列表中是否已有新的随机卡,如果是,则重新生成新卡,直到它不在列表中。不要忘记检查清单上有所有卡,以避免无限循环。 附:不要使用这种方法,一次生成所有列表并使其更有效。
答案 2 :(得分:0)
您必须保留所有正在播放的牌的列表,然后检查最近生成的牌是否在该列表中,如果是,则重新生成。
generated_cards = []
...
card = random.randint(1,52)
while card in generated_cards:
card = random.randint(1,52)
generated_cards.append(card) # once the card is a new, unique one,
# add it to the generated_cards list
一种更简单的处理方法可能是生成卡片列表,随机播放它们,然后逐个将它们从列表中弹出。
deck_of_cards = [x for x in range(0, 52)]
random.shuffle(deck_of_cards)
...
card = deck_of_cards.pop() # this removes the item from the list
# and puts it in the card variable