0

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()
misterManSam
  • 24,303
  • 11
  • 69
  • 89
  • 1
    You are removing items from `still` while iterating it in `still.remove(remainPlayer)`. Could cause problems. – Rliger Jul 14 '14 at 18:09
  • @Rliger: Not *Could* but *Will*. – Martijn Pieters Jul 14 '14 at 18:11
  • @MartijnPieters meh only in this case ... im pretty sure you could iterate in reverse for example and be ok ... (not advocating that ... but I think you could if for some terrible reason you felt like doing so) – Joran Beasley Jul 14 '14 at 18:53
  • @JoranBeasley: yeah, looping in reverse can help in many cases. But that's not the case here. – Martijn Pieters Jul 14 '14 at 18:54
  • @MartijnPieters meh I was just quibbling ... in general I think you should never modify a iterator as you iterate it in python – Joran Beasley Jul 14 '14 at 18:56
  • Thanks for the answers everyone! Putting 'still[:]' in the for-loop helped fix the problem when there is only one winner, but it only affects the copied list. When there is more than one winner and the while-loop executes again, the list is exactly the same with no removals. Is there a way to use that newer copied & modified 'still[:]' list as the still[] when the while-loop executes again? – user3754248 Jul 14 '14 at 19:08
  • A related question - [link](http://stackoverflow.com/questions/7573115/delete-item-from-list-in-python-while-iterating-over-it) – Rliger Jul 14 '14 at 19:13
  • Awesome! Everything is working great now. Thanks! – user3754248 Jul 14 '14 at 19:44

0 Answers0