自定義文字拼圖遊戲


在本文中,我們將探討使用C++建立自定義文字拼圖遊戲的概念。文字謎題不僅有趣,而且是提高詞彙量和認知能力的好方法。我們將引導您完成使用C++設計和實現遊戲的過程,並提供一個測試用例示例來說明遊戲的工作原理。

自定義文字拼圖遊戲

文字拼圖遊戲的目標是將給定的一組字母重新排列成一個有效的單詞。玩家會得到一個亂序的單詞,他們必須重新排列字母以形成原來的單詞。為了使遊戲更具吸引力,您可以新增提示、關卡或時間限制等功能。

C++實現

示例

以下是實現上述策略的程式:

#include <stdio.h>
#include <string.h>
#include <time.h>
#include <stdlib.h>

// Function to jumble the word
void jumbleWord(char *word) {
   int length = strlen(word);
   for (int i = 0; i < length - 1; i++) {
      int j = i + rand() % (length - i);
      char temp = word[i];
      word[i] = word[j];
      word[j] = temp;
   }
}

int main() {
   srand((unsigned int)time(NULL));
   
   char originalWord[] = "programming";
   char jumbledWord[12]; // One extra for the null terminator '\0'
   strcpy(jumbledWord, originalWord);
   jumbleWord(jumbledWord);
   
   // Simulate user actions
   char firstUserGuess[] = "prominggam";
   char userHintRequest[] = "hint";
   char secondUserGuess[] = "programming";
   
   printf("Welcome to the Jumble Word Game!\n");
   printf("Unscramble the letters to form a word.\n");
   printf("Enter 'hint' for a hint.\n");
   printf("Enter 'quit' to quit the game.\n");
   printf("The jumble word is: %s\n", jumbledWord);
   
   // Simulate the first user guess
   printf("User's first guess: %s\n", firstUserGuess);
   if (strcmp(firstUserGuess, originalWord) == 0) {
      printf("Congratulations! You have successfully unscrambled the word!\n");
   } else {
      printf("Incorrect guess. Please try again!\n");
   }
   
   // Simulate user asking for a hint
   if (strcmp(userHintRequest, "hint") == 0) {
      printf("The first letter of the word is: %c\n", originalWord[0]);
   }
   
   // Simulate the second user guess 
   printf("User's second guess: %s\n", secondUserGuess);
   if (strcmp(secondUserGuess, originalWord) == 0) {
      printf("Congratulations! You have successfully unscrambled the word!\n");
   } else {
      printf("Incorrect guess. Please try again!\n");
   }
   
   return 0;
}

輸出

Welcome to the Jumble Word Game!
Unscramble the letters to form a word.
Enter 'hint' for a hint.
Enter 'quit' to quit the game.
The jumble word is: pmiarnogrmg
User's first guess: prominggam
Incorrect guess. Please try again!
The first letter of the word is: p
User's second guess: programming
Congratulations! You have successfully unscrambled the word!
#include <iostream>
#include <string>
#include <ctime>
#include <cstdlib>
#include <algorithm>

std::string jumbleWord(const std::string &word) {
   std::string jumbledWord = word;
   std::random_shuffle(jumbledWord.begin(), jumbledWord.end());
   return jumbledWord;
}

int main() {
   srand(static_cast<unsigned int>(time(0)));
   
   std::string originalWord = "programming";
   std::string jumbledWord = jumbleWord(originalWord);
   
   // Simulate user actions
   std::string firstUserGuess = "prominggam";
   std::string userHintRequest = "hint";
   std::string secondUserGuess = "programming";
   
   std::cout << "Welcome to the Jumble Word Game!" << std::endl;
   std::cout << "Unscramble the letters to form a word." << std::endl;
   std::cout << "Enter 'hint' for a hint." << std::endl;
   std::cout << "Enter 'quit' to quit the game." << std::endl;
   std::cout << "The jumble word is: " << jumbledWord << std::endl;
   
   // Simulate the first user guess
   std::cout << "User's first guess: " << firstUserGuess << std::endl;
   if (firstUserGuess == originalWord) {
      std::cout << "Congratulations! You have successfully unscrambled the word!" << std::endl;
   } else {
      std::cout << "Incorrect guess. Please try again!" << std::endl;
   }
   
   // Simulate user asking for a hint
   if (userHintRequest == "hint") {
      std::cout << "The first letter of the word is: " << originalWord[0] << std::endl;
   }
   
   // Simulate the second user guess
   std::cout << "User's second guess: " << secondUserGuess << std::endl;
   if (secondUserGuess == originalWord) {
      std::cout << "Congratulations! You have successfully unscrambled the word!" << std::endl;
   } else {
      std::cout << "Incorrect guess. Please try again!" << std::endl;
   }
   
   return 0;
}

輸出

Welcome to the Jumble Word Game!
Unscramble the letters to form a word.
Enter 'hint' for a hint.
Enter 'quit' to quit the game.
The jumble word is: pmiarnogrmg
User's first guess: prominggam
Incorrect guess. Please try again!
The first letter of the word is: p
User's second guess: programming
Congratulations! You have successfully unscrambled the word!
import java.util.Random;

