0

I'm attempting to model the game of Pazzak (from the star wars game) in python.

The current code creates a hand from possible 24 cards (-6 to 6) x 2 repetitions of each.

This hand is stored in 2 class variables (player_hand, and player_hand_history). the idea behind this repetition was that player_hand would keep track of whether a particular card had yet to be played, and player_hand_history would keep track of the whole hand.

As the card is played, I use .remove() to remove it from player_hand. For reasons I can't understand, when I remove it from player_hand, the number is also removed from player_hand history.

An example of buggy output from the below code shows

Hand history beginning [[-3, 5, -4, -2]]

play card2 [[5, -4, -2]]

Hand history end [[5, -4, -2]]

It looks like from this that the variable player_hand_history is formed correctly, but is somehow modified by running the line

self.player_hand.remove(x)

Can someone help me understand why this happens?

import random

class Player:
    
    #class variables that are important
    player_wins = 0
    player_losses = 0
    player_turn = False
    player_hand = []
    player_hand_history = []
    pazzak_deck_total = 0

    
    def deal_hand(self):
        player_deck = []
        
        #create the Pazzak deck, by calling 2 ranges. The 3rd for statment is used to double up the list
        for x in range(-6,0):
            player_deck.append(x)
        for x in range(1,7):
            player_deck.append(x)
        for x in range(len(player_deck)):
            player_deck.append(player_deck[x])
            
        #picks 4 random cards, and addes them to the player hand
        for x in range(4):
            card = random.randint(0,len(player_deck))
            self.player_hand.append(player_deck.pop(card-1))
        self.player_hand_history.append(self.player_hand)
        print("Hand history beginning",self.player_hand_history)


class St1(Player):
    #this function plays an entire game of Pazzak for a single player
    def play_cards(self):
        
        #add cards to the general table normally until a specified number
        while self.pazzak_deck_total <= 16:
            self.pazzak_deck_total +=random.randint(1,10)
            
        #look in player hand
        for x in self.player_hand:
            new_result = x + self.pazzak_deck_total
            
            #play card if it (1) gets you an 18,19,20, and adds to the result
            if new_result >= 18 and new_result <= 20 and new_result > self.pazzak_deck_total:
                print("play card1",pl1.player_hand_history)
                self.player_hand.remove(x)
                self.pazzak_deck_total += x
                break
            
            #play card if it would save the hand
            elif self.pazzak_deck_total > 20 and new_result <= 20:
                self.player_hand.remove(x)
                self.pazzak_deck_total += x
                print("play card2",pl1.player_hand_history)
                break


def play_hand(player):
    player.pazzak_deck_total = 0
    player.deal_hand()
    player.play_cards()
  


def main():
    play_hand(pl1)
    print("Hand history end",pl1.player_hand_history)
    
pl1 = St1()
main()



  • 1
    `self.player_hand_history.append(self.player_hand)` This does not append a _copy_ of player_hand; it appends _the actual object_. If player_hand changes, then hand_history also changes. – John Gordon Jan 20 '22 at 02:06
  • Does this answer your question? [List changes unexpectedly after assignment. Why is this and how can I prevent it?](https://stackoverflow.com/q/2612802/4518341) – wjandrea Jan 20 '22 at 02:11

0 Answers0