用c++写一个包含StL容器的游戏
时间: 2023-11-26 21:03:49 浏览: 60
好的,我可以给您提供一个简单的示例,这个游戏是一个猜数字游戏,玩家需要猜出随机生成的数字。
首先,我们需要包含必要的头文件:
```c++
#include <iostream>
#include <vector>
#include <algorithm>
#include <ctime>
#include <cstdlib>
```
然后,我们需要定义一个函数来生成随机数字。在这个函数中,我们使用了 `srand` 函数来设置随机数生成器的种子,以便每次运行程序时都能生成不同的随机数。
```c++
int generateRandomNumber(int min, int max) {
srand(time(NULL));
return (rand() % (max - min + 1)) + min;
}
```
接下来,我们需要定义一个函数来处理玩家的猜测并返回结果。在这个函数中,我们使用了 `std::vector` 来存储之前的猜测,并使用 `std::find` 函数来检查玩家是否已经猜过这个数字。如果玩家猜对了,函数将返回一个 `true` 值,否则返回一个 `false` 值。
```c++
bool handleGuess(int guess, int number, std::vector<int>& guesses) {
if (std::find(guesses.begin(), guesses.end(), guess) != guesses.end()) {
std::cout << "You've already guessed that number. Try again.\n";
return false;
}
guesses.push_back(guess);
if (guess == number) {
std::cout << "Congratulations! You guessed the number.\n";
return true;
}
else if (guess < number) {
std::cout << "Your guess is too low. Guess again.\n";
return false;
}
else {
std::cout << "Your guess is too high. Guess again.\n";
return false;
}
}
```
最后,我们需要定义 `main` 函数来运行游戏。在 `main` 函数中,我们首先生成一个随机数字,然后使用一个循环来让玩家猜测数字,直到玩家猜中为止。
```c++
int main() {
int min = 1;
int max = 100;
int number = generateRandomNumber(min, max);
std::vector<int> guesses;
std::cout << "Welcome to the Number Guessing Game!\n";
while (true) {
std::cout << "Guess a number between " << min << " and " << max << ": ";
int guess;
std::cin >> guess;
if (handleGuess(guess, number, guesses)) {
break;
}
}
return 0;
}
```
完整代码如下:
```c++
#include <iostream>
#include <vector>
#include <algorithm>
#include <ctime>
#include <cstdlib>
int generateRandomNumber(int min, int max) {
srand(time(NULL));
return (rand() % (max - min + 1)) + min;
}
bool handleGuess(int guess, int number, std::vector<int>& guesses) {
if (std::find(guesses.begin(), guesses.end(), guess) != guesses.end()) {
std::cout << "You've already guessed that number. Try again.\n";
return false;
}
guesses.push_back(guess);
if (guess == number) {
std::cout << "Congratulations! You guessed the number.\n";
return true;
}
else if (guess < number) {
std::cout << "Your guess is too low. Guess again.\n";
return false;
}
else {
std::cout << "Your guess is too high. Guess again.\n";
return false;
}
}
int main() {
int min = 1;
int max = 100;
int number = generateRandomNumber(min, max);
std::vector<int> guesses;
std::cout << "Welcome to the Number Guessing Game!\n";
while (true) {
std::cout << "Guess a number between " << min << " and " << max << ": ";
int guess;
std::cin >> guess;
if (handleGuess(guess, number, guesses)) {
break;
}
}
return 0;
}
```
相关推荐
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)