Implementing Teen Patti Using Circular Linked List in Python
Teen Patti, also known as Indian Poker, is a popular card game that combines elements of betting, strategy, and luck. It has gained immense popularity not just in India but across various platforms and digital applications. In this blog, we will delve into how to implement a simplified version of Teen Patti using a circular linked list in Python. This article is not only an exciting challenge for programming enthusiasts, but it also illustrates essential data structure concepts.
Understanding Circular Linked Lists
Before we jump into the specifics of Teen Patti, it’s crucial to grasp what a circular linked list is. A circular linked list is a collection of nodes where each node points to the next, and the last node points back to the first node, forming a circle. This structure is particularly useful in scenarios that require a cyclical iteration over a dataset, such as in games, where players take turns in a loop.
The Game of Teen Patti
In our version of Teen Patti, we will focus on a basic structure that allows for a limited number of players, namely two to four players. Each player will receive three cards, and we will implement basic betting functionality. The game will highlight the players in order and facilitate the turn-taking process using a circular linked list.
Setting Up the Circular Linked List
Let’s start by defining the structure of our circular linked list. Each node in this list will represent a player, containing their name and an info dictionary to hold their cards and betting status. We will implement methods to add players, display the current player, and move to the next player.
class Node:
def __init__(self, player_name):
self.player_name = player_name
self.cards = []
self.bet = 0
self.next = None
class CircularLinkedList:
def __init__(self):
self.head = None
def add_player(self, player_name):
new_node = Node(player_name)
if not self.head:
self.head = new_node
self.head.next = self.head # Point to itself
else:
current = self.head
while current.next != self.head:
current = current.next
current.next = new_node
new_node.next = self.head
def display_players(self):
if not self.head:
return "No players in the game."
players = []
current = self.head
while True:
players.append(current.player_name)
current = current.next
if current == self.head:
break
return players
Distributing Cards
Once we have our players set up, the next step is to distribute cards. For simplicity, we will assume a standard deck of cards, and we will randomly allocate three cards to each player from the deck. Here, we can utilize the random library to shuffle the deck and distribute cards effectively.
import random
class TeenPattiGame:
def __init__(self):
self.players_list = CircularLinkedList()
self.deck = self.create_deck()
def create_deck(self):
suits = ['Hearts', 'Diamonds', 'Clubs', 'Spades']
ranks = ['2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K', 'A']
return [f"{rank} of {suit}" for suit in suits for rank in ranks]
def shuffle_deck(self):
random.shuffle(self.deck)
def deal_cards(self):
self.shuffle_deck()
current = self.players_list.head
while current:
current.cards = self.deck[:3] # Give three cards
self.deck = self.deck[3:] # Remove dealt cards
current = current.next
if current == self.players_list.head:
break
Implementing the Betting System
No game of Teen Patti would be complete without a betting mechanism. We will add a simple betting system that allows players to place bets during their turn. Each player can either call, raise, or fold, leading to subsequent turns until all but one player folds.
def place_bet(self, player_name, amount):
current = self.players_list.head
while current:
if current.player_name == player_name:
current.bet += amount
break
current = current.next
if current == self.players_list.head:
break
Managing Turns
To effectively manage the turns among players, we will implement a turn management system linked to our circular linked list structure. This will ensure that after a player takes their action, the next player is automatically identified and prompted to make their move. This addresses the primary requirement of the cyclic behavior of players in Teen Patti.
def next_turn(self):
if self.players_list.head:
current = self.players_list.head
while True:
print(f"It's {current.player_name}'s turn.")
# Here we can include logic for taking action: betting or folding
current = current.next
if current == self.players_list.head:
break
Putting It All Together
Now that we have defined the essential components of our Teen Patti game using a circular linked list, the main loop can be constructed for running the actual game session. This loop will include initiating players, dealing cards, managing turns, and accepting bets until the game reaches a conclusion, which could be defined as either all but one player folding or the players opting to show their hands.
def start_game(self, player_names):
for name in player_names:
self.players_list.add_player(name)
self.deal_cards()
self.next_turn()
# Example Execution
game = TeenPattiGame()
game.start_game(['Alice', 'Bob', 'Charlie', 'Diana'])
Final Thoughts
This implementation is a simple yet effective demonstration of using a circular linked list in a game scenario. It provides a seamless way for players to take turns and manage their cards and bets. As the game evolves, you can add further features such as the ability to compare hands, eliminate players, or incorporate a more sophisticated user interface.
By utilizing Python's object-oriented capabilities alongside the principles of linked list data structures, we create a robust foundation for a digital version of Teen Patti. Such projects not only strengthen your programming skills but also provide unique insights into game development and design. Throughout this article, we showcased the simplicity and effectiveness of circular linked lists in maintaining essential game flow dynamics, which can be adapted for various applications beyond card games.




