Python實現單詞亂序遊戲程式


準備好測試你的詞彙能力了嗎?單詞亂序遊戲將挑戰你打亂字母並組成有意義單詞的能力。準備好打亂和解開單詞,沉浸在這個引人入勝且益智的娛樂中吧。單詞亂序遊戲是一種有趣的方式,可以挑戰你的詞彙能力並提高你的解決問題的能力。

在這篇文章中,我們將學習如何使用Python程式語言實現一個單詞亂序遊戲。我們將探討三種不同的方法來解決這個遊戲,每種方法都有其自身的演算法和程式碼實現。

方法一:隨機排列字母

第一種方法包括隨機打亂給定單詞的字母,並將其顯示給玩家。然後,玩家必須重新排列字母以組成正確的單詞。讓我們深入探討實現這種方法的分步過程。

演算法

  • 步驟1 − 開始時定義遊戲中要使用的單詞列表。

  • 步驟2 − 從列表中隨機選擇一個單詞。

  • 步驟3 − 隨機打亂所選單詞的字母。

  • 步驟4 − 將打亂的單詞顯示給玩家。

  • 步驟5 − 讀取玩家的輸入。

  • 步驟6 − 檢查輸入是否與原始單詞匹配。

  • 步驟7 − 給玩家關於他們答案的反饋。

  • 步驟8 − 重複此過程,直到玩家需要退出遊戲。

示例

import random
words = ["Papaya ", "banana", "orange", "grape", " water melon"]

while True:
    
   word = random.choice(words)

   jumbled_word = list(word)
   random.shuffle(jumbled_word)
    
   jumbled_word = ''.join(jumbled_word)
    
   print("Jumbled input :", jumbled_word)
    
   guess = input("Player guess: ")
    
   if guess == word:
      print("Correct!")
   else:
      print("Incorrect!")

   play_again = input("Want to play again? (yes/no): ")
   if play_again.lower() != "yes":
      break

輸出

umble input : noager
Player guess: orange
Correct!
Do you want to play again? (yes/no): yes
Jumbled Input: pnalep
Player guess: apple
Correct!
Do you want to play again? (yes/no): no

方法二:建立變位詞

第二種方法包括建立給定單詞的所有可能的變位詞,並將它們顯示給玩家。然後,玩家應該從重新排列的單詞列表中選擇正確的單詞。讓我們來看看這種方法的演算法和實現。

演算法

  • 步驟1 − 開始時定義遊戲中要使用的單詞列表。

  • 步驟2 − 從列表中隨機選擇一個單詞。

  • 步驟3 − 生成所選單詞的所有可能的變位詞。

  • 步驟4 − 將變位詞顯示給玩家。

  • 步驟5 − 讀取玩家的輸入。

  • 步驟6 − 檢查輸入是否與原始單詞匹配。

  • 步驟7 − 給玩家關於他們答案的反饋。

  • 步驟8 − 重複此方法,直到玩家需要退出遊戲。

示例

import random
from itertools import permutations

words = ["apple", "banana", "orange", "grape", "melon"]

while True:
    
   word = random.choice(words)

   anagrams = [''.join(perm) for perm in permutations(word)]

   print("Anagrams:")
   for index, anagram in enumerate(anagrams, start=1):
      print(index, ".", anagram)
    
   guess = int(input("number corresponding to your guess: ")) - 1
    
   if anagrams[guess] == word:
      print("Correct!")
   else:
      print("Incorrect!")

   play_again = input("Do you want to play again? (yes/no): ")

   if play_again.lower() != "yes":
      break

輸出

Anagrams:
1 . grape
2 . graep
3 . grpae
4 . grpea
5 . greap
6 . grepa
7 . garpe
8 . garep
9 . gapre
10 . gaper
11 . gaerp
12 . gaepr
13 . gprae
14 . gprea
15 . gpare
16 . gpaer
17 . gpera
18 . gpear
19 . gerap

方法三:單詞亂序線索

第三種方法包括向玩家顯示一個線索,該線索包含打亂的字母和單詞的定義。玩家應該根據給定的線索解開字母以組成正確的單詞。讓我們來看看這種方法的演算法和實現。

演算法

  • 步驟1 − 開始時定義單詞列表及其對應的線索。

  • 步驟2 − 然後從列表中選擇一個單詞及其線索。

  • 步驟3 − 將線索和打亂的字母顯示給玩家。

  • 步驟4 − 讀取玩家的輸入。

  • 步驟5 − 檢查輸入是否與原始單詞匹配。

  • 步驟6 − 給玩家關於他們答案的反饋。

  • 步驟7 − 重複此過程,直到玩家需要退出遊戲。

示例

import random

word_clues = {
   "apple": " red grows on trees",
   "banana": " curved fruit with a yellow skin",
   "orange": " orange skin",
   "grape": " round fruit that grows in clusters.",
   "melon": " fruit with juicy, sweet flesh."
}

while True:
    
   word, clue = random.choice(list(word_clues.items()))
 
   jumbled_word = list(word)
   random.shuffle(jumbled_word)
 
   jumbled_word = ''.join(jumbled_word)

   print("Clue:", clue)
   print("Jumbled Input:", jumbled_word)

   guess = input("Player  guess: ")

   if guess == word:
      print("Correct!")
   else:
      print("Incorrect!")
    
   play_again = input("Do you want to play again? (yes/no): ")

   if play_again.lower() != "yes":
      break

輸出

Clue: red and grows on trees.
Jumbled Input: plape
Player guess: apple
Correct!
Do you want to play again? (yes/no): yes
Clue: orange skin
Jumbled Input : oernag
Player guess: orange
Correct!
Do you want to play again? (yes/no): no 

結論

在這篇文章中,我們使用Python中的三種不同方法實現了一個單詞亂序遊戲。每種方法都提供了一種獨特的方式來挑戰玩家的詞彙能力和解決問題的能力。透過隨機排列字母、建立變位詞或提供單詞亂序線索,玩家可以在提高知識的同時獲得令人愉悅和有趣的體驗。

更新於:2023年8月29日

瀏覽量:693

開啟你的職業生涯

完成課程獲得認證

開始學習
廣告