0

I'm a beginner at pyhton. i'm working on a poker game. currently i'm stuck at dealing every player two cards. dealing the flop, turn and river work fine but dealing the player cards doesn't work. I don't understand why it doesn't work. Any solutions, explanation, other approaches advice are appreciated. Link to my github with the full code Github

    def deal(self, location, times=1): 
        for i in range(times):
            location.cards.append(self.pop(0)) 
    def deal_hole(self):
        count = 0
        while count < 2:
            for player in self.players_not_out:
                self.deck.deal(player, 1)
            count += 1

The Deal_hole method should append two cards to all the player class instances at the attribute cards which is a list, but it returns a empty list. i used the following code to test the method

list_of_players = [player('a'), player('b'), player('c'), player('d')]

game = game('q')
deck = deck()

a = player('a')
b = player('b')
c = player('c')
d = player('d')

game.shuffle_deck()
game.deal_hole()

player.show_hand(a,a)

This returned: a's hand: []

  • Could you, please, also share/push to repo the code that you use to verify that `deal_hole` does not work as intended? I cannot reproduce it (worked well for me, although some bad practices in code could be improved ;)). – yeti Nov 01 '22 at 11:46
  • @yeti i updated the post. any pointers on the bad practises? – Nick Belterman 225538 Nov 01 '22 at 12:55
  • You don't seem to understand your own code. The ```game.deal_hole()``` call does not access the ```a``` player at all. What do you expect? A question like this is not useful to others. – relent95 Nov 01 '22 at 14:31
  • I'm trying to acces the a player. your answer is not helpful. – Nick Belterman 225538 Nov 01 '22 at 14:40
  • I don't play cards, and this is not an answer but more of a tip/hint, once you started working with your classes in a list you have to use that list everywhere you go as it's your player's list, once you use `player("a")` you are creating an entire new player in which you don't need nor want, going to `class game` you assign `list_of_players` and then not use it, it's your list of players, means the dealer is one of the players aka `list_of_players` not a new player aka `player("a")` and the rest as well, if you apply this else where you will have your player's hand of cards. does this work? – mr.xed Nov 01 '22 at 18:52
  • If you can't understand my previous comment, you are programming in a wrong way. See [Rubber duck programming](https://en.wikipedia.org/wiki/Rubber_duck_debugging). That said, you should access a player like ```game.list_of_players[0]```. – relent95 Nov 02 '22 at 10:04

1 Answers1

1

In the code you use for testing, by doing the following:

a = player('a')

you do not assign player('a') from list_of_players to variable a, but create another player with name "a". That is why you cannot see its cards. But it will work properly, when you do it like this:

a = list_of_players[0]
a.show_hand("a")

And speaking about good practices, you should not pass the player's name in the show_hand() method, because it takes self.name for printing, not the name you pass. So it's not used and its presence there is counterintuitive.

Apart from that:

  1. show_name() should return the string, not print it.
  2. You don't have to inherit after object like class Player(object):, this means the same: class Player:. The inheritance is implicit.
  3. You should pass list_of_players to Game's __init__ method, not intialize it outside and use that variable.
  4. Classes should be named starting with a capital letter (CapCase, CamelCase), according to Python Style Guide.

...and some more.

You can get your code's review on Code Review Stack Exchange.

yeti
  • 125
  • 1
  • 10