public class JumbleWordGame {
   // Function to jumble the word
   public static String jumbleWord(String word) {
      char[] jumbledWord = word.toCharArray();
      Random rand = new Random();
      for (int i = 0; i < jumbledWord.length - 1; i++) {
         int j = i + rand.nextInt(jumbledWord.length - i);
         char temp = jumbledWord[i];
         jumbledWord[i] = jumbledWord[j];
         jumbledWord[j] = temp;
      }
      return new String(jumbledWord);
   }

   public static void main(String[] args) {
      Random rand = new Random();
       
      String originalWord = "programming";
      String jumbledWord = jumbleWord(originalWord);
      
      // Simulate user actions
      String firstUserGuess = "prominggam";
      String userHintRequest = "hint";
      String secondUserGuess = "programming";
      
      System.out.println("Welcome to the Jumble Word Game!");
      System.out.println("Unscramble the letters to form a word.");
      System.out.println("Enter 'hint' for a hint.");
      System.out.println("Enter 'quit' to quit the game.");
      System.out.println("The jumble word is: " + jumbledWord);
      
      // Simulate the first user guess
      System.out.println("User's first guess: " + firstUserGuess);
      if (firstUserGuess.equals(originalWord)) {
         System.out.println("Congratulations! You have successfully unscrambled the word!");
      } else {
         System.out.println("Incorrect guess. Please try again!");
      }
       
      // Simulate user asking for a hint
      if (userHintRequest.equals("hint")) {
         System.out.println("The first letter of the word is: " + originalWord.charAt(0));
      }
       
      // Simulate the second user guess 
      System.out.println("User's second guess: " + secondUserGuess);
      if (secondUserGuess.equals(originalWord)) {
         System.out.println("Congratulations! You have successfully unscrambled the word!");
      } else {
         System.out.println("Incorrect guess. Please try again!");
      }
   }
}

輸出

Welcome to the Jumble Word Game!
Unscramble the letters to form a word.
Enter 'hint' for a hint.
Enter 'quit' to quit the game.
The jumble word is: pmiarnogrmg
User's first guess: prominggam
Incorrect guess. Please try again!
The first letter of the word is: p
User's second guess: programming
Congratulations! You have successfully unscrambled the word!
import random
# Function to jumble the word
def jumble_word(word):
   jumbled_word = list(word)
   random.shuffle(jumbled_word)
   return ''.join(jumbled_word)

def main():
   random.seed()
   
   original_word = "programming"
   jumbled_word = jumble_word(original_word)
   
   # Simulate user actions
   first_user_guess = "prominggam"
   user_hint_request = "hint"
   second_user_guess = "programming"
   
   print("Welcome to the Jumble Word Game!")
   print("Unscramble the letters to form a word.")
   print("Enter 'hint' for a hint.")
   print("Enter 'quit' to quit the game.")
   print(f"The jumble word is: {jumbled_word}")
   
   # Simulate the first user guess
   print(f"User's first guess: {first_user_guess}")
   if first_user_guess == original_word:
      print("Congratulations! You have successfully unscrambled the word!")
   else:
      print("Incorrect guess. Please try again!")
   
   # Simulate user asking for a hint
   if user_hint_request == "hint":
      print(f"The first letter of the word is: {original_word[0]}")
   
   # Simulate the second user guess 
   print(f"User's second guess: {second_user_guess}")
   if second_user_guess == original_word:
      print("Congratulations! You have successfully unscrambled the word!")
   else:
      print("Incorrect guess. Please try again!")

if __name__ == "__main__":
   main()

輸出

Welcome to the Jumble Word Game!
Unscramble the letters to form a word.
Enter 'hint' for a hint.
Enter 'quit' to quit the game.
The jumble word is: pmiarnogrmg
User's first guess: prominggam
Incorrect guess. Please try again!
The first letter of the word is: p
User's second guess: programming
Congratulations! You have successfully unscrambled the word!

以下是上面實現的自定義文字拼圖遊戲的示例測試用例。

假設原始單詞是“programming”。程式將生成該單詞的亂序版本,例如:“mgimnaprorg”。使用者的任務是解開亂序的單詞並找到原始單詞。

在這個測試用例中:

  • 程式顯示亂序的單詞:mgimnaprorg。

  • 使用者輸入他們的第一次猜測:“prominggam”(錯誤的猜測)。

  • 使用者請求提示:程式顯示該單詞的第一個字母是“p”。

  • 使用者輸入另一個猜測:“programming”(正確的猜測)。

  • 程式祝賀使用者成功解開單詞並退出。

此示例測試用例演示了自定義文字拼圖遊戲的工作原理以及使用者如何透過猜測、請求提示或退出遊戲來與遊戲進行互動。

結論

在本文中,我們透過使用C++建立一個自定義文字拼圖遊戲來探索文字謎題的精彩世界。我們討論了遊戲的目標、C++實現,並提供了一個測試用例示例來說明遊戲的功能。透過實現此遊戲,您不僅可以提高程式設計技能,還可以參與一項有趣且具有教育意義的活動。您可以透過新增更多功能(例如關卡、類別或時間限制)來進一步改進此遊戲,使其對玩家更具挑戰性和趣味性。祝您程式設計和遊戲愉快!

更新於:2023年10月16日

瀏覽量:398

開啟您的職業生涯

透過完成課程獲得認證

開始學習
廣告