我从 python 开始,我正在尝试编写一个纸牌游戏,用户和计算机可以玩 5 轮。用户和计算机必须从我创建的牌组中随机获得一张卡片,获胜者是卡片最高的那一张。
我的代码中有几个问题。
例如,当我创建整个牌组时,我会得到“bastos”卡片的输出:
“8 de bastos”、“9 de bastos”、“10 de bastos”和“11 de bastos”而不是“sota de bastos”、“caballo de bastos”、“rey de bastos”和“as de bastos” ”。
它只发生在“bastos”上,因为它是我在列表中的第一个变量。但我不知道如何解决这个问题。
然后我的结果也有问题:
Tu carta es caballo de oros > Your card is King of Gold
La carta del ordenador es sota de espadas > Your card is Jokey of Spades
Esta ronda la ha ganado el ordenador. > The computer has won this round.
所以我的程序说计算机不正常时赢了。
我也有类似的抽签问题。我的程序无法识别任何平局,例如:
Tu carta es 5 de espadas > Your card is 5 of spaces
La carta del ordenador es 5 de copas > Computer's card is 5 of coups.
Has ganado. > You have won.
这是我到目前为止的全部代码:
import random
print("Who will be the best out of 5 rounds?")
contador_humano = 0
contador_ordenador = 0
rondas = 0
while rondas < 5:
rondas = rondas + 1
print("\nRounds", rondas)
palos = ["bastos", "copas", "espadas", "oros"]
num = [2, 3, 4, 5, 6, 7, 8, 9, 10, 11]
baraja = []
for n in num:
for palo in palos:
carta = "{} de {}".format(n,palo)
for c in carta:
if n == 8:
n = sota
elif n == 9:
n = "caballo"
elif n == 10:
n = "rey"
elif n == 11:
n = "as"
baraja.append(carta)
random.shuffle(baraja)
input("\nPress enter to shuffle.")
print(baraja)
carta_humano = random.choice(baraja)
print("\nThis is your card", carta_humano)
baraja.remove(carta_humano)
carta_ordenador = random.choice(baraja)
print("This is the computer card", carta_ordenador)
if carta_humano > carta_ordenador:
contador_humano += 1
contador_ordenador += 0
print("\nYou have won this round.")
elif carta_humano == carta_ordenador:
contador_humano += 0
contador_ordenador += 0
print("\nThis is a draw.")
else:
contador_humano += 0
contador_ordenador += 1
print("\nThe computer has won this round.")
baraja.append(carta_humano)
random.shuffle(baraja)
print("\nFinal score:", contador_humano, " - ", contador_ordenador)
if contador_humano > contador_ordenador:
print("You win!")
elif contador_humano == contador_ordenador:
print("It is a draw.")
elif contador_humano < contador_ordenador:
print("You lose.")
答案 0 :(得分:2)
一些问题:
for c in carta
将迭代 carta
的每个 字符。这不是你想要的。n
在您为 carta
赋值后,不会更改 carta
。carta_humano > carta_ordenador
时,您是在比较这些字符串,因此例如“rey”将被视为大于“as”。您需要比较卡片的数值。我建议为卡片创建一个类,它就像一个具有等级和花色属性的元组,它有一个 __repr__
方法来负责生成“好”的名字。通过将其定义为元组,顺序基于第一个成员(排名),这正是我们所需要的。
我也不会洗牌和随机选择一张牌。如果你已经洗牌了,你可以拿走最后一张牌。这就像随机选择一个一样随机。你把选定的牌放回牌组,但在我看来这太过分了。有足够玩 5 轮的卡片,所以不要费心把它们放回去。但这只是我的观点。对于您的问题,这不是必需的。
最后的 elif
可以只是一个 else
,因为只剩下一种可能性。
这是它的工作原理:
import random
from collections import namedtuple
class Card(namedtuple('Card', ['rank', 'suit'])):
def __repr__(self):
return "{} of {}".format(self.rank if self.rank < 8 else ["sota", "caballo", "rey", "as"][self.rank-8], self.suit)
print("Who will be the best out of 5 rounds?")
contador_humano = 0
contador_ordenador = 0
baraja = [Card(rank, suit)
for suit in ["bastos", "copas", "espadas", "oros"]
for rank in [2, 3, 4, 5, 6, 7, 8, 9, 10, 11]]
random.shuffle(baraja)
for rondas in range(1, 6):
input("\nPress enter to start round {}".format(rondas))
carta_humano = baraja.pop()
print("\nThis is your card: {}".format(carta_humano))
carta_ordenador = baraja.pop()
print("This is the computer card: {}".format(carta_ordenador))
if carta_humano > carta_ordenador:
contador_humano += 1
print("\nYou have won this round.")
elif carta_humano == carta_ordenador:
print("\nThis is a draw.")
else:
contador_ordenador += 1
print("\nThe computer has won this round.")
print("\nFinal score:", contador_humano, " - ", contador_ordenador)
if contador_humano > contador_ordenador:
print("You win!")
elif contador_humano == contador_ordenador:
print("It is a draw.")
else:
print("You lose.")
答案 1 :(得分:0)
人类知道在纸牌游戏中检查 '7 of copa' > '2 of oros' 意味着什么。然而,计算机并不理解卡片,它只知道你告诉它每张卡片都是一个字符串,它会根据字符串的内容评估表达式以确定谁获胜。
要解决比较每张卡片的整个字符串的问题,“{n} de {palo}”,您只需要获取每张卡片的 num 部分(我假设这就是我们'正在检查,如果我错了,请纠正我),您可以通过将字符串拆分为单词(其中单词是由空格分隔的字符串部分)并将字符串中的第一个单词(因为我们知道是数字)转换成一个变量,然后用于表达式以确定谁赢了。
我取出了您现在将 8-11 转换为 sota-as 的代码块。当我让它工作时,我会在这里编辑如何解决这个问题。现在,这是你的代码减去那个块,我添加了两行代码并编辑了另外两行,现在它给出了预期的赢家。
import random
print("Who will be the best out of 5 rounds?")
contador_humano = 0
contador_ordenador = 0
rondas = 0
while rondas < 5:
rondas = rondas + 1
print("\nRounds", rondas)
palos = ["bastos", "copas", "espadas", "oros"]
num = [2, 3, 4, 5, 6, 7, 8, 9, 10, 11]
baraja = []
for n in num:
for palo in palos:
carta = "{} de {}".format(n,palo)
baraja.append(carta)
random.shuffle(baraja)
input("\nPress enter to shuffle.")
print(baraja)
carta_humano = random.choice(baraja)
print("\nThis is your card", carta_humano)
baraja.remove(carta_humano)
carta_ordenador = random.choice(baraja)
print("This is the computer card", carta_ordenador)
# get the number portion of carta_humano
num_carta_humano = int(carta_humano.split()[0])
# get the number portion of carta_ordenador
num_carta_ordenador = int(carta_ordenador.split()[0])
if num_carta_humano > num_carta_ordenador: # compare numeric portion of each card
contador_humano += 1
contador_ordenador += 0
print("\nYou have won this round.")
elif num_carta_humano == num_carta_ordenador: # compare numeric portion of each card
contador_humano += 0
contador_ordenador += 0
print("\nThis is a draw.")
else:
contador_humano += 0
contador_ordenador += 1
print("\nThe computer has won this round.")
baraja.append(carta_humano)
random.shuffle(baraja)
print("\nFinal score:", contador_humano, " - ", contador_ordenador)
if contador_humano > contador_ordenador:
print("You win!")
elif contador_humano == contador_ordenador:
print("It is a draw.")
elif contador_humano < contador_ordenador:
print("You lose.")
编辑:
您的问题的另一半是您用 sota-as 替换 8-11 的代码无法按预期运行。
我修复了您的代码,使其按您预期的方式运行。由于您需要将 8-11 替换为 sota-as 的唯一原因是打印卡片时,我更改了代码,以便卡片本身不会更改,但打印的是什么。我在导入的顶部添加了一个函数。当您打印一副套牌和卡片时,该功能用于将 8-11 替换为 sota-as,但它不会更改原始的套牌或卡片,只会更改打印的内容。
这是包含所有更改的完整更正代码:
import random
def replace_num_carta(baraja, carta):
# return the text representation of a card, replacing 8-11 with sota-as
if int(carta.split()[0]) == 8: # if the first word in the split string is 8
return ('sota de ' + carta.split()[-1]) # print 'sota de' concatenated to the last word of the split string
elif int(carta.split()[0]) == 9: # if the first word in the split string is 9
return ('caballo de ' + carta.split()[-1]) # print 'caballo de' concatenated to the last word of the split string
elif int(carta.split()[0]) == 10: # if the first word in the split string is 10
return ('rey de ' + carta.split()[-1]) # print 'rey de' concatenated to the last word of the split string
elif int(carta.split()[0]) == 11: # if the first word in the split string is 11
return ('as de ' + carta.split()[-1]) # print 'as de' concatenated to the last word of the split string
else:
return(carta)
print("Who will be the best out of 5 rounds?")
contador_humano = 0
contador_ordenador = 0
rondas = 0
while rondas < 5:
rondas = rondas + 1
print("\nRounds", rondas)
palos = ["bastos", "copas", "espadas", "oros"]
num = [2, 3, 4, 5, 6, 7, 8, 9, 10, 11]
baraja = []
for n in num:
for palo in palos:
carta = "{} de {}".format(n,palo)
baraja.append(carta)
random.shuffle(baraja)
input("\nPress enter to shuffle.")
baraja_to_print = []
for carta in baraja:
baraja_to_print.append(replace_num_carta(baraja, carta)) # append the current card, after running through the function replace_num_carta
print(baraja_to_print)
carta_humano = random.choice(baraja)
print("\nThis is your card " + replace_num_carta(baraja, carta_humano))
baraja.remove(carta_humano)
carta_ordenador = random.choice(baraja)
print("This is the computer card " + replace_num_carta(baraja, carta_ordenador))
# get the number portion of carta_humano
num_carta_humano = int(carta_humano.split()[0])
# get the number portion of carta_ordenador
num_carta_ordenador = int(carta_ordenador.split()[0])
if num_carta_humano > num_carta_ordenador: # compare numeric portion of each card
contador_humano += 1
contador_ordenador += 0
print("\nYou have won this round.")
elif num_carta_humano == num_carta_ordenador: # compare numeric portion of each card
contador_humano += 0
contador_ordenador += 0
print("\nThis is a draw.")
else:
contador_humano += 0
contador_ordenador += 1
print("\nThe computer has won this round.")
baraja.append(carta_humano)
random.shuffle(baraja)
print("\nFinal score:", contador_humano, " - ", contador_ordenador)
if contador_humano > contador_ordenador:
print("You win!")
elif contador_humano == contador_ordenador:
print("It is a draw.")
elif contador_humano < contador_ordenador:
print("You lose.")