我用python做了一个简单的纸牌游戏,玩家比较一张牌的属性,属性值高的牌赢得这一轮,输家给他们那张牌,把它从他们的牌组中拿出来,放到赢家牌组的最后。
以下是我的游戏代码,其中包括一轮游戏:
import random
class Ram():
def __init__(self, name, weight, milk, wool, offs, thigh, fert, meat, fat):
self.name = name
self.weight = weight
self.milk = milk
self.wool = wool
self.offs = offs
self.thigh = thigh
self.fert = fert
self.meat = meat
self.fat = fat
def __repr__(self):
return f"{self.name,self.weight,self.milk,self.wool,self.offs,self.thigh,self.fert,self.meat,self.fat}"
def Read(txtfile):
datalist=[]
f = open(txtfile, "r", encoding="utf-8")
for line in f:
datalist.append(line.split(','))
f.close()
cardlist = []
for i in datalist:
cardlist.append(Ram(i[0], i[1], i[2], i[3], i[4], i[5], i[6], i[7]))
return cardlist
def RandomChoice():
l=["User","Computer"]
return random.choice(l)
cardlist=Read("hrutaspil.txt")
random.shuffle(cardlist)
split = len(cardlist) // 2
user_deck = cardlist[:split]
computer_deck = cardlist[split:]
game=input("Do you want to play?(y/n)")
if game == "y":
if RandomChoice() == "User":
print("The user chooses first")
print("Your top card is:",user_deck[0].name)
print("1 = weight =",user_deck[0].weight)
print("2 = milk =", user_deck[0].milk)
print("3 = wool =", user_deck[0].wool)
print("4 = offs =", user_deck[0].offs)
print("5 = thigh =", user_deck[0].thigh)
print("6 = fert =", user_deck[0].fert)
print("7 = meat =", user_deck[0].meat)
print("8 = fat =", user_deck[0].fat)
choice=input("Choose stat: ")
if choice == ("1"):
print("Weight is:",float(user_deck[0].weight))
print("compared with computer:",float(computer_deck[0].weight))
if float(computer_deck[0].weight) > float(user_deck[0].weight):
print("Computer wins.")
else:
print("You win.")现在,如何将一张牌从输家牌中移除,并将其添加到赢家牌的底部?
发布于 2018-05-10 05:05:22
if float(computer_deck[0].weight) > float(user_deck[0].weight):
print("Computer wins.")
computer_deck.append(user_deck.pop(0))
else:
print("You win.")
user_deck.append(computer_deck.pop(0))你是否也意识到用户赢得了平局?根据你的问题,我不确定这是不是你想要的。
https://stackoverflow.com/questions/50261835
复制相似问题