Results 1 to 14 of 14
  1. #1
    VvITylerIvV's Avatar
    Join Date
    Oct 2009
    Gender
    male
    Location
    The streets
    Posts
    668
    Reputation
    5
    Thanks
    61
    My Mood
    In Love

    How to make a simple text based game in c++

    well. This is my first tutorial on c++, normally I can write good tutorials but it is my first on this topic so it may not be the best.

    The code for the simple text based game we will be creating is as follows

    in a console application I have created the following two files with the following code within them.

    functions.h[php]
    #include <iostream>
    #include <string>
    #include <sstream>

    using namespace std;

    void Felse(){
    cout << "that wasn't an answer...\n";
    }

    void test (string a, char c[40], char e[40] ){
    if(a == "y"){
    cout << c << endl;
    }
    else if(a == "n"){
    cout << e << endl;
    }
    else { Felse(); }
    }[/php]

    functions.cpp
    [php]#include "functions.h"

    int main(){
    string b;
    char d[40] = "you answered yes";
    char f[40] = "you answered no";

    cout << "please enter y/n"<< endl;
    getline (cin,a);
    test(a, c, e);
    }[/php]
    If you put these in your compiler, it will be a VERY simple text based game.

    If this makes sense to you, you shouldn't be reading this. If it doesn't make sense, well you came to the right place. I hope.



    Lets take this apart, piece by piece.

    The first things we notice in functions.h is [php]#include <iostream>
    #include <string>
    #include <sstream>[/php]

    What this is saying, is to include the iostream.h file which includes input/output streams (cout/cin etc)

    to include string, so we can use the string variable which is quite handy and to include sstream so we can use the getline (cin,a); which is far less glitchy the cin >>

    as we continue down the code we see [php]using namespace std;[/php]

    this handy piece of code makes it so we don't have to add std:: before just about everything. I like that .

    Now we get to the confusing part of it... [php]void Felse(){
    cout << "that wasn't an answer...\n";
    }[/php]

    Well, if this were to be void Felse; it would just be a variable with no type declared. But, it's a function with no type declared because its void Felse(){}

    what this does, is it makes the code within the '{' '}' run whenever you call Felse();

    which MEANS when we calle Felse(); it will basically be replaced by cout <<"that wasn't an answer...\n";

    and THAT is what functions is all about (kinda)

    [php]void test (string a, char c[40], char e[40] ){
    if(a == "y"){
    cout << c << endl;
    }
    else if(a == "n"){
    cout << e << endl;
    }
    else { Felse(); }
    }[/php]

    now, its void test(string a, char c[40], char e[40]){} now, the strings and chars inside the () are replaced by whatever variables we use when we call this function in our code... more on that later.

    Then we have the basic if statements, and what have you.

    What you may or may not have noticed, is [php]else { Felse(); }[/php] Yes, thats right. I didn't wanna right cout <<... so I used the Felse function. Really handy and cleans up the code a lot.

    Alright, now to expain functions.cpp
    [php]#include "functions.h"

    int main(){
    string b;
    char d[40] = "you answered yes";
    char f[40] = "you answered no";

    cout << "please enter y/n"<< endl;
    getline (cin,a);
    test(a, c, e);
    }[/php]

    You may be wondering, WHAT THE HELL1!?!?! but don't worry.

    I included function test.h which included everything else we need (chain reaction type thing) By including our own header file, the .cpp file now contains all the code from that header file. Which means, we can use the functions in our header file in our .cpp file. Make sense?

    Well, I basically have three variables, a string and two chars. char c is "you answered yes"

    char e is "you answered no"

    string a has nothing assigned to it yet.

    We then use the cout function to display enter y/n then the getline function to get whatever was on that line, take that and put it into a.


    get line works a little like cin, just a bit differently. It only works for strings, so you can't do

    [php]
    int a;
    getline (cin,a);

    / have to do

    string a;
    getline (cin,a);

    [/php]

    and it just inputs whatever was on the line into the variable. quite simple really, not as buggy as cin.

    weeellll now we arrive at the functions.

    [php]
    test(b, d, f);
    [/php]

    remember how we made the function
    [php]
    void test (string a, char c[40], char e[40] )
    [/php]

    well, see how they both have three variables within the brackets? thats important.

    when you call a function, whatever is in the brackets replaces any other instance of what is in the brackets when you create the function.


    soo, by using test(b,d,f);

    we are changing the

    [php]
    void test (string a, char c[40], char e[40] ){
    if(a == "y"){
    cout << c << endl;
    }
    else if(a == "n"){
    cout << e << endl;
    }
    else { Felse(); }
    }
    [/php]
    function too

    [php]
    void test (string b, char d[40], char f[40] ){
    if(b == "y"){
    cout << d << endl;
    }
    else if(b == "n"){
    cout << f << endl;
    }
    else { Felse(); }
    }
    [/php]

    If this STILL does not make sense, please PM me and I'll explain further.


    Please, if there is any way I could clean this up please PLEASE tell me. I want to help, not confuse.
    Favourite quotes:

    Code:
    I don't need easy, I just need possible. ~ Me 
    
    There are three birds on a fence. Two decide to fly away, how many are left? Three, just because you decide to do something doesn't mean you've done it. ~ Don't know who said this
    
    Do not go where the path may lead, go instead where there is no path and leave a trail. ~ Ralph Waldo Emerson
    Quote Originally Posted by VirtualSia View Post
    You both have a very weird and awkward view on Computer science.
    Computer science is about computing, which is programming.
    Definition of computing: The use or operation of computers.
    Turning on my computer = computing = programming
    LAWLFAIL

  2. The Following User Says Thank You to VvITylerIvV For This Useful Post:

    Disco // (08-12-2010)

  3. #2
    Auxilium's Avatar
    Join Date
    Jul 2010
    Gender
    male
    Location
    深い碧の果てに
    Posts
    4,518
    Reputation
    445
    Thanks
    609
    My Mood
    Happy
    Cool
    I wouldn't call them Text Based games. Just Console Applications. Text based games would be like Games for a cp/m where its like an rpg just with text
    Last edited by Auxilium; 08-07-2010 at 06:48 PM.

  4. #3
    VvITylerIvV's Avatar
    Join Date
    Oct 2009
    Gender
    male
    Location
    The streets
    Posts
    668
    Reputation
    5
    Thanks
    61
    My Mood
    In Love
    Quote Originally Posted by Koreans View Post
    Cool
    I wouldn't call them Text Based games. Just Console Applications. Text based games would be like Games for a cp/m where its like an rpg just with text
    Well, using this you can make a text based game. All you gotta do is re-use the code that is shown here.
    Favourite quotes:

    Code:
    I don't need easy, I just need possible. ~ Me 
    
    There are three birds on a fence. Two decide to fly away, how many are left? Three, just because you decide to do something doesn't mean you've done it. ~ Don't know who said this
    
    Do not go where the path may lead, go instead where there is no path and leave a trail. ~ Ralph Waldo Emerson
    Quote Originally Posted by VirtualSia View Post
    You both have a very weird and awkward view on Computer science.
    Computer science is about computing, which is programming.
    Definition of computing: The use or operation of computers.
    Turning on my computer = computing = programming
    LAWLFAIL

  5. #4
    Auxilium's Avatar
    Join Date
    Jul 2010
    Gender
    male
    Location
    深い碧の果てに
    Posts
    4,518
    Reputation
    445
    Thanks
    609
    My Mood
    Happy
    It'd be a little more than that.
    Most text based games were like you choose the direction to go and you get another string of text.
    And you'd need to incorporate stuff like classes

  6. #5
    VvITylerIvV's Avatar
    Join Date
    Oct 2009
    Gender
    male
    Location
    The streets
    Posts
    668
    Reputation
    5
    Thanks
    61
    My Mood
    In Love
    Quote Originally Posted by Koreans View Post
    It'd be a little more than that.
    Most text based games were like you choose the direction to go and you get another string of text.
    And you'd need to incorporate stuff like classes
    shh don't scare me with classes yet every time I do anything with classes something happens to me... Last time I saw a code with classes in them my mother came home.

    But no, you could make an entire RPG text based game without using any functions or classes. It just makes the code A LOT simpler and A LOT cleaner to read.

    And yes, this is all you need to make a simple game, perhaps not a fighting game but a lifestyle game.

    "Hi there, this is the bus. Where would you like today?
    1. Victoria
    2. Duncan
    3. The Beach"

    3

    "Alright, off to the beach we goo"

    "A lady wearing a yellow poka dot bikini walks up to you."

    "Hi, I was wondering if you've seen my shoes anywhere... I've seem to have lost them"

    "1. yes"
    "2. no"


    2

    "Oh, sorry to bother you"

    Later on I may add in how to make an RPG text based game but I'm kinda half asleep right now.
    Favourite quotes:

    Code:
    I don't need easy, I just need possible. ~ Me 
    
    There are three birds on a fence. Two decide to fly away, how many are left? Three, just because you decide to do something doesn't mean you've done it. ~ Don't know who said this
    
    Do not go where the path may lead, go instead where there is no path and leave a trail. ~ Ralph Waldo Emerson
    Quote Originally Posted by VirtualSia View Post
    You both have a very weird and awkward view on Computer science.
    Computer science is about computing, which is programming.
    Definition of computing: The use or operation of computers.
    Turning on my computer = computing = programming
    LAWLFAIL

  7. #6
    Kallisti's Avatar
    Join Date
    Jul 2009
    Gender
    male
    Location
    深い碧の果てに
    Posts
    4,019
    Reputation
    52
    Thanks
    376
    My Mood
    In Love
    You can't make them without Classes or functions. Period.

    未来が見えなくて怖いから
    未来が見えてしまって悲しいから
    目を閉じて優しい思い出に浸ってしまう




  8. #7
    Lonely Tedy Bear's Avatar
    Join Date
    Jul 2010
    Gender
    female
    Location
    Utah,Salt Lake Posts: 5,371 ►►►►Lonely-Tedy◄◄◄◄
    Posts
    1,541
    Reputation
    -151
    Thanks
    321
    My Mood
    Lonely
    Quote Originally Posted by Kallisti View Post
    You can't make them without Classes or functions. Period.
    dam dude you spam so much you had 300 like 1 day ago , also you should of got yor 1333 ban damit, we need a brake from you /
             

  9. #8
    Kallisti's Avatar
    Join Date
    Jul 2009
    Gender
    male
    Location
    深い碧の果てに
    Posts
    4,019
    Reputation
    52
    Thanks
    376
    My Mood
    In Love
    Quote Originally Posted by Lonely Tedy Bear View Post
    dam dude you spam so much you had 300 like 1 day ago , also you should of got yor 1333 ban damit, we need a brake from you /
    I just came back from my 1337 ban today ******
    and i came back to mpgh this summer with 455 in mid july.

    jealous ****** code leecha that enters leeched code in vbotw is jealous
    Last edited by Kallisti; 08-09-2010 at 07:36 AM.

    未来が見えなくて怖いから
    未来が見えてしまって悲しいから
    目を閉じて優しい思い出に浸ってしまう




  10. #9
    Lonely Tedy Bear's Avatar
    Join Date
    Jul 2010
    Gender
    female
    Location
    Utah,Salt Lake Posts: 5,371 ►►►►Lonely-Tedy◄◄◄◄
    Posts
    1,541
    Reputation
    -151
    Thanks
    321
    My Mood
    Lonely
    Quote Originally Posted by Kallisti View Post


    I just came back from my 1337 ban today ******
    and i came back to mpgh this summer with 455 in mid july.

    jealous ****** code leecha that enters leeched code in vbotw is jealous
    hmm it's very hard to understand you. Maybe you should go learn English better
             

  11. #10
    Kallisti's Avatar
    Join Date
    Jul 2009
    Gender
    male
    Location
    深い碧の果てに
    Posts
    4,019
    Reputation
    52
    Thanks
    376
    My Mood
    In Love
    I hope you can understand this

    I just came back from my 1337 post ban TODAY
    And i came back to MPGH mid july, after i quit during school.

    And
    You are a jealous ****** who leeches code, then enters the leeched code into VBOTW. Easy enough to understand? I think you are a foreigner who cant read properly


    LEECH MORE CODE
    Last edited by Kallisti; 08-09-2010 at 10:43 AM.

    未来が見えなくて怖いから
    未来が見えてしまって悲しいから
    目を閉じて優しい思い出に浸ってしまう




  12. #11
    aclonegeek's Avatar
    Join Date
    Aug 2010
    Gender
    male
    Location
    Canada
    Posts
    349
    Reputation
    10
    Thanks
    23
    My Mood
    Relaxed
    Quote Originally Posted by Kallisti View Post
    I hope you can understand this

    I just came back from my 1337 post ban TODAY
    And i came back to MPGH mid july, after i quit during school.

    And
    You are a jealous ****** who leeches code, then enters the leeched code into VBOTW. Easy enough to understand? I think you are a foreigner who cant listen read properly


    LEECH MORE CODE
    sarry wat can u lurn too speek englash!

    No but seriously I still don't think he understands. It took him awhile on the VBOTW thread.

  13. #12
    Kallisti's Avatar
    Join Date
    Jul 2009
    Gender
    male
    Location
    深い碧の果てに
    Posts
    4,019
    Reputation
    52
    Thanks
    376
    My Mood
    In Love
    I have a couple things to say about this from what i read,

    1. [php]void felse;[/php] you said would be a variable with no type. you cant do that in C++

    2. I wanna know how [php]getline(cin,shit);[/php] is 'far' less glitchy than [php]cin >> shit;[/php]

    未来が見えなくて怖いから
    未来が見えてしまって悲しいから
    目を閉じて優しい思い出に浸ってしまう




  14. #13
    VvITylerIvV's Avatar
    Join Date
    Oct 2009
    Gender
    male
    Location
    The streets
    Posts
    668
    Reputation
    5
    Thanks
    61
    My Mood
    In Love
    Quote Originally Posted by Kallisti View Post
    I have a couple things to say about this from what i read,

    1. [php]void felse;[/php] you said would be a variable with no type. you cant do that in C++

    2. I wanna know how [php]getline(cin,shit);[/php] is 'far' less glitchy than [php]cin >> shit;[/php]


    Yeah, I meant to say "which is not allowed..." on the variable with no type.

    And I've found that cin >> shit; makes it combine all the lines and doesn't wait for input. Getline works perfectly for me, for other people I've recommended it for so I prefer to use it.


    And btw
    Quote Originally Posted by Kallisti View Post
    You can't make them without Classes or functions. Period.

    you absolutely can... it's just a LOT more code... like 100s to maybe even hundreds of thousands more lines... depending on how big of a game it would be.
    Last edited by VvITylerIvV; 08-09-2010 at 05:33 PM.
    Favourite quotes:

    Code:
    I don't need easy, I just need possible. ~ Me 
    
    There are three birds on a fence. Two decide to fly away, how many are left? Three, just because you decide to do something doesn't mean you've done it. ~ Don't know who said this
    
    Do not go where the path may lead, go instead where there is no path and leave a trail. ~ Ralph Waldo Emerson
    Quote Originally Posted by VirtualSia View Post
    You both have a very weird and awkward view on Computer science.
    Computer science is about computing, which is programming.
    Definition of computing: The use or operation of computers.
    Turning on my computer = computing = programming
    LAWLFAIL

  15. #14
    Kallisti's Avatar
    Join Date
    Jul 2009
    Gender
    male
    Location
    深い碧の果てに
    Posts
    4,019
    Reputation
    52
    Thanks
    376
    My Mood
    In Love
    Quote Originally Posted by VvITylerIvV View Post
    Yeah, I meant to say "which is not allowed..." on the variable with no type.

    And I've found that cin >> shit; makes it combine all the lines and doesn't wait for input. Getline works perfectly for me, for other people I've recommended it for so I prefer to use it.


    And btw



    you absolutely can... it's just a LOT more code... like 100s to maybe even hundreds of thousands more lines... depending on how big of a game it would be.
    1. [php]cin >>[/php] does wait for input,

    Code:
    Enter number
    (waits till number entered and enter is press)
    2. If youre making a REAL game, that would cause spaghetti code and unwanted behavior. And not hundreds or thousands. If you were making a REAL game without classes and functions, code would be close to a million (or more) lines longer. And youd still deal with the spaghetti code

    未来が見えなくて怖いから
    未来が見えてしまって悲しいから
    目を閉じて優しい思い出に浸ってしまう




Similar Threads

  1. How to make a simple game in VB2008
    By trevor206 in forum Visual Basic Programming
    Replies: 0
    Last Post: 09-06-2009, 06:33 PM
  2. Text based game hacks
    By Justin4240 in forum General Hacking
    Replies: 7
    Last Post: 12-31-2008, 01:07 PM
  3. Text Based Game Hack..
    By Tayyab in forum Hack Requests
    Replies: 0
    Last Post: 07-08-2008, 04:43 PM
  4. Text-based-games
    By tigerjr38 in forum Hack Requests
    Replies: 0
    Last Post: 05-04-2008, 10:38 PM
  5. Text - Based - Game
    By ktalin91 in forum WarRock - International Hacks
    Replies: 7
    Last Post: 06-30-2007, 03:16 PM