For an exercise to learn OOP I'm coding a card game of war where whoever has the highest card value wins. The amount of players are user-defined, going from 1-7 excluding the computer player. The program and the modules are too big to post, so here is a link as an attachment.
As shown below, in the method I coded a for-loop to determine which players (queried from the 'still' list) win or lose depending if their hand values are the same as the current highest card value. If they win, the players remain, but if they lose they are then removed from the list. The problem I'm facing is that for some reason not all of the elements in that list are processed by the for-loop. When only one player is playing it works fine. However, if more than 1 is playing then around 2-3 players who should've lost do not get processed and removed from the list. What could be causing this to happen?
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()