写一个文字游戏的C++代码
时间: 2023-12-01 19:58:40 浏览: 123
以下是一个简单的文字游戏的C++代码:
```cpp
#include <iostream>
#include <string>
using namespace std;
int main() {
int choice;
string name;
cout << "Welcome to the game!" << endl;
cout << "What is your name? ";
cin >> name;
cout << "Hello, " << name << "!" << endl;
cout << "Choose your adventure:" << endl;
cout << "1. Go on a treasure hunt" << endl;
cout << "2. Explore a haunted house" << endl;
cout << "3. Solve a mystery" << endl;
cout << "Enter your choice: ";
cin >> choice;
switch (choice) {
case 1:
cout << "You have chosen to go on a treasure hunt!" << endl;
// Add treasure hunt game logic here
break;
case 2:
cout << "You have chosen to explore a haunted house!" << endl;
// Add haunted house game logic here
break;
case 3:
cout << "You have chosen to solve a mystery!" << endl;
// Add mystery game logic here
break;
default:
cout << "Invalid choice. Please try again." << endl;
return 0;
}
return 0;
}
```
这个游戏很简单,只是提供了三种不同的选择,每个选择会导致不同的游戏逻辑。你可以根据自己的想法将游戏逻辑添加到相应的case语句中。
阅读全文