Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9057337
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T14:25:45+00:00 2026-06-16T14:25:45+00:00

I’ve been trying to make a poker game bot for IRC, but I can’t

  • 0

I’ve been trying to make a poker game bot for IRC, but I can’t seem to get my head around dealing the cards.

I know this algorithm is quite inefficient, but it’s the best I could come up with using my current Python skills. Any improvements are welcome!

players is a dictionary where the key is a player’s nickname, and the value is the amount of money they have.

When the code is run, if there is only 1 player, it gives 5 cards, as it should. But if there are 2 players, it generates anywhere from 4 to 6 cards each. I haven’t tested with more players yet.

Some of the variables initialised beforehand:

numberOfPlayers = 0 #The numerical value of the amount of players in the game
players = {} #The nickname of each player and the amount of money they have
bets = {} #How much each player has bet
decks = 1 #The number of decks in play
suits = ["C","D","H","S"] #All the possible suits (Clubs, Diamonds, Hearts, Spades)
ranks = ["2","3","4","5","4","6","7","8","9","J","Q","K","A"] #All the possible ranks (Excluding jokers)
cardsGiven = {} #The cards that have been dealt from the deck, and the amount of times they have been given. If one deck is in play, the max is 1, if two decks are in play, the max is 2 and so on...
hands = {} #Each players cards

The code:

def deal(channel, msgnick):
    try:
        s.send("PRIVMSG " + channel + " :Dealing...\n")
        for k, v in players.iteritems():
               for c in range(0, 5):
                suit = random.randrange(1, 4)
                rank = random.randrange(0,12)
                suit = suits[suit]
                rank = ranks[rank]
                card = rank + suit
                print(card)
                if card in cardsGiven:
                    if cardsGiven[card] < decks:
                        if c == 5:
                            hands[k] = hands[k] + card
                            cardsGiven[card] += 1
                        else:
                            hands[k] = hands[k] + card + ", "
                            cardsGiven[card] += 1
                    else:
                        c -= 1
                else:
                    if c == 5:
                        hands[k] = hands[k] + card
                        cardsGiven[card] = 1
                    else:
                        hands[k] = hands[k] + card + ", "
                        cardsGiven[card] = 1
            s.send("NOTICE " + k + " :Your hand: " + hands[k] + "\n")
    except Exception:
        excHandler(s, channel)

If any examples or further explanations are needed, please ask 🙂

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T14:25:46+00:00Added an answer on June 16, 2026 at 2:25 pm

    I would use itertools.product to get the possible value of cards into a list, then shuffle that and take of 5 cards at a time for each player you want.

    from itertools import product
    from random import shuffle
    
    suits = ["C","D","H","S"] 
    ranks = ["2","3","4","5","4","6","7","8","9","J","Q","K","A"]
    
    cards = list(r + s for r, s in product(ranks, suits))
    shuffle(cards)
    
    print cards[:5], cards[5:10] # change this into a suitable for loop to slice
    ['4D', 'KC', '5H', '9H', '7D'] ['2D', '4S', '8D', '8S', '4C']
    

    And you could use the following recipe from itertools to just get the next 5 cards depending on how many players.

    def grouper(n, iterable, fillvalue=None):
        from itertools import izip_longest
        "Collect data into fixed-length chunks or blocks"
        # grouper(3, 'ABCDEFG', 'x') --> ABC DEF Gxx
        args = [iter(iterable)] * n
        return izip_longest(fillvalue=fillvalue, *args)
    
    hand = grouper(5, cards) 
    for i in xrange(5): # deal for 5 players...
        print next(hand) # each time we call this, we get another 5 cards
    
    ('4D', 'KC', '5H', '9H', '7D')
    ('2D', '4S', '8D', '8S', '4C')
    ('AD', '2H', '4S', 'KS', '9S')
    ('6H', 'AH', '4H', '5S', 'KD')
    ('6S', 'QD', '3C', 'QC', '8H')
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I have a jquery bug and I've been looking for hours now, I can't
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I am trying to find ID3V2 tags from MP3 file using jid3lib in Java.
I have a French site that I want to parse, but am running into
I am trying to render a haml file in a javascript response like so:
This could be a duplicate question, but I have no idea what search terms

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.