Creating a Teen Patti Game using Circular Linked List in Python

Teen Patti, also known as Indian Poker, is a popular game played in many parts of the world, especially in South Asia. The game can involve various strategies and approaches to shine, and its coding can allow you to understand both Python and data structures such as circular linked lists. In this article, we will explore how to create a simple Teen Patti game using a circular linked list in Python. Let's dive into the world of gaming and programming!
Understanding Circular Linked Lists
A circular linked list is a variation of a regular linked list where all nodes are connected in a circular fashion. This means that the last node points back to the first node instead of pointing to null. This structure can be particularly advantageous for games where players take turns in a loop until a specific condition is met.
Why Use Circular Linked Lists for Teen Patti?
In Teen Patti, players take turns to play, and the circular linked list is a perfect fit for representing this cyclic gameplay. It allows us to easily traverse through players, manage gameplay sequences, and efficiently handle player actions. Using a circular linked list can also help manage the deck of cards, where the last card can link back to the first card, creating a smooth flow of play.
Setting Up the Python Environment
Before we delve into coding, ensure that you have Python installed on your system. If you haven't, you can download the latest version from the official Python website. Once you have Python set up, we can start coding our Teen Patti game.
Defining Node Class for Circular Linked List
To represent each player in our game, we first need to create a Node class that contains information about the player. Here’s how the Node class can be defined:
class Node:
def __init__(self, player_name):
self.player_name = player_name
self.next = None
In this code snippet, the Node class has two attributes: player_name
, which will hold the name of the player, and next
, which points to the next player in the game.
Creating the Circular Linked List class
Now that we have our Node defined, we can create a CircularLinkedList class to manage the players:
class CircularLinkedList:
def __init__(self):
self.head = None
def append(self, player_name):
new_node = Node(player_name)
if not self.head:
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
In this class, the append
method adds a new player to the circular list. If the list is empty (indicated by self.head
being None), the new node becomes the head and points to itself. Otherwise, it traverses to the last node and updates the next pointers accordingly.
Implementing Player Turns
In Teen Patti, players take turns. We can manage player turns using a method that iterates through the circular linked list:
def play_game(self):
current = self.head
while True:
print(f"It's {current.player_name}'s turn.")
action = input("Enter 'play' to play, 'quit' to exit: ")
if action == "quit":
print(f"{current.player_name} has exited the game.")
break
current = current.next
The play_game
method iteratively goes through each player, allowing them to take their turn. The loop continues until a player decides to quit the game.
Card Management in Teen Patti
In addition to the players, we also need to manage a deck of cards. Let's create a simple class to represent a deck:
import random
class Deck:
def __init__(self):
self.cards = [f"{rank} of {suit}" for rank in ['2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K', 'A']
for suit in ['Hearts', 'Diamonds', 'Clubs', 'Spades']]
random.shuffle(self.cards)
def draw_card(self):
return self.cards.pop() if self.cards else None
The Deck class initializes a standard set of playing cards and has a method to draw cards. This simulates the action of dealing cards to players in Teen Patti.
Integrating the Deck with Players
Let's modify the play_game method to allow players to draw cards:
def play_game(self):
current = self.head
deck = Deck() # Initialize deck
while True:
print(f"It's {current.player_name}'s turn.")
action = input("Enter 'draw' to draw a card, 'quit' to exit: ")
if action == "draw":
card = deck.draw_card()
if card:
print(f"{current.player_name} drew the {card}.")
else:
print("No more cards in the deck.")
break
elif action == "quit":
print(f"{current.player_name} has exited the game.")
break
current = current.next
This updated play_game
method allows players to draw a card during their turn. If cards are available, the drawn card is displayed to the player.
Finalizing our Teen Patti Game
Now, we can combine all the pieces into a simple game loop. Below is how the entire structure comes together:
if __name__ == "__main__":
game = CircularLinkedList()
players = ['Alice', 'Bob', 'Charlie', 'Diana']
for player in players:
game.append(player)
game.play_game()
Running this code will create a new Teen Patti game with the specified players, allowing them to take turns drawing cards from the deck until they choose to exit the game.
Extending the Game
This setup provides a foundational structure for a Teen Patti game. You can extend the game with additional features like betting, scoring, or a graphical user interface (GUI). Libraries like Tkinter or Pygame can be used for a more interactive experience, allowing you to create a fully-fledged gaming application.
Learning Outcomes
By creating a Teen Patti game using circular linked lists in Python, you deepen your understanding of both the game and the underlying data structures. This endeavor not only enhances your programming skills but also provides an entertaining way to learn!
Final Thoughts
Embarking on a project like this can be an excellent way to practice programming and logic skills while engaging with a game that many love. The concepts and techniques you learn can also be applied to a variety of other projects.