Implement FLAMES Game in Python



The FLAMES game is a fun and popular game that determines the relationship between two names using a simple algorithm. The name FLAMES is an acronym for the possible relationships: Friendship, Love, Affection, Marriage, Enemy, and Sibling. This article will demonstrate how to implement the FLAMES game using Python, providing explanations of the concepts used along with code examples and their outputs.

Concepts Used

  • String Manipulation − Extracting and comparing characters from two strings.
  • Data Structures − Using lists and sets to manage and compare characters.
  • Looping and Conditional Statements − Iterating through characters and applying conditions to determine the outcome.

Approach 1: Basic Implementation

Step 1: Basic Setup

We'll start by writing a Python function that calculates the FLAMES result based on two input names. The approach involves −

  • Removing spaces and converting names to lowercase.
  • Counting the frequency of each character in both names.
  • Calculating the remaining characters after cancellation.
  • Using the count to determine the FLAMES result.

Example

def flames_game_basic(name1, name2): 
   # Remove spaces and convert to lowercase 
   name1 = name1.replace(" ", "").lower() 
   name2 = name2.replace(" ", "").lower() 

   # Remove common characters 
   for char in name1[:]: 
      if char in name2: 
         name1 = name1.replace(char, "", 1) 
         name2 = name2.replace(char, "", 1) 

   # Count the remaining characters 
   count = len(name1 + name2) 

   # FLAMES outcome based on the count 
   flames = ["Friendship", "Love", "Affection", "Marriage", "Enemy", "Sibling"] 
   while len(flames) > 1: 
      split_index = (count % len(flames)) - 1 
      if split_index >= 0: 
         flames = flames[split_index + 1:] + flames[:split_index] 
      else: 
         flames = flames[:len(flames) - 1] 

   return f"The relationship is: {flames[0]}" 

# Test the basic FLAMES game 
print(flames_game_basic("Alice", "Bob")) 

Output

The relationship is: Affection

Explaination

  • String Manipulation − The program removes spaces and converts both names to lowercase for uniformity.
  • Removing Common Characters − The common characters between the two names are removed.
  • Counting Remaining Characters − The total count of the remaining characters is calculated.
  • Determining FLAMES Outcome − The count is used to cycle through the letters in "FLAMES" to determine the relationship.

Approach 2: Advanced Implementation with Input Validation

In this approach, we enhance the basic implementation by adding input validation to ensure the names are alphabetic and non-empty. Additionally, the program allows multiple rounds of play.

Example

Here's the advanced implementation code −

def flames_game_advanced(): 
   while True: 
      name1 = input("Enter the first name: ").strip() 
      name2 = input("Enter the second name: ").strip() 

      # Validate inputs 
      if not name1.isalpha() or not name2.isalpha(): 
         print("Invalid input! Names should only contain alphabets and should not be empty.") 
         continue 

      # Remove common characters 
      for char in name1[:]: 
         if char in name2: 
            name1 = name1.replace(char, "", 1) 
            name2 = name2.replace(char, "", 1) 

      # Count the remaining characters 
      count = len(name1 + name2) 

      # FLAMES outcome based on the count 
      flames = ["Friendship", "Love", "Affection", "Marriage", "Enemy", "Sibling"] 
      while len(flames) > 1: 
         split_index = (count % len(flames)) - 1 
         if split_index >= 0: 
            flames = flames[split_index + 1:] + flames[:split_index] 
         else: 
            flames = flames[:len(flames) - 1] 

      print(f"The relationship is: {flames[0]}") 

      # Ask if the player wants to play again 
      another = input("Do you want to play again? (yes/no): ").strip().lower() 
      if another != 'yes': 
         break 

# Run the advanced FLAMES game 
flames_game_advanced() 

Output

Enter the first name: Alice 
Enter the second name: Bob 
The relationship is: Marriage 
  
Do you want to play again? (yes/no): no 

Explaination

  • Input Validation − The program ensures that both names contain only alphabetic characters and are non-empty.
  • Enhanced User Interaction − The program allows users to play multiple rounds by asking if they want to play again after each round.

Conclusion

The FLAMES game is an engaging way to practice Python programming skills, including string manipulation, loops, and conditionals. This article explored both a basic and an advanced implementation of the FLAMES game, offering a solid foundation for further enhancements, such as adding more complex relationship outcomes or integrating a graphical user interface (GUI).

python_projects_from_basic_to_advanced.htm
Advertisements