用 Python 實現 FLAMES 遊戲



FLAMES 遊戲是一款有趣且流行的遊戲,它使用簡單的演算法來確定兩個名字之間的關係。FLAMES 是以下可能關係的首字母縮寫:友情 (Friendship)、愛情 (Love)、好感 (Affection)、婚姻 (Marriage)、敵意 (Enemy) 和兄弟姐妹 (Sibling)。本文將演示如何使用 Python 實現 FLAMES 遊戲,並提供對所用概念的解釋以及程式碼示例及其輸出。

使用的概念

  • 字串操作 - 從兩個字串中提取和比較字元。
  • 資料結構 - 使用列表和集合來管理和比較字元。
  • 迴圈和條件語句 - 遍歷字元並應用條件來確定結果。

方法 1:基本實現

步驟 1:基本設定

我們將首先編寫一個 Python 函式,該函式根據兩個輸入名稱計算 FLAMES 結果。該方法包括 -

  • 刪除空格並將名稱轉換為小寫。
  • 計算兩個名稱中每個字元的頻率。
  • 計算取消後剩餘的字元。
  • 使用計數來確定 FLAMES 結果。

示例

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")) 

輸出

The relationship is: Affection

解釋

  • 字串操作 - 程式刪除空格並將兩個名稱轉換為小寫以保持一致性。
  • 刪除公共字元 - 刪除兩個名稱之間的公共字元。
  • 計算剩餘字元 - 計算剩餘字元的總數。
  • 確定 FLAMES 結果 - 使用計數迴圈遍歷“FLAMES”中的字母以確定關係。

方法 2:帶有輸入驗證的高階實現

在這種方法中,我們透過新增輸入驗證來增強基本實現,以確保名稱是字母數字並且非空。此外,程式允許多輪遊戲。

示例

以下是高階實現程式碼 -

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() 

輸出

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

解釋

  • 輸入驗證 - 程式確保兩個名稱僅包含字母字元並且非空。
  • 增強的使用者互動 - 程式允許使用者透過在每一輪之後詢問他們是否要再次玩來玩多輪遊戲。

結論

FLAMES 遊戲是練習 Python 程式設計技能(包括字串操作、迴圈和條件語句)的一種引人入勝的方式。本文探討了 FLAMES 遊戲的基本實現和高階實現,為進一步增強提供了堅實的基礎,例如新增更復雜的關係結果或整合圖形使用者介面 (GUI)。

python_projects_from_basic_to_advanced.htm
廣告