1

There's this poker repo: https://github.com/fedden/poker_ai

In the README.md file there is a section titled "Playing a game of poker"

In this section the author provides a short script to play a round of poker using his repo:

from poker_ai import utils
from poker_ai.ai.dummy import RandomPlayer
from poker_ai.poker.table import PokerTable
from poker_ai.poker.engine import PokerEngine
from poker_ai.poker.pot import Pot

# Seed so things are deterministic.
utils.random.seed(42)

# Some settings for the amount of chips.
initial_chips_amount = 10000
small_blind_amount = 50
big_blind_amount = 100

# Create the pot.
pot = Pot()
# Instanciate six players that will make random moves, make sure 
# they can reference the pot so they can add chips to it.
players = [
    RandomPlayer(
        name=f'player {player_i}',
        initial_chips=initial_chips_amount,
        pot=pot)
    for player_i in range(6)
]
# Create the table with the players on it.
table = PokerTable(players=players, pot=pot)
# Create the engine that will manage the poker game lifecycle.
engine = PokerEngine(
    table=table,
    small_blind=small_blind_amount,
    big_blind=big_blind_amount)
# Play a round of Texas Hold'em Poker!
engine.play_one_round()

This short script seems to work, but I would like to be able to play a round of poker but choosing each player's cards and the community cards myself. Currently this script only allows me to play a round of poker with random players doing random actions getting random cards which I cannot even see.

Solution I have tried

I've tried implementing card picking by making a small change in the repo.

I changed the pick() function in the deck.py file and the deal_card() function in the dealer.py file slightly, but it did not work for me, don't know why. Where I implemented input('Pick a card:') doesn't seem to work because I do not get prompted to pick cards when I run the round (and, in theory, the python inbuilt function input() should prompt me to type something when called). The small changes are shown below.

    def pick(self, random: int = 0) -> Card:
        """Return a card from the deck.

        Parameters
        ----------
        random : int
            If this is 0, return a completely random card, else return the
            next card in the deck (1), else you pick (2)

        Returns
        -------
        card : Card
            The card that was picked.
        """
        if not len(self._cards_in_deck):
            raise ValueError("Deck is empty - please use Deck.reset()")
        elif 0:
            index: int = np.random.randint(len(self._cards_in_deck), size=None)
        elif 1:
            index: int = len(self._cards_in_deck) - 1
        elif 2:
            index: int = self._cards_in_deck.index(input('Pick a card:'))
        card: Card = self._cards_in_deck.pop(index)
        self._dealt_cards.append(card)
        return card
    def deal_card(self) -> Card:
        """Return a completely random card."""
        return self.deck.pick(2)

After this change I tweaked the "Playing a game of poker" script printed above, and got this:

Script:

from poker_ai import utils
from poker_ai.poker.random_player import RandomPlayer
from poker_ai.poker.table import PokerTable
from poker_ai.poker.engine import PokerEngine
from poker_ai.poker.pot import Pot

# Seed so things are deterministic.
utils.random.seed(42)

# Some settings for the amount of chips.
initial_chips_amount = 10000
small_blind_amount = 50
big_blind_amount = 100

# Create the pot.
pot = Pot()
# Instanciate six players that will make random moves, make sure 
# they can reference the pot so they can add chips to it.
players = [
    RandomPlayer(
        name='player 0',
        initial_chips=initial_chips_amount,
        pot=pot),
    RandomPlayer(
        name='jaime',
        initial_chips=56700,
        pot=pot)
]
# Create the table with the players on it.
table = PokerTable(players=players, pot=pot)
# Create the engine that will manage the poker game lifecycle.
engine = PokerEngine(
    table=table,
    small_blind=small_blind_amount,
    big_blind=big_blind_amount)

# Play a round of Texas Hold'em Poker!
table.pot.reset(), pp.pprint(players)
engine._assign_order_to_players(), pp.pprint(players)
engine._assign_blinds(), pp.pprint(players)
table.dealer.deal_private_cards(table.players), pp.pprint(players)
engine._betting_round(first_round=True), pp.pprint(players)
table.dealer.deal_flop(table), pp.pprint(players)
engine._betting_round(), pp.pprint(players)
table.dealer.deal_turn(table), pp.pprint(players)
engine._betting_round(), pp.pprint(players)
table.dealer.deal_river(table), pp.pprint(players)
engine._betting_round(), pp.pprint(players)
engine.compute_winners(), pp.pprint(players)
engine._round_cleanup(), pp.pprint(players)

Output:

[   <Player name="player 0" n_chips=10000 n_bet_chips=00000 folded=0>,
    <Player name="jaime" n_chips=56700 n_bet_chips=00000 folded=0>]
[   <Player name="player 0" n_chips=10000 n_bet_chips=00000 folded=0>,
    <Player name="jaime" n_chips=56700 n_bet_chips=00000 folded=0>]
[   <Player name="player 0" n_chips=09950 n_bet_chips=00050 folded=0>,
    <Player name="jaime" n_chips=56600 n_bet_chips=00100 folded=0>]
[   <Player name="player 0" n_chips=09950 n_bet_chips=00050 folded=0>,
    <Player name="jaime" n_chips=56600 n_bet_chips=00100 folded=0>]
[   <Player name="player 0" n_chips=09900 n_bet_chips=00100 folded=0>,
    <Player name="jaime" n_chips=56600 n_bet_chips=00100 folded=0>]
[   <Player name="player 0" n_chips=09900 n_bet_chips=00100 folded=0>,
    <Player name="jaime" n_chips=56600 n_bet_chips=00100 folded=0>]
[   <Player name="player 0" n_chips=09900 n_bet_chips=00100 folded=0>,
    <Player name="jaime" n_chips=56600 n_bet_chips=00100 folded=0>]
[   <Player name="player 0" n_chips=09900 n_bet_chips=00100 folded=0>,
    <Player name="jaime" n_chips=56600 n_bet_chips=00100 folded=0>]
[   <Player name="player 0" n_chips=09800 n_bet_chips=00200 folded=0>,
    <Player name="jaime" n_chips=56500 n_bet_chips=00200 folded=0>]
[   <Player name="player 0" n_chips=09800 n_bet_chips=00200 folded=0>,
    <Player name="jaime" n_chips=56500 n_bet_chips=00200 folded=0>]
[   <Player name="player 0" n_chips=09800 n_bet_chips=00200 folded=1>,
    <Player name="jaime" n_chips=56500 n_bet_chips=00200 folded=0>]
[   <Player name="player 0" n_chips=09800 n_bet_chips=00000 folded=1>,
    <Player name="jaime" n_chips=56900 n_bet_chips=00000 folded=0>]
[   <Player name="player 0" n_chips=09800 n_bet_chips=00000 folded=1>,
    <Player name="jaime" n_chips=56900 n_bet_chips=00000 folded=0>]
(None, None)

As you can see, my attempt did not work, since at no point was I prompted to pick a card.

What should I change exactly in order to get this short script to allow me to pick the cards?

Jaime
  • 21
  • 4
  • Maybe first use `print()` (and `print(type(...))`, `print(len(...))`, etc.) to see which part of code is executed and what you really have in variables. It is called `"print debuging"` and it helps to see what code is really doing. BTW: you could use `print()` in original function and in your function to compare them. – furas Oct 21 '22 at 11:59

0 Answers0