Home > Back-end >  Choose which item to pop from list
Choose which item to pop from list

Time:07-01

building a "little" card game.

Lets look at my problem.

I can draw cards from the deck and put the last drawn card back on top.

But I want to decide which card should placed back on top!

import random

lets assume we have some "cards"

Cards = [1,2,3,4,5,6]

a Deck Class

class Deck():                             # Deck class definition for deck of cards
    

    def __init__(self, Cards):                   # Creating an empty list of cards. Constructor.
        self.cards = Cards
      
    
    def deal(self, i=-1):                  # Method to deal the top most card of the deck
        return self.cards.pop(i)

    
    def shuffle(self):                     # Method to shuffle the deck of cards
        random.shuffle(self.cards)         # Shuffling the card deck  

and a Player Class

class Player():
    
    def __init__(self):


        # creat deck for each player
        self.deck = Deck([1,2,3,4,5,6,7,8]) 
        # shuffle the deck before the hand is drawn
        self.deck.shuffle()
        
        # draw the starthand
        self.hand = []
        for i in range(3):
            card = self.deck.deal()
            self.hand.append(card)
        
                        

    def draw(self):
        card = self.deck.deal()
        return self.hand.append(card)

    
    def on_top(self, i=-1):
        card = self.hand.pop(i)
        return self.deck.cards.append(card)

When the Player is created with

player_1 = Player()

the list of cards is shuffled and 3 cards are drawn

lets check the hand

player_1.hand

[1, 4, 5]

when a card is drawn

player_1.draw()

and hand checked again

player_1.hand

[1, 4, 5, 3]

the next item from list is drawn.

Ok lets place the item back on top

player_1.on_top()

player_1.hand

[1, 4, 5]


Ok perfect. But how can I choose the card to put back on top of the deck by my self?

And the very next question would be:

How to search for a specific Item in the list and choose to draw?

You should imagine that the numbers are replaced by a front of a card.

Thanks already for helping me!

CodePudding user response:

I'm not quite sure if I can help you enough, still struggling to learn Python OOP, but I will give it a try with my noob mindset

I think with the way you defined your on_top() you could simply give and index as argument i

>>> player1.on_top(1)    # choosing 4 (with index 1) to be put on top
>>> player1.hand
[1, 5]
  • Now I will assume that what you meant with

How to search for a specific Item in the list and choose to draw?

means that you want to on_top() a card from your hand (without knowing which index it has)

>>> player_1.on_top(player_1.hand.index(5))    # let's say you put away the card 5
>>> player_1.hand
[1]
  • If instead you want to draw() it from the deck, had to change your function a bit
...
def draw(self, i=-1):    # adding `i` argument here 
        card = self.deck.deal(i)    # and here
        return self.hand.append(card)
...

so that this could work out

>>> player_1.draw(player_1.deck.cards.index(5))
>>> player_1.hand
[1, 5]

I guess this way of writing would be hard to read

  • Related