Implementing Teen Patti Using Circular Linked List in Python
Teen Patti, also known as Indian Poker, is a popular card game that is played among friends and is often seen in casinos across India. It’s a game of skill, strategy, and luck, and has captivated many players around the globe. In this article, we will explore how to implement a game of Teen Patti using a circular linked list in Python.
Understanding Circular Linked List
A circular linked list is a data structure where the last node points back to the first node, forming a circle. This structure is particularly useful in games and applications requiring round-robin sorting or sequential access. In Teen Patti, players take turns, and a circular linked list perfectly models this behavior by allowing easy traversal of players in a loop.
Defining the Player Class
First off, let's create a simple Player class that will represent each player in the game. This class will hold the player's name and their current hand of cards.
class Player:
def __init__(self, name):
self.name = name
self.hand = []
def receive_card(self, card):
self.hand.append(card)
def show_hand(self):
return f"{self.name}: {', '.join(self.hand)}"
Creating the Circular Linked List for Players
Now we need to create the circular linked list to hold the players. Each node will store a Player object.
class Node:
def __init__(self, player):
self.player = player
self.next = None
class CircularLinkedList:
def __init__(self):
self.head = None
def add_player(self, player):
new_node = Node(player)
if self.head is None:
self.head = new_node
new_node.next = self.head
else:
current = self.head
while current.next != self.head:
current = current.next
current.next = new_node
new_node.next = self.head
def display(self):
if self.head is None:
return "No players in the game."
current = self.head
players = []
while True:
players.append(current.player.name)
current = current.next
if current == self.head:
break
return "Players: " + ", ".join(players)
Dealing Cards
Next, we’ll implement the part of the game that deals cards to each player. Assume we have a deck of cards, and we will randomly distribute three cards to each player.
import random
class Deck:
def __init__(self):
suits = ['Hearts', 'Diamonds', 'Clubs', 'Spades']
ranks = ['2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K', 'A']
self.cards = [f"{rank} of {suit}" for suit in suits for rank in ranks]
random.shuffle(self.cards)
def deal_card(self):
return self.cards.pop() if self.cards else None
Putting it All Together
Now let’s assemble these components. We will create a new game instance, add players, deal cards, and print the players’ hands.
def main():
# Create a circular linked list to hold players
player_list = CircularLinkedList()
# Create a deck of cards
deck = Deck()
# Add players to the game
for player_name in ['Alice', 'Bob', 'Charlie', 'David']:
player = Player(player_name)
player_list.add_player(player)
print(player_list.display())
# Deal cards to each player
current = player_list.head
for _ in range(3): # each player gets 3 cards
while True:
current.player.receive_card(deck.deal_card())
current = current.next
if current == player_list.head:
break
# Show each player's hand
current = player_list.head
while True:
print(current.player.show_hand())
current = current.next
if current == player_list.head:
break
if __name__ == "__main__":
main()
Game Logic and Functionality
At this stage, we successfully have a circular linked list that holds our players and provides them with cards. The next step would be to implement the core game logic, including betting rounds, determining the winner, and handling user input for plays. Each aspect would require careful planning and coding to ensure a smooth gaming experience. Here are a few components you might need to add:
- Betting System: Allow players to place bets during each round of the game.
- Turns Management: Implement a way for players to take turns. This can be done using the circular linked list to cycle through each player.
- Comparing Hands: After the betting rounds are over, the player with the best hand will need to be determined based on Teen Patti rules.
- User Input: Allow players to make choices such as folding or betting.
Further Enhancements
To enhance the game further, you can consider adding the following features:
- Graphical User Interface (GUI): Using libraries like Tkinter or Pygame to create an interactive game interface.
- Network Play: Allowing players to connect over the internet and play against each other online.
- Statistics Tracking: Keep track of wins, losses, and other statistics for each player throughout multiple games.
Wrapping Up the Implementation
Writing a full-fledged card game like Teen Patti in Python is an exciting project that combines various programming concepts like data structures, classes, and user interactions. By using a circular linked list, we can easily manage player turns, enhancing both the coding experience and gameplay. The key to a successful game implementation lies in thorough testing and iterative refinement, so take your time to develop and polish your code.