In: Computer Science
Write a short text based game using C++ were you can chose the gender of the character, than choose between two location, desert or forest. If either location is chosen than you can choose to either stay at your spot or travel to find help. If player chose desert, than choosing either to stay or travel can be led to death by heat exhaustion or saved randomized, or if forest is chosen, than it can be either death of starvation or saved also randomized. Winner lives, loser dies.
I tried to create a game that fulfills all the requirements written above and make the game as much interactive. below is the code of the game. If you have any problem let me know in a comment.
Code:-
//including header files
#include <iostream>
using namespace std;
//Main function
int main()
{
//variable to keep track weather you want to play game again or not
char game='1';
do
{
//Welcome message
cout<<"Welcome to Game 'Winner lives Loser dies'\n";
//Select Gender
string Gender;
cout<<"Select Gender of Player:- \n1.Male\n2.Female\n";
//loop so that value enterd is valid or not
while(1)
{
cout<<"Enter Gender : ";
cin>>Gender;
if(Gender=="1")
{
cout<<"Male Gender is selected\n";
Gender="Male";
break;
}
else if(Gender=="2")
{
cout<<"Female Gender is selected\n";
Gender="Female";
break;
}
else
{
cout<<"Invalid Entry! Try again\n";
}
}
//location selction
string location;
cout<<"\nSelect location where you want to go :- \n1.Desert\n2.Forest\n";
//to check weather value enterd valid or not
while(1)
{
cout<<"Enter location : ";
cin>>location;
if(location=="1")
{
cout<<"Desert is selected\n";
location="Desert";
break;
}
else if(location=="2")
{
cout<<"Forest is selected\n";
location="Forest";
break;
}
else
{
cout<<"Invalid Entry! Try again\n";
}
}
//Decision to stay or travel
string decision;
cout<<"\nSelect either to stay or travel :- \n1.Stay\n2.Travel\n";
//to check enterd value is valid or not
while(1)
{
cout<<"Enter your decision : ";
cin>>decision;
if(decision=="1")
{
cout<<"Player selected to stay\n";
decision="Stay";
break;
}
else if(decision=="2")
{
cout<<"Player selected to travel\n";
decision="Travel";
break;
}
else
{
cout<<"Invalid Entry! Try again\n";
}
}
//to seed random value with current time
srand(time(NULL));
//finding random value o or 1
int random=rand()%2;
cout<<"\n";
//showing result according to random value
if(random==1)
{
cout<<Gender<<" Player is dead while "<<decision<<"ing in the "<<location;
if(location=="Desert")
{
cout<<" due to heat exhaustion";
}
else
{
cout<<" due to starvation";
}
cout<<"\nSorry! you Lose the game \n";
}
else
{
cout<<Gender<<" Player is able to survive while "<<decision<<"ing in the "<<location;
cout<<"\nYou won the game\n";
}
//asking weather to play again or not
cout<<"\nWant to play again? \nEnter 1 to play or any key for no\n";
cout<<"Enter decision : ";
cin>>game;
cout<<"\n\n";
}
while(game=='1');
return 0;
}
Output:-
Welcome to Game 'Winner lives Loser dies' Select Gender of Player:- 1.Male 2.Female Enter Gender : 1 Male Gender is selected Select location where you want to go :- 1.Desert 2.Forest Enter location : 1 Desert is selected Select either to stay or travel :- 1.Stay 2. Travel Enter your decision : 1 Player selected to stay Male Player is able to survive while staying in the Desert You won the game Want to play again? Enter 1 to play or any key for no Enter decision : 0
Welcome to Game 'Winner lives Loser dies' Select Gender of Player:- 1.Male 2.Female Enter Gender : 1 Male Gender is selected Select location where you want to go :- 1.Desert 2.Forest Enter location : 1 Desert is selected Select either to stay or travel :- 1.Stay 2. Travel Enter your decision : 1 Player selected to stay Male Player is able to survive while staying in the Desert You won the game Want to play again? Enter 1 to play or any key for no Enter decision : 0