【发布时间】:2021-01-03 07:05:49
【问题描述】:
这是一场战争游戏。 我试图添加它应该询问用户是否想再次播放的代码。 它再次开始游戏,但从不重置玩家的牌,因此每次再次播放时他们拥有超过 26 张牌。 我不明白每次玩家想再次玩时我应该添加什么来重置卡片
import random
values = {"Two":2, "Three":3, "Four":4, "Five":5, "Six":6,
"Seven":7, "Eight":8, "Nine":9, "Ten":10, "Jack":11,
"Queen":12, "King":13, "Ace":14}
suits = ("Hearts", "Diamonds", "Clubs", "Spades")
ranks = ("Two", "Three", "Four", "Five", "Six",
"Seven", "Eight", "Nine", "Ten", "Jack",
"Queen", "King", "Ace")
class Card:
def __init__(self,suit,rank):
self.suit = suit
self.rank = rank
self.value = values[rank]
def __str__(self):
return self.rank + " of " + self.suit
class Deck:
def __init__(self):
self.all_cards = []
for suit in suits:
for rank in ranks:
created_card = Card(suit,rank)
self.all_cards.append(created_card)
def shuffle(self):
random.shuffle(self.all_cards)
def deal_one(self):
return self.all_cards.pop()
class Player:
def __init__(self, name):
self.name = name
self.all_cards = []
def remove_one(self):
return self.all_cards.pop(0)
def add_cards(self, new_cards):
if type(new_cards) == type([]):
self.all_cards.extend(new_cards)
else:
self.all_cards.append(new_cards)
def __str__(self):
return f"{self.name}"
#*****SCRIPT*****#
player_one = Player(input("Player One: Enter Name "))
player_two = Player(input("Player Two: Enter Name "))
play_again = True
while play_again:
game_on = True
round_num = 0
new_deck = Deck()
new_deck.shuffle()
for x in range(26):
player_one.add_cards(new_deck.deal_one())
player_two.add_cards(new_deck.deal_one())
print(f"player one cards: {len(player_one.all_cards)}")
print(f"player two cards: {len(player_two.all_cards)}")
while game_on:
round_num += 1
print(f"Round Number: {round_num}")
if len(player_one.all_cards) == 0:
print(f"{player_one} is out of cards! \n{player_two} won the game!! \nGAME OVER")
game_on = False
a = input("Do you want to play agian? ")
if a == "Y":
play_again = True
else:
play_again = False
break
if len(player_two.all_cards) == 0:
print(f"{player_two} is out of cards! \n{player_one} won the game!! \nGAME OVER")
game_on = False
a = input("Do you want to play agian? ")
if a == "Y":
play_again = True
else:
play_again = False
break
player_one_cards = []
player_one_cards.append(player_one.remove_one())
player_two_cards = []
player_two_cards.append(player_two.remove_one())
at_war = True
while at_war:
if player_one_cards[-1].value > player_two_cards[-1].value:
player_one.add_cards(player_one_cards)
player_one.add_cards(player_two_cards)
at_war = False
elif player_one_cards[-1].value < player_two_cards[-1].value:
player_two.add_cards(player_two_cards)
player_two.add_cards(player_one_cards)
at_war = False
else:
print("****WAR HAS BEGUN****")
if len(player_one.all_cards) < 5:
print(f"{player_one} is unable to play war! \n{player_two} won! \nGAME OVER")
game_on = False
a = input("Do you want to play agian? ")
if a == "Y":
play_again = True
else:
play_again = False
break
elif len(player_two.all_cards) < 5:
print(f"{player_two} is unable to play war! \n{player_one} won! \nGAME OVER")
game_on = False
a = input("Do you want to play agian? ")
if a == "Y":
play_again = True
else:
play_again = False
break
else:
for num in range(5):
player_one_cards.append(player_one.remove_one())
player_two_cards.append(player_two.remove_one())
【问题讨论】:
-
请分享一个最小的示例。
-
在快速查看您的代码之后,我打算分享一个较小的 sn-p。我猜你应该在
if a == "Y":中使用def deal_one()来重置牌组。 -
我添加了显示两位玩家牌的图像
标签: python