How to use classes in python when playing poker?

I take the first course in python, and since my textbook is quite small, I try to get acquainted with classes from experience.

I want to write a program that devotes a few poker hands to Texas Hold'em (on the one hand, to get started), then puts five cards on the table before he looks at all possible hand permutations with 5 table cards and 2 hand cards. (You can combine two cards of the same hand with three of the five cards of the table). In the end, I want to expand it to compare and evaluate hands, and nominate a winner. But it will be later.

What I'm curious about is a good way to structure this problem with classes.

It would be wise to make a hand class that contains a transactional function and contains different hands, and let the deck be a global variable?

#The hand-class is a class for the dealt poker-hands. import random deck = [i for i in range (102, 115)] deck += [i for i in range(202, 215)] deck += [i for i in range(302, 315)] deck += [i for i in range(402, 415)] # I define a card by a prefix number - the suit - and the card value. Jack of spades = 111 if spades corresponds to 1. class Hand: def __init__(self): return None def deal_hand(self): self.hand = [] for i in range (0,2): card = random.choice(deck) self.hand.append(card) deck.remove(card) #def score(): #a function to determine the sore of the hand.. 

What I'm asking: What is the correct way to use classes for this purpose? Should I make another class to hold five cards dealt to the poker table, and another class to hold different permutations?

Or both hands and manual evaluation, a card table and various permutations of manual cards into a table belong to one class?

I do not ask anyone to write me any code, but if you had the time to give me a quick hint about which direction I should look, I would be very grateful! Thank you Marius

+4
source share
3 answers

Firstly, there is no right way to do this. It is all about personal preferences and design considerations.

Personally, I would like for me to create 3 data classes:

Deck to deal with cards and keep track of which cards have already been processed

I would give this class a generic draw method that returns a random map from available maps

Player to track each of the playerโ€™s cards, which can be determined by calling Deck.draw() twice

Table to track cards in a table

Then I would wrap it all in the Game class, which processed all the logic of the game, for example, so that the cards in the table were drawn at the right time

+3
source

If you want to go with classes, a commonly used rule of thumb is for the class to be the thing (s) performing the action (s) or performing the actions with.

For example, a hand does not have the right to practice, and you do not hold a hand with this hand; the handle is processed by the deck. So you can create a Deck class that has a .deal_hand() method that returns Hand .

The Hand class will have a .score() method, any other related to what you actually do with the hand.


However, you do not need to use classes for this. If you want, this is great, but decks and hands can easily be represented by set .

+2
source

You do not need to use classes to use classes (unless you use Java)

As I approach classes, I first think about what โ€œobjectโ€ or โ€œthingโ€ I will need, and then define the properties and methods that will determine this thing. If I'm going to create multiple instances of the same thing, then the class is useful. If I need only one instance, then most likely there will be enough module or global variable.

For example, in your example, you really don't need classes. However, if you are doing something like supporting multiple decks for your game, you need to define a Deck class that can contain and control all the information about its own cards.

Think about poker itself - how does it work?

You have a dealer, you have players, the dealer has one or more decks of cards. The dealer shuffles and then passes the cards to the players and the table. How would you like to define each of these processes as objects?

I would look at a real example and split it into its reusable parts, and this will become your class. So, for example, I would probably look at him and say:

 class Deck(object): """ class for managing a deck of cards """ class Player(object): """ defines properties and methods that an individual player would have """ def __init__( self ): self._cards = [] # hold a player current cards self._chips = 10 # define how much money a player has def clearCards( self ): self._cards = [] def dealCard( self, card ): self._cards.append(card) class Dealer(object): """ defines properties and methods that a dealer would have """ def __init__( self ): self._decks = [] # hold multiple decks for playing with self._stack = [] # holds all the shuffled cards as a def nextCard( self ): """ pop a card off the stack and return it """ return self._stack.pop() def shuffle( self ): for deck in self._decks: deck.shuffle() # shuffle each individual deck # re-populate a shuffled stack of cards self._stack = [] # randomly go through each deck and put each card into the stack def deal( self, *players ): """ Create a new hand based on the current deck stack """ # determine if i need to shuffle if ( len(self._stack) < self._minimumStackCount ): self.shuffle() return Hand(self, *players) class Hand(object): def __init__( self, dealer, *players ): self._dealer = dealer # points back to the dealer self._players = players # defines all the players in the hand self._table = [] # defines the cards on the table self._round = 1 for player in players: player.clearCards() self.deal() def deal( self ): # in holdem, each round you get a different card combination per round round = self._round self._round += 1 # deal each player 2 cards in round 1 if ( round == 1 ): for i in range(2): for player in players: player.dealCard( self._dealer.nextCard() ) # deal the table 3 cards in round 2 (flop) elif ( round == 2 ): self._table = [self._dealer.nextCard() for i in range(3)] # deal the table 1 card in round 2 (turn) elif ( round == 3 ): self._table.append(self._dealer.nextCard()) # deal the table 1 card in round 3 (river) else: self._table.append(self._dealer.nextCard()) 

So, etc.

All of this code is usually pseudo-code to illustrate how to visualize a way to smash things mentally. In fact, the easiest way is to think about a real script and write in plain English how it works, and then the classes will begin to visualize themselves.

+2
source

Source: https://habr.com/ru/post/1433860/


All Articles