Создать функцию для выбора - PullRequest
0 голосов
/ 16 марта 2020
#include<iostream>
#include<string>
#include <unistd.h>
    using namespace std;
    string livingroom();
    string diningroom();
    string bathroom();
    string stair();
    int roomchoice();
    int room_counts = 0;
    bool key_spade;
        string basicinfo(){
            string name ;
            getline(cin,name);
            cout<<"Your name is "<<name<<endl;
            cout<<"Hello"<<endl;
        }
    int roomchoice(){
        int room_choices;
            cin>>room_choices;
                if(room_choices==1){
                    cout<<"You are now in the living room"<<endl;
                    livingroom();
                }
                else if(room_choices==2){
                    cout<<"You are now in the bathroom"<<endl;
                    bathroom();
                }
                else if (room_choices==3){
                    cout<<"You are now in the dining room"<<endl;
                    diningroom();
                }
                else if (room_choices==4){
                    cout<<"You are now at the stair"<<endl;
                    stair();
                }
                else {
                    cout<<"Invalid number "<<endl;
                }
        string livingroom();
                cout<<"it's actually a pretty living room"<<endl;
                cout<<"if you have money your future living room will look like this but not cover in dust"<<endl;
                sleep(2);
                cout<<"In the living room there are a few pictures"<<endl;
                cout<<"Look like it belong to the previous family"<<endl;
                sleep(2);
                cout<<"Behind the picture there are a number"<<endl;
                cout<<"4672"<<endl;
                sleep(2);
                cout<<"Beside the number nothing else "<<endl;
                cout<<"You keep looking "<<endl;
                cout<<"Beside a bookcase and a tv nothing much"<<endl;
                cout<<"You should check others rooms"<<endl;
                cout<<"Which room do you choose "<<endl;
                cout<<"1.Livingroom"<<endl;
                cout<<"2.Bathroom"<<endl;
                cout<<"3.Dining room"<<endl;
                cout<<"4.The stair"<<endl;
                room_counts=room_counts+1;
                roomchoice();
            }
    string bathroom(){
                cout<<"This is so creepy "<<endl;
                sleep(2);
                cout<<"And then you see a huge puddle of blood "<<endl;
                sleep(2);
                cout<<"Looking up you see a corpse hanging up from the celling"<<endl;
                cout<<"You immediately call the police "<<endl;
                cout<<"The phone ringing"<<endl;
                sleep(4);
                cout<<"NO SIGNAL"<<endl;
                cout<<"NO . Damm it . NO signal "<<endl;
                cout<<"You leave the bathroom"<<endl;
                cout<<"You should go to others room"<<endl;
                room_counts=room_counts+1;
                cout<<"1.Livingroom"<<endl;
                cout<<"2.Bathroom"<<endl;
                cout<<"3.Dining room"<<endl;
                cout<<"4.The stair"<<endl;
                roomchoice();
            }
    string diningroom(){

                cout<<"You go to the dining room"<<endl;
                cout<<"It's a lovely room"<<endl;
                cout<<"A small kitchen with a small dining table for 4 people "<<endl;
                cout<<"Nice kitchen "<<" . "<<"You said"<<endl;
                cout<<"While looking around the kitchen . You see a key hidden under a carpet"<<endl;
                cout<<"Will you pick up"<<endl;
                cout<<"Yes or No"<<endl;
            string dineChoice ;
                cin>>dineChoice;
                if(dineChoice=="Yes"|| dineChoice=="YES"){
                    key_spade = true;
                    cout<<"You pick up the key"<<endl;
                }
                else {
                    key_spade=false;
                    cout<<"You put the key back "<<endl;
                }
            cout<<"Nothing much ."<<endl;
            sleep(2);
            cout<<"Maybe I should check others room"<<endl;
            room_counts=room_counts+1;
            roomchoice();
    }

        string decision1(){
            string choices1;
            cin>>choices1;
            if(choices1=="Yes"){
                cout<<"You decide to follow the footprints"<<endl;
            }
            else if(choices1=="No"){
                cout<<"You decide to head home "<<endl;
                cout<<"Nothing happens"<<endl;
                cout<<"Game over"<<endl;
                exit(0);
            }
            else {
                cout<<"Invalid choice"<<endl;
            }
        }
        string decision2(){
            string choices2;
            cin>>choices2;
            if(choices2=="Yes"){
                cout<<"You decide to go in "<<endl;
            }
            else if(choices2=="No"){
                cout<<"Are you sure"<<endl;
            string subchoices;
            int  subchoices2;
            cin>>subchoices;
            if(subchoices=="No"){
                cout<<"Well so what do you choose"<<endl;
                cout<<"1.Go in"<<endl;
                cout<<"2.Go back"<<endl;
                cout<<"Your choice is :"<<endl;
                cin>>subchoices2;
                if(subchoices2==1){
                    cout<<"You decide to investsigate the house"<<endl;
                }
                else if(subchoices2==2){
                    cout<<"You go back home"<<endl;
                    exit(0);
                }
            }
        }
            }
        string decision3(){
                    int choices3;
                    cin>>choices3;
                    if(choices3==1){
                        livingroom();
                    }
                    else if(choices3==2){
                        bathroom();
                    }
                    else if (choices3==3){
                        diningroom();
                    }

                }





        int main(){
            basicinfo();
            cout<<"You wake up in a beautiful morning"<<endl;
            cout<<"What a beautiful morning . You said"<<endl;
            cout<<"What should i do now ?"<<endl;
            cout<<"A leisurely stroll will be a great idea"<<endl;
            cout<<"You go the the forest north of the town"<<endl;
            cout<<"After a while you suddenly a few strange footprints"<<endl;
            cout<<"Narrator: Will you follow ?"<<endl;
            cout<<"1.Yes"<<endl;
            cout<<"2.No.It's to dangerous"<<endl;
            decision1();
            cout<<"Narrator : Interesting choices . You are quite an adventure yourself"<<endl;
            cout<<"After around 1 hours , the footprints lead you to an abandoned house"<<endl;
            cout<<"Will you go in "<<endl;
            cout<<"Yes or No you choose "<<endl;
            decision2();
            cout<<"Narrator : So you choose to go in "<<endl;
            cout<<"it is an old house with 1 floor"<<endl;
            cout<<"There are a living room, a dining room, a bed room,a bathroom and a stair which look like it lead to the basement"<<endl;
            cout<<"Which room you choose "<<endl;
            cout<<"1.Livingroom"<<endl;
            cout<<"2.Bathroom"<<endl;
            cout<<"3.Dining room"<<endl;
            cout<<"4.The stair"<<endl;
            decision3();
        }

Я сделал игру для своего промежуточного периода. Я хочу создать функцию, когда я ее вызываю, она приведет меня в другую комнату, но когда я в гостиной, я не могу выбрать ванную или лестницу

1 Ответ

0 голосов
/ 16 марта 2020

Вопрос еще недостаточно ясен, чтобы получить конкретные ответы. Однако я могу дать вам несколько общих советов:

  • всегда используйте описательные имена, чтобы сделать ваш код более понятным. choices3 не очень полезно как имя, особенно если ваша программа становится большой.
  • старайтесь избегать повторяющегося кода. Переместите общий код в функции.
  • попробуйте прочитать об использовании классов в качестве типов объектов.
  • enum также может быть интересно посмотреть.
...