Building a poker game from scratch is a fantastic way to learn Python programming, object-oriented design, and a bit of game theory. In this guide, you’ll learn how to create a simple, playable poker game in Python that runs in the command line. We’ll cover core concepts, from deck construction and hand evaluation to a clean game loop and a tiny AI opponent. The approach here emphasizes readability and extensibility, so you can extend the game later with more features, a graphical interface, or more sophisticated AI.
The project will implement a simplified two-player poker game in the console, inspired by Texas Hold'em but kept intentionally approachable. The main features include:
Before diving into code, it helps to outline a minimal, extensible structure. You can implement everything in a single Python file or split into multiple modules later. For this guide, you’ll see the following responsibilities split logically:
You only need Python 3.x. A text editor or IDE (VS Code, PyCharm, or a simple editor) is sufficient. Optional enhancements later could include packaging with setuptools, adding a requirements.txt if you use extra libraries, or creating a tiny GUI with Tkinter or PyGame. For now, this is intentionally lightweight and CLI-based to keep the focus on game logic and Python fundamentals.
The following code blocks provide the essential building blocks. Copy them into a Python file (for example, poker_cli.py) and run it to start the game. The evaluator implements a common seven-card to five-card hand ranking, including high-card, pair, two pair, trips, straight, flush, full house, four of a kind, and straight flush.
# Basic card and deck definitions
from itertools import combinations
import random
RANKS = '23456789TJQKA'
SUITS = 'SHDC' # Spades, Hearts, Diamonds, Clubs
def make_deck():
return [r + s for r in RANKS for s in SUITS]
def rank_value(card):
return RANKS.index(card[0]) + 2 # 2..14
def card_suit(card):
return card[1]
# 5-card evaluator: returns (category, tiebreaker_list)
# Categories: 0-high card, 1-one pair, 2-two pair, 3-three of a kind,
# 4-straight, 5-flush, 6-full house, 7-four of a kind, 8-straight flush
def evaluate_5card(cards5):
ranks = sorted([rank_value(c) for c in cards5], reverse=True)
suits = [card_suit(c) for c in cards5]
# Count occurrences of each rank
counts = {}
for r in ranks:
counts[r] = counts.get(r, 0) + 1
# Sort by count then rank
count_groups = sorted(((cnt, r) for r, cnt in counts.items()), reverse=True)
is_flush = all(s == suits[0] for s in suits)
# Straight detection (handle wheel A-5)
uniq_ranks = sorted(set(ranks), reverse=True)
is_straight = False
top_straight = None
if len(uniq_ranks) >= 5:
# Check all 5-rank sequences
for i in range(len(uniq_ranks) - 4):
window = uniq_ranks[i:i+5]
if window == list(range(window[0], window[0]-5, -1)):
is_straight = True
top_straight = window[0]
break
# Wheel straight: A-5
if not is_straight and set([14, 5, 4, 3, 2]).issubset(set(ranks)):
is_straight = True
top_straight = 5
if is_straight and is_flush:
return (8, [top_straight])
if count_groups[0][0] == 4:
quad = count_groups[0][1]
kicker = max([r for r in ranks if r != quad])
return (7, [quad, kicker])
if count_groups[0][0] == 3 and count_groups[1][0] == 2:
trip = count_groups[0][1]
pair = count_groups[1][1]
return (6, [trip, pair])
if is_flush:
return (5, ranks)
if is_straight:
return (4, [top_straight])
if count_groups[0][0] == 3:
trip = count_groups[0][1]
kickers = sorted([r for r in ranks if r != trip], reverse=True)[:2]
return (3, [trip] + kickers)
if count_groups[0][0] == 2 and count_groups[1][0] == 2:
high_pair = max(count_groups[0][1], count_groups[1][1])
low_pair = min(count_groups[0][1], count_groups[1][1])
kicker = max([r for r in ranks if r not in (high_pair, low_pair)])
return (2, [high_pair, low_pair, kicker])
if count_groups[0][0] == 2:
pair = count_groups[0][1]
kickers = sorted([r for r in ranks if r != pair], reverse=True)[:3]
return (1, [pair] + kickers)
return (0, ranks)
# Best hand from 7 cards: try all 5-card combinations and pick the best
def best_hand(cards7):
best = None
for combo in combinations(cards7, 5):
rank = evaluate_5card(list(combo))
if best is None or rank > best:
best = rank
return best
def compare_hands(h1, h2):
# h1 and h2 are tuples (category, tiebreaker_list)
if h1[0] != h2[0]:
return 1 if h1[0] > h2[0] else -1
# Compare tiebreaker lists lexicographically
for a, b in zip(h1[1], h2[1]):
if a != b:
return 1 if a > b else -1
return 0
# Helpers to display cards nicely
def display_card(card):
rank_map = {'T':'10','J':'Jack','Q':'Queen','K':'King','A':'Ace'}
r = card[0]
suit = card[1]
rank_name = rank_map.get(r, r)
suit_names = {'S':'Spades','H':'Hearts','D':'Diamonds','C':'Clubs'}
return f"{rank_name} of {suit_names[suit]}"
def show_hand(label, cards):
return f"{label}: " + ", ".join([display_card(c) for c in cards])
# CLI game loop: two players (human vs AI) with Texas Hold'em style hands
def deal_round():
deck = make_deck()
random.shuffle(deck)
# Hole cards
human = [deck.pop(), deck.pop()]
ai = [deck.pop(), deck.pop()]
# Community cards (flop 3, turn 1, river 1)
community = []
# Flop
community.extend([deck.pop(), deck.pop(), deck.pop()])
# Turn
community.append(deck.pop())
# River
community.append(deck.pop())
# Evaluate best hand using 2 hole cards + 5 community
human_best = best_hand(human + community)
ai_best = best_hand(ai + community)
result = compare_hands(human_best, ai_best)
if result > 0:
winner = "Human"
elif result < 0:
winner = "AI"
else:
winner = "Draw"
return {
"human": human,
"ai": ai,
"community": community,
"human_best": human_best,
"ai_best": ai_best,
"winner": winner
}
def hand_to_string(hand_rank):
# Simple mapping for display purposes
category_names = [
"High Card", "One Pair", "Two Pair", "Three of a Kind",
"Straight", "Flush", "Full House", "Four of a Kind",
"Straight Flush"
]
cat = hand_rank[0]
# We'll display category and top values succinctly
tiebreakers = ", ".join(str(x) for x in hand_rank[1])
return f"{category_names[cat]} ({tiebreakers})"
Put it all together with a simple loop that lets you play multiple rounds. The human can press Enter to deal a new round or type q to quit. After each round, the program prints both players’ hole cards, the community cards, and the outcome. This structure keeps the code approachable while still illustrating a real poker flow.
def main():
print("Welcome to Python Poker (CLI). Two players: You vs. AI.")
print("Each round deals two hole cards per player and five community cards.")
print("The best five-card hand wins. No betting for this simple version.")
print("Enter 'q' at any prompt to quit.\n")
rounds = 0
while True:
choice = input("Press Enter to deal a new round or 'q' to quit: ").strip().lower()
if choice == 'q':
print("Thanks for playing! Goodbye.")
break
result = deal_round()
rounds += 1
# Human and AI cards
print(show_hand("Your hand", result["human"]))
print(show_hand("AI hand", result["ai"]))
print(show_hand("Community", result["community"]))
print("Human best:", hand_to_string(result["human_best"]))
print("AI best :", hand_to_string(result["ai_best"]))
print(f"Round {rounds} result: {result['winner']}\n")
if __name__ == "__main__":
main()
This version focuses on clarity and expandability rather than perfect realism. Here are some considerations and potential improvements you can attempt as learning exercises:
For a single-file CLI tool, you can simply share the Python script with others. If you want a more formal package, consider packaging it with setuptools, adding a requirements.txt (though this project has zero external dependencies), and providing a small README with instructions. If you later add a GUI, you’ll also want to ensure the UI is cross-platform and robust for different terminal sizes.
In this tutorial, you learned how to design and implement a compact poker game in Python that runs in the terminal. The main components include a deck, a hand evaluator for 5-card hands, a best-hand calculator over seven cards, and a simple two-player game loop with a human and an AI opponent. The project emphasizes maintainability and extensibility, so you can start with a clear minimal viable product and progressively add features such as betting, multiple rounds, an improved AI, and a graphical interface.
Whether it’s me-time or tea-time, Teen Patti Master offers quick games that lift your mood.
No pressure, no judgment—Teen Patti Master is filled with friendly players and kind chats.
Enjoy colorful events, surprise bonuses, and themed games that keep things joyful every day.
Private tables, secure data, and a peaceful space—Teen Patti Master is poker you can trust.
Copyright © Teen Patti Master All Rights Reserved