Textadventure (Feedback)



  • Hello, I'm a new User willing to improve my programming skills. So please give me some feedback and i hope you like reading it. It's a bit confuse but don't worry my head is fully functioning.

    BlueBrain

    #include <iostream>
    #include <string>
    
    using namespace std;
    
    int main()
    {
      cout << "Welcome to my Text Adventure..." << endl;
      cin.get();
      string username;
      cout << "Please tell me your username: ";
      cin >> username;
      cout << "Well done " << username << " your Adventure could will begin right now." << endl;
      cout << "But first talk about the Rules: I, the Narrator, will soon give you choices... to choose from..." << endl;
      cin.get();
      cout << "Choose wisely and please only Input numbers!" << endl;
      cin.get();
      cout << "You traveled by plane and it crashed on a lonely Island, nobody is there, they all seem to be dead! And its pitch black dark!" << endl;
      cout << "What are you going to do? 1. Make a fire! 2. Search for survivors!" << endl;
    
      choice1:
      cout << "Your choice: ";
      int choice1;
      cin >> choice1;
      switch(choice1)
      {
        case 1:
          cout << "Some metres away from your position, you see a dead person lying around!" << endl;
          cout << "You have to go for a look!" << endl;
          cin.get();
          cout << "The person was shot! Is here something going on, am I'm getting telled the truth?" << endl;
          cin.get();
          cout << "We'll see..." << endl;
          break;
        case 2:
          cout << "You are fondling around...something warm lies there in the dark..." << endl;
          cin.get();
          cout << "Your hands are getting wet... What the hell?" << endl;
          cin.get();
          cout << "The fluid on your hands smells like old iron bars." << endl;
          cin.get();
          cout << "OMG, it's a dead body with a big gaping wound in his/her chest!" << endl;
          break;
        default:
          cout << "Please type a number given in the answers before." << endl;
          goto choice1;
      }
      if(choice1==1)
      {
        cout << "You are searching for a Weapon, which could do those holes." << endl;
        cin.get();
        cout << "Oh, you've found a golden chasm in the guys' hands..." << endl;
        cin.get();
        cout << "What does this thing have to do with my task? For searching a weapon...?" << endl;
        cin.get();
        cout << "I don't know, but i played many games before and golden chasms are mostly important later on..." << endl;
        cin.get();
        cout << "I can't find anything else interesting, so i'll stop searching for now!" << endl;
        cin.get();
        cout << "There is coming a strange noise out of the plane..." << endl;
        cin.get();
        cout << "What do you want me to do? 1. Go for a look! 2. Hide and see what's going on." << endl;
        choice2:
        cout << "Your choice: ";
        int choice2;
        cin >> choice2;
        switch(choice2)
        {
          case 1:
            cout << "Slowly you are peeking around the corner of the broken airplane..." << endl;
            cin.get();
    	cout << "My mind tells me: \"Run " << username << ", Run...\"" << endl;
    	cin.get();
    	cout << "But i don't! I'm still here to go for a look!" << endl;
    	cin.get();
    	cout << "But, what? What's that? Is it a penguin! It can't be true!" << endl;
    	cin.get();
    	cout << "Here in the jungle, in the middle of nowhere? A penguin! I'm starting to laugh! But what does he...?" << endl;
    	cin.get();
    	cout << "What does he is holding in his mouth? A gun!?! The gun i was searching for?" << endl;
    	cin.get();
    	cout << "The Penguin starts to look around...!" << endl;
    	cin.get();
    	cout << "For me it looks like he has heard something... Is it..?" << endl;
    	cin.get();
    	cout << "Is it possible, that he heard me... NOWAY! YES! HE HEARD!!!" << endl;
    	cin.get();
    	cout << "He spit out his weapon... And now he holds it..." << endl;
    	cin.get();
    	cout << "He seems to aim around, but is it possible... It's a penguin... I'm for sure!" << endl;
    	cin.get();
    	cout << "ITS POSSIBLE.. HE'S SHOOTING AT ME... My mind tells me to run... but i'm to perplex to run away..." << endl;
    	cout << "Is this the end?" << endl;
    	cin.get();
    	cout << "GAME OVER!" << endl;
    	return 0;
    	break;
          case 2:
    	cout << "I'm hiding behind a bush near the hole in the side of the airplane..." << endl;
    	cin.get();
    	cout << "Does the murder is still around?" << endl;
    	cin.get();
    	cout << "Is he in there...? The noise is getting louder... I can clearly hear someone moaning in there..." << endl;
    	cin.get();
    	cout << "A SHOT!" << endl;
    	cout << "The moaning stopped and birds are flying around..." << endl;
    	cin.get();
    	cout << "I'm very nervous... The murder must be in there!" << endl;
    	cin.get();
    	cout << "The chasm starts blinking... What does this mean?" << endl;
    	cin.get();
    	cout << "A dark green light..." << endl;
    	cout << "It's pulse is getting slower..." << endl;
    	cin.get();
    	cout << "It has stopped blinking!" << endl;
    	cout << "What does this mean...? Where am I?" << endl;
    	cin.get();
    	cout << "Wait, someone is leaving the fuselage... The double murderer?" << endl;
    	cin.get();
    	cout << "Look it's only a Penguin..." << endl;
    	cout << "But, maybe the Killer is still in there... I don't going to leave my hide for now... Keep looking..." << endl;
    	cin.get();
    	cout << "The Penguin seems to hold a Weapon... Am I dreaming... I must be dreaming..." << endl;
    	cin.get();
    	cout << "Did he shot the all the People? Could he even do it?" << endl;
    	cin.get();
    	cout << "15 MINUTES LATER" << endl;
    	cin.get();
    	cout << "I can't see the him anymore..." << endl;
    	cout << "I'm leaving my hide to look for the dead corpse..." << endl;
    	cin.get();
    	cout << "Another Shot!" << endl;
    	cout << "The Chasm again starts blinking!" << endl;
    	cin.get();
    	cout << "What does this mean, but hey wait... There is a radio device lying around the dead captain..." << endl;
    	cin.get();
    	cout << "What should i do? 1. Start to radio for emergency! 2. Take it and try it elsewhere!" << endl;
    	int choice3;
    	choice3:
    	cout << "Your choice: ";
    	cin >> choice3;
    	switch(choice3)
    	{
    	  case 1:
    	    cout << "BOOOOOOM" << endl;
    	    cin.get();
    	    cout << "You died in an explosion!" << endl;
    	    return 0;
    	    break;
    	  case 2:
    	    cout << "Backpacked!" << endl;
    	    cin.get();
    	    cout << "I'm sitting in the airplane!" << endl;
    	    break;
    	  default:
    	    cout << "Please try to input a choice given before!" << endl;
    	    goto choice3;
    	}
    	break;
          default:
    	cout << "Please try to input a choice given before!" << endl;
    	goto choice2;
        }
      if(choice1==2)
      {
        cout << "What is going on here?" << endl;
        cin.get();
        cout << "You've heard a clicking sound!" << endl;
        cin.get();
        cout << "BOOM! You are falling to the ground!" << endl;
        cin.get();
        cout << "Your eyes are closed, heavy breathing... You hear someone moving towards you..." << endl;
        cin.get();
        cout << "\"I'm a SuperTux, the owner of these abandoned Island! And I hate People try to Install Windows on my Island!\"" << endl;
        cin.get();
        cout << "\"The Computers you bring with you are very outdated... in case of its fle...\"" << endl;
        cout << "The Voice is getting very dull... Your heart is hammering..." << endl;
        cin.get();
        cout << "YOU DIED!" << endl;
        return 0;
      }
        }
      cout << "I'm looking around, nothing special excludeed those corpses lying 'round!" << endl;
      cin.get();
      cout << "And my fire enlightened with these clothes which are hanging on the plane wreckages..." << endl;
      cin.get();
      cout << "What did that do? God, Pastafarian, Methheads? Or just random stuff happening like everydays..." << endl;
      cin.get();
      cout << "Am I dreaming my morbid fantasy? My Computer Science lessons back in time...and the Game..." << endl;
      cin.get();
      cout << "The Game was solvable they told me, the Game only needs to be programmed... They said..." << endl;
      cin.get();
      cout << "But i was to weak... I can't solve the puzzle, I can't open pandoras box... the Game..." << endl;
      cin.get();
      cout << "I'm such a pessimistic Person in front of a LSD Monitor... the World..." << endl;
      cin.get();
      cout << "WEAKNESS, \"You can't... can't solve the Puzzle...\", they said..." << endl;
      cin.get();
      cout << "The clicking of a mouse, a computer mouse..." << endl;
      cout << "My Spirit is leaving me..." << endl;
      cin.get();
      cout << "What should I do? 1. Breath in... Breath out... 2. Die like the rest!" << endl;
      int choice4;
      choice4:
      cout << "Your choice: ";
      cin >> choice4;
      switch(choice4)
      {
        case 1:
    	cout << "Ahh, I can feel fresh air exhilarating my lungs..." << endl;
    	cin.get();
    	cout << "Is it possible, that i've to algorithmise the World... to stop my pain... headaches..." << endl;
    	cin.get();
    	cout << "Pain... I need to go out, leave my dream... my absolution..." << endl;
    	cin.get();
    	cout << "I'm angry, the Sun is so bright... Look at those People runnin' around, enjoying life..." << endl;
    	cin.get();
    	cout << "Flee... leaving this place..." << endl;
    	cin.get();
    	cout << "Back to virtual life, booting Penguin..." << endl;
    	cin.get();
    	cout << "cd Videos/Movies/Anime/... ENTER" << endl;
    	cin.get();
    	cout << "Animes, those surreal little flicks on my synaptic cortex..." << endl;
    	cin.get();
    	cout << "The real world... backhanded... Trying to live... giving my life back into my hands... backhanded..." << endl;
    	cin.get();
    	cout << "Footsteps... little footsteps... i've to borne everydays new curses..." << endl;
    	cin.get();
    	cout << "Formatting my Brain..." << endl;
    	cin.get();
    	cout << "Restarting... ShuttingOff... Booting..." << endl;
    	cin.get();
    	cout << "Hello World!" << endl;
    	return 0;
    	break;
        case 2:
    	cout << "ENTER PASSWORD:_ _ _ _ _" << endl;
    	cout << "BRUTEFORCING... ... ... ..." << endl;
    	cin.get();
    	cout << "HEADS ON... MUSIC 1... NOTEPAD.executed..." << endl;
    	cout << "#included some libraries..." << endl;
    	cin.get();
    	cout << "Books.. E-Books.." << endl;
    	cin.get();
    	cout << "DECLARING some Floats, Integers..." << endl;
    	cin.get();
    	cout << "Bits and bytes infilling my brain, random accsess memorys..." << endl;
    	cout << "Like a lightning running up and down my nerves, my central nervous system..." << endl;
    	cin.get();
    	cout << "Stimulating, stimulated... My Heart is pumping electricity... the essence..." << endl;
    	cin.get();
    	cout << "Enter the Matrix, the updated one, the Socilisation in Perfection..." << endl;
    	cin.get();
    	cout << "The Movie was good... the Game was better... Living the Matrix is best..." << endl;
    	cin.get();
    	cout << "Starvation..." << endl;
    	cout << "Beeing hungry for more..." << endl;
    	cin.get();
    	cout << "sudo apt-get deinstall reallife" << endl;
    	cout << "... ... ... ... ... ... ... ..." << endl;
    	cin.get();
    	cout << "TO BE CONTINUED!" << endl;
    	return 0;
    	break;
         default:
    	cout << "Please give me a better choice..." << endl;
    	goto choice4;
    	break;
       }
    }
    

  • Mod

    Goto is considered harmful

    You spend hundreds of lines of code to do the same program logic all over again and again, just with different texts. You should build some kind of engine that justs implements this kind of logic once and reads the dialogue from a file or some other source.

    endl = '\n' + flush. Do you really want to flush every time you use endl in your program? You probably only want a line break ('\n'). Any operation on cin will automatically flush cout (as those two streams are tied), thus, you probably never want to explicitly flush in your program.

    cin.get(); is not the same as "wait for the user to press enter". It seems you expect it to work like this. But it won't. For example if the user types some text and then presses enter, cin.get(); will read the first (non-whitespace) character of said text and the rest of the text will remain in the stream.



  • Als erstes möchte ich mich für Ihr ausführliches Feedback bedanken. Und mir ist klar das ich dieselbe Schose auch durchaus mit weniger Code und in eine Textdatei auslagern kann, was aber bisher noch meinen Horizont übersteigt.

    Die Flush Geschichte habe ich beim Testen (Er überspringt quasi die nächste Eingabeaufforderung mit cin und gibt da die übrigen Zeichen zurück) bemerkt und überlege ob man das ganze besser mit der "ncurses" Library implementieren könnte (getch(), printw(), etc.). Macht das Sinn?

    EDIT: Und vor allem hätte ich hier mehr Macht, bzw. bessere Funktionen mit denen ich arbeiten könnte. Farbgebung, Cls, etc.

    Zu den GOTOS:
    Ich denke für kleinere Spielereien sind sie zweckdienlich, lasse mich jedoch gerne eines besseren belehren. Im Grunde ging es um eine kleinere Spielerei die ich evtl. wenn ich Zeit finde, weiter ausbaue bzw. verbessere.

    Ich habe auch kleinere Programme zur Integralrechnung und dergleichen geschrieben, vielleicht kommt da eher mein eigentlicher Programmierstil raus. Bei Interesse hänge ich das mal an, denn hier ging es mir in erster Linie um die Story und ich wollte mal drauflos programmieren, "coden". Denn es wird immer gesagt, Übung, Übung, Übung. Ich stoße jedoch sehr schnell an Probleme, die für mich auf den ersten Blick unlösbar erscheinen. Also ist dieser Code - Schnippsel eher unstrukturiert und nicht durchdacht. Aber ich fand Ihn als Einstieg ins Forum angebracht. Weil simpel und deutlich wird wie konfus sowas werden kann auf längere Sicht (Eigene Implementierung, Auslagerung als Textdatei liegt nahe und ist auch einfacher nachzuvollziehen).

    OFFTOPIC:
    Interessant, dass ich erst jetzt bemerke das die Homepage deutschsprachig ist. Hatte heute Morgen eigentlich nach dem englischsprachigem Pendant Ausschau gehalten.


  • Mod

    BlueBrain schrieb:

    Die Flush Geschichte habe ich beim Testen (Er überspringt quasi die nächste Eingabeaufforderung mit cin und gibt da die übrigen Zeichen zurück)

    Das hat überhaupt nichts mit dem flushen der Ausgabe zu tun.

    bemerkt und überlege ob man das ganze besser mit der "ncurses" Library implementieren könnte (getch(), printw(), etc.). Macht das Sinn?

    Ja. C++-Streams arbeiten auf abstrakten Zeichenketten, da gibt es kein Konzept von Tastatur oder Bildschirm. Wenn du aber Programmlogik hast, die nur mit Tastatur/Bildschirm wirklich Sinn macht, dann bietet sich die Benutzung eines Ein-/Ausgabekonzepts an, das auf deren Benutzung ausgelegt ist.

    Zu den GOTOS:
    Ich denke für kleinere Spielereien sind sie zweckdienlich, lasse mich jedoch gerne eines besseren belehren. Im Grunde ging es um eine kleinere Spielerei die ich evtl. wenn ich Zeit finde, weiter ausbaue bzw. verbessere.

    Wieso ist das ein Argument für goto? Das kommt mir vor wie eine Liste von Gründen gegen goto. So lernst du nicht einmal am kleinen Beispiel, wie es besser ginge, sondern gewöhnst dir goto an. Erweitern ist bei Spagetticode auch nicht mehr wirklich machbar, das ist ja gerade das Problem.

    Ich habe auch kleinere Programme zur Integralrechnung und dergleichen geschrieben, vielleicht kommt da eher mein eigentlicher Programmierstil raus. Bei Interesse hänge ich das mal an, denn hier ging es mir in erster Linie um die Story und ich wollte mal drauflos programmieren, "coden".

    Den Text des Adventures habe ich mir übrigens gar nicht angesehen.



  • Hab ein bug gefunden, sobald man beim Weg entscheiden wohin man geht eine Zahl eingeben muss gibt man ein Zeichen an und man steckt in einer Dauerschleife



  • Ok habe mich ein wenig informiert bzgl. Flushen:
    Zeichen, bzw. Zeichenketten werden also im Buffer zwischengespeichert und erst nach einem Flush oder nach einem Überlauf ausgegeben und deshalb wurden mir in meinem Programm Zeichen unterschlagen wenn ich bei der Fkt. cin.get() schon eine eingabe erzeuge, grob richtig?

    Bsp:

    std::cout << "Platzhalter"; // Bis hierhin keine Ausgabe
    std::flush(std::cout);
    

    Ich verstehe das Problem mit den GOTOs grundsätzlich und habe es auch eigentlich immer gemieden. Bin das nen bisschen von BASIC gewöhnt.

    Und bei Interesse: Schau dir meinen anderen Beitrag an zur Integralrechnung, bzw. der Bildung von Ober- und Untersumme und der näherung des Integralwertes. Das sollte angenehmer zu lesen sein.

    Und nochmals danke für Ihr Feedback.

    BlueBrain


  • Mod

    BlueBrain schrieb:

    Ok habe mich ein wenig informiert bzgl. Flushen:
    Zeichen, bzw. Zeichenketten werden also im Buffer zwischengespeichert und erst nach einem Flush oder nach einem Überlauf ausgegeben und deshalb wurden mir in meinem Programm Zeichen unterschlagen wenn ich bei der Fkt. cin.get() schon eine eingabe erzeuge, grob richtig?

    Deine Problem mit der Benutzerführung haben überhaupt nichts mit der Ausgabe zu tun. Das ist eine ganz andere Problematik.

    SeppJ schrieb:

    cin.get(); is not the same as "wait for the user to press enter". It seems you expect it to work like this. But it won't. For example if the user types some text and then presses enter, cin.get(); will read the first (non-whitespace) character of said text and the rest of the text will remain in the stream.


Anmelden zum Antworten