?#include <stdio.h> #include <stdlib.h> #include <time.h> #include <string.h> // 生成隨機文字 char* generateRandomWord() { // 定義一些可能的文字 char* words[] = {"a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"}; int randomIndex = rand() % (sizeof(words) / sizeof(words[0])); return words[randomIndex]; } // 檢查用戶輸入是否正確 int checkInput(char* userInput, char* targetWord) { return strcmp(userInput, targetWord) == 0; } int main() { srand(time(NULL)); // 初始化隨機數(shù)種子 char* targetWord = generateRandomWord(); int score = 0; int timeLeft = 60; // 假設(shè)游戲時間為 60 秒 printf("輸入以下文字:%s\n", targetWord); char userInput[20]; while (timeLeft > 0) { printf("剩余時間:%d 秒\n", timeLeft); scanf("%s", userInput); if (checkInput(userInput, targetWord)) { score++; targetWord = generateRandomWord(); printf("正確!新的文字:%s\n", targetWord); } else { printf("錯誤,請重新輸入\n"); } timeLeft--; } printf("游戲結(jié)束,您的得分是:%d\n", score); return 0; } |
|