对于学习OOP的练习,我正在编写一个战争纸牌游戏,其中卡牌价值最高的人获胜。玩家数量是用户定义的,从1-7开始,不包括计算机播放器。程序和模块太大而无法发布,所以here is a link as an attachment。
如下所示,在方法中,我编码了一个for循环,以确定哪些玩家(从“静止”列表中查询)输赢,这取决于他们的手牌值是否与当前最高牌值相同。如果他们赢了,球员仍然存在,但如果他们输了,他们就会被从名单中删除。我面临的问题是,由于某种原因,该列表中的所有元素都不是由for循环处理的。当只有一个玩家正在玩时,它可以正常工作。但是,如果超过1,那么大约2-3名应该输掉的玩家不会被处理并从列表中删除。可能导致这种情况发生的原因是什么?
def play(self):
# Set list for players who are still playing & add players to list
still = []
for player in self.players:
still.append(player)
still.append(self.dealer)
# Game loop continues as long as there is more than 1 player still playing
while len(still) > 1:
# Display who is in 'still' list
print("\n\n***************************************************************************\n")
print("\nHere are the",len(still) ,"players still remaining: ")
for player in still:
print("\n", player.playerName)
input("\nPress 'enter' to continue.\n")
print("____________________________________")
print("\n\nTime to play. I DECLARE WAR!!!\n")
# Create list that will list all of the card values in hands
cardsInHands = []
# Deal one card to the players, including dealer, and print the hands
self.deck.deal(self.players + [self.dealer], per_hand = 1)
# Set up player cards hand value total and append to cardsInHands
for player in self.players:
print("\n", player)
cardsInHands.append(player.total)
# Set up dealer cards hand value total and append to cardsInHands
print("\n", self.dealer)
cardsInHands.append(self.dealer.total)
# Find the highest card value among the hands
highCard = max(cardsInHands)
print("\n\n***The highest card value is: ", highCard,"***\n")
input("\nPress 'enter' to continue.\n")
""" Determine winners/losers of round"""
# Loop comparing hand of players to highest card value
print("____________________________________")
for remainPlayer in still:
if remainPlayer.total == highCard:
remainPlayer.remain()
else:
still.remove(remainPlayer)
remainPlayer.lose()
# remove everyone's cards
for player in self.players:
player.clear()
self.dealer.clear()
# When loop is done, sole remaining player wins the game
if len(still) == 1:
for remainPlayer in still:
print("\n\n", remainPlayer.playerName,"has won the game! Congratulations!\n\n")
#remainPlayer.win()