|
| 1 | +#include <iostream> |
| 2 | +#include <string> |
| 3 | +using namespace std; |
| 4 | +void display() { |
| 5 | + cout << "Welcome to the Text Adventure Game!" << endl; |
| 6 | + cout << "You find yourself in a mysterious land with several rooms." << endl; |
| 7 | + cout << "Your goal is to find the hidden treasure and escape." << endl; |
| 8 | + cout << "Good luck!" << endl << endl; |
| 9 | +} |
| 10 | +int getplayer() { |
| 11 | + int c; |
| 12 | + cout << "Enter your choice: "; |
| 13 | + cin >> c; |
| 14 | + return c; |
| 15 | +} |
| 16 | +void room1(); |
| 17 | +void room2(); |
| 18 | +void room3(); |
| 19 | +void room1() { |
| 20 | + cout << "You are in Room 1. There are doors to Room 2 and Room 3." << endl; |
| 21 | + cout << "1. Go to Room 2" << endl; |
| 22 | + cout << "2. Go to Room 3" << endl; |
| 23 | + int choice = getplayer(); |
| 24 | + if (choice == 1) { |
| 25 | + room2(); |
| 26 | + } else if (choice == 2) { |
| 27 | + room3(); |
| 28 | + } else { |
| 29 | + cout << "Invalid choice. Try again." << endl; |
| 30 | + room1(); |
| 31 | + } |
| 32 | +} |
| 33 | +void room2() { |
| 34 | + cout << "You are in Room 2. There are doors to Room 1 and Room 3." << endl; |
| 35 | + cout << "1. Go to Room 1" << endl; |
| 36 | + cout << "2. Go to Room 3" << endl; |
| 37 | + int choice = getplayer(); |
| 38 | + if (choice == 1) { |
| 39 | + room1(); |
| 40 | + } else if (choice == 2) { |
| 41 | + room3(); |
| 42 | + } else { |
| 43 | + cout << "Invalid choice. Try again." << endl; |
| 44 | + room2(); |
| 45 | + } |
| 46 | +} |
| 47 | +void room3() { |
| 48 | + cout << "You are in Room 3. There is a door to Room 1 and a hidden treasure here!" << endl; |
| 49 | + cout << "1. Go to Room 1" << endl; |
| 50 | + cout << "2. Take the treasure and win the game!" << endl; |
| 51 | + int choice = getplayer(); |
| 52 | + if (choice == 1) { |
| 53 | + room1(); |
| 54 | + } else if (choice == 2) { |
| 55 | + cout << "Congratulations! You found the hidden treasure and won the game!" << endl; |
| 56 | + } else { |
| 57 | + cout << "Invalid choice. Try again." << endl; |
| 58 | + room3(); |
| 59 | + } |
| 60 | +} |
| 61 | +int main() { |
| 62 | + display(); |
| 63 | + int m; |
| 64 | + cout<<"Enter the room number(1,2,3):"<<endl; |
| 65 | + cin>>m; |
| 66 | + if(m==1){ |
| 67 | + room1(); |
| 68 | + } |
| 69 | + else if(m==2){ |
| 70 | + room2(); |
| 71 | + } |
| 72 | + else if(m==3){ |
| 73 | + room3(); |
| 74 | + } |
| 75 | + else{ |
| 76 | + cout<<"Invalid !"<<endl; |
| 77 | + } |
| 78 | + return 0; |
| 79 | +} |
0 commit comments