using c++ Create a basic adventure game using functions. The main function shoul
ID: 3914491 • Letter: U
Question
using c++
Create a basic adventure game using functions.
The main function should contain only calls to other functions.
When the program starts, ask the user if they want to create a game, or play the game.
For creating the game, prompt the user for a direction/command and the description of what happens when the user does that. Create a file with the name of the direction/command and write the description to the file.
Continue allowing the user to create direction/commands until they desire to exit.
For playing the game:
Prompts the user for a direction/command, look and see if there is a file with that direction/command and print the contents, or print "you can't do that"
Loop forever until they enter some secret word
Explanation / Answer
#include <iostream>
#include <fstream>
#include<string>
using namespace std;
string arr[100];
int count=0;
void command(){
string current,desc;
int more;
do{ cout<<"enter command ";
cin>>current;
arr[count++]=current;
ofstream myfile;
myfile.open (current.c_str());
cout<<"enter description ";
getline(cin, desc);
myfile << desc;
myfile.close();
cout<<"want to enter more commands press 1 else press 0 ";
cin>>more;
}while(more==1);
}
void play_game(){
int more,i;
string command;
while(1){ i=0;
cout<<"enter command ";
cin>>command;
while(i<count){
if(arr[i]==command){
cout<<"you can't do so ";
break;
}
i++;
}
if(i==count){
cout<<"you found secret command ";
break;
}
}
}
int main()
{
int choice,more;
do{
cout<<"1:enter command 2:play game ";
cin>>choice;
switch(choice){
case 1:{
command();
break;
}
case 2:{
play_game();
break;
}
}
cout<<"for more operation press 1 else press 0 ";
cin>>more;
}while(more==1);
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.