【发布时间】:2017-07-20 16:51:11
【问题描述】:
CPU 和用户得到重复卡。我使用了shuffle 函数以及pop。有没有办法防止用户和CPU重复卡?
这是一个执行示例:
这是你的卡片:
1) 俱乐部 10
2) 钻石 4
3) 钻石 6
4) 俱乐部 7
5) 俱乐部 10要玩牌,只需一次输入一张牌即可。完成后, 输入空格
2 1 3 4
您玩过: 方块 4 梅花 10 方块 6 方块 7 俱乐部
CPU 播放:黑桃J 黑桃J
如您所见,用户两次获得了 10 of Clubs。
我的代码:
import random
import math
from collections import Counter
print("Gui-less poker sucks, but it sure is addicting probably")
if 1:
deck = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52]
random.shuffle(deck)
hcardss = [""]
hcardsc = [""]
hcardsh = [""]
ccardss = [""]
ccardsc = [""]
ccardsh = [""]
ingame = "true"
while (ingame == "true"):
undone = 5
while (undone > 0):
card = deck.pop()
# 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
random.shuffle(deck)
while (undone > 0):
# THIS ONE IS THE COMPUTER
card = deck.pop()
# 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")
instant = "true"
doneinput = "false"
hplay = [""]
while (doneinput == "false"):
latestinput = input("> ")
if (latestinput == ""):
if (instant == "true"):
print("Okay, you fold")
ingame = "false"
exit()
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(hplay)
#print(str(cplayh))
hscore = 0
cscore = 0
#checker = 1
#highnumber = 0
#quantity = [""]
#quancheck = 0
htrans = [""]
temp5 = 1
while (len(hplay) > temp5):
htrans.append(int(hcardsc[hplay]))
temp5 += 1
ctrans = [""]
temp5 = 1
while (len(cplay) > temp5):
ctrans.append(int(ccardsc[cplay]))
temp5 += 1
hoccur = Counter(htrans).most_common()
coccur = Counter(ctrans).most_common()
print(hoccur)
#while (len(hplay) > checker):
# if (hcardsc[hplay[checker]] > highnumber):
# quancheck += 1
# quantity.append(quancheck)
#checker += 1
ingame = "false"
【问题讨论】:
-
你可以创建一个
set(或者list,如果你真的想要),选择一个随机元素并将其删除。那么接下来你就不可能拿到同一张卡了。 -
我认为您应该将
deck的声明移到外部while 循环内。 -
另外,我很确定如果它得到两个数字,如
14和15,因为这些数字除以4和13返回相同的值。 -
对随机函数使用种子,时间戳将是一个很好的种子
-
ingame = "true".... 你为什么不使用布尔值,ingame=True?
标签: python random shuffle poker