写c++程序:1 猜数字:由计算机随机产生一个1~10000以内的数字用户根据提示猜出该数字功能要求如下:1游戏结束条件:a在规定时间内正确猜出该数字;b规定用时耗完。2游戏提示:根据用户的输入与所产生数字的大小关系给出正确提示例如随机产生数为5000若用户输入6000则提示高了若用户输入4000则提示低了。3游戏结果显示:1若用户在规定时间内正确猜出数字则显示恭喜!正确猜出数字。;2若到达规定时
#include
using namespace std;
int main() { int min = 1; int max = 10000; int target; int guess; int timeLimit; char playAgain;
srand(time(0)); // seed the random number generator
do {
// Setting game time
cout << "Enter the time limit for the game (in seconds): ";
cin >> timeLimit;
// Generating random target number
target = rand() % (max - min + 1) + min;
// Start game timer
time_t startTime = time(0);
time_t currentTime;
int elapsedTime;
cout << "Guess the number between " << min << " and " << max << endl;
// Game loop
do {
cout << "Enter your guess: ";
cin >> guess;
if (guess < target) {
cout << "Too low! Try again." << endl;
} else if (guess > target) {
cout << "Too high! Try again." << endl;
} else {
cout << "Congratulations! You guessed the correct number " << target << "." << endl;
break;
}
// Check time limit
currentTime = time(0);
elapsedTime = currentTime - startTime;
} while (elapsedTime < timeLimit);
// Game over
if (elapsedTime >= timeLimit) {
cout << "Sorry, you ran out of time. The correct number was " << target << "." << endl;
}
// Prompt for replay
cout << "Do you want to play again? (Y/N): ";
cin >> playAgain;
} while (playAgain == 'Y' || playAgain == 'y');
return 0;
原文地址: https://www.cveoy.top/t/topic/iytM 著作权归作者所有。请勿转载和采集!