Jump to content


Simple text based game in C++


25 replies to this topic

#1 Alex007152

    Member

  • Members
  • PipPip
  • 47 posts

Posted 07 July 2006 - 08:14 PM

I decided to write a simple text based game just incase someone's interested in playing around with C++.

My compiler is dev C++ and i highly reccomend it. Click here if your interested in dev C++.

This is the sourcode:


#include <iostream>

using namespace std;


int main()

{

    cout <<"\tWelcome to my text based game!\n";

    char userName[100];

    cout <<"\nPlease enter your username: ";

    cin >>userName;

    cout <<"Hello, "<<userName<<"!\n\n";

    

    cout <<"Please pick your race: \n";

    cout <<"1 - Human\n";

    cout <<"2 - Orc\n";

    int pickRace;

    cout <<"Pick your race: ";

    cin >>pickRace;

    

    switch (pickRace)

    {

           case 1:

                cout <<"You picked the Human race.\n";

                break;

           case 2:

                cout <<"You picked the Orc race.\n";

                break;

           default:

                   cout <<"Error - Invalid input; only 1 or 2 allowed.\n";

    }


    

    int difficulty;

    cout <<"\nPick your level difficulty: \n";

    cout <<"1 - Easy\n";

    cout <<"2 - Medium\n";

    cout <<"3 - Hard\n";


    cout <<"Pick your level difficulty: ";

    cin >>difficulty;

    

    switch (difficulty)

    {

           case 1:

                cout <<"You picked Easy.\n\n";

                break;

           case 2:

                cout <<"You picked Medium.\n\n";

                break;

           case 3:

                cout <<"You picked Hard.\n\n";

                break;

           default:

                   cout <<"Error - Invalid input; only 1,2 or 3 allowed.\n";

    }

  

    system("PAUSE");

    return 1;

}

I will explain some of the code in here just incase your new to C++.
before you start, make sure you downloaded dev C++.
lets start with a nice introduction to the user:

#include <iostream>

using namespace std;


int main()

{

    cout <<"\t\tWelcome to my game!\n\n";

    

    system("PAUSE");

    return 1;

}

the 'cout' command shows the user that runs your .exe file the content you entered within the "". Never forget the ; after your done with the content within the ""!
the 'system("PAUSE");' command obviously stops the DOS window from quickly showing and automaticaly closing.
the 'return 1;' command indicates the computer that the program ended without any problems. So it's quite useful to write it! :lol:
the '\t\t' within the "" after 'cout <<' moves the text you entered right after it a little bit to the right so the more '\t' you enter the more the text moves to the right.

now, lets write something cool.
heres the code of an interesting program:

#include <iostream>

using namespace std;


int main()

{

    cout <<"\t\tWelcome to my game!";


    char name[100];

    cout <<"What is your name? ";

    cin >>name;

    cout <<"Hello, "<<name<<"!";

    

    

    system("PAUSE");

    return 1;

}

Now we know how to use the cout command.
the 'char' command creates a variable. everything you enter right after the char part automaticaly becomes the name of the variable. the '[100]' part i added means that the user can enter up to 100 characters in the text box.
the 'cin' command is basicaly a text box. The user can enter content in it that can be displayed later on. In order to use the content written by the user you must add the following code in a cout command:

"<<name<<"

in this example, it is:

    cout <<"Hello, "<<name"!";

As you can see i added the '"<<name<<"' part in the cout command. 'name' is in this case the name of the variable you wrote:

    char name[100];

I'll be editing this page later on when i find time, ill add more cool tricks soon =).

PS this is my first topic, hopefully it's appropriate!

#2 Moulinex

    New Member

  • Members
  • Pip
  • 7 posts

Posted 07 July 2006 - 09:03 PM

Alex007152 said:

the 'return 1;' command indicates the computer that the program ended without any problems. So it's quite useful to write it! :lol:

Is it compiler specific or main should return 0 to tell the OS that the program ended without any problem?

#3 eddie

    Senior Member

  • Members
  • PipPipPipPip
  • 751 posts

Posted 07 July 2006 - 09:17 PM

main should always return 0 on success. Non-zero specifies an error condition.

Most build tools will break on non-zero, for instance.

That said, in a stand-alone application, what you return doesn't really matter. Noone will be checking your return code anyhow.

Still, get in the habit of returning '0' for success. There's various reasons this can be confusing (boolean values vs. system expected return codes), but just keep in mind that you want to return 0 to the OS.

#4 Reedbeta

    DevMaster Staff

  • Administrators
  • 5310 posts
  • LocationSanta Clara, CA

Posted 08 July 2006 - 12:09 AM

Also, please use [ code ] tags rather than [ quote ] for code, in the future ;)
reedbeta.com - developer blog, OpenGL demos, and other projects

#5 Alex007152

    Member

  • Members
  • PipPip
  • 47 posts

Posted 08 July 2006 - 08:44 AM

Thanks all, i will edit the first post. It's just that i used '1' instead of '0' for this program as i got used to it due to several tutorials but now i've noticed what's going on with it.

Will use code tags instead next time Reedbeta :p.

#6 monjardin

    Senior Member

  • Members
  • PipPipPipPip
  • 1033 posts

Posted 09 July 2006 - 09:44 PM

You might consider using a std::string instead of a char array to hold the user name input. Also, I think your program will break if the user enters a name with embedded white space.
monjardin's JwN Meter (1,2,3,4,5,6):
|----|----|----|----|----|----|----|----|----|----|
*

#7 eddie

    Senior Member

  • Members
  • PipPipPipPip
  • 751 posts

Posted 09 July 2006 - 09:49 PM

[muse]
You know, it might be interesting/beneficial to make a Wiki post of something like this "game", and then slowly refactor it, using the Wiki history as a way to catalogue the change history.

People could tune in and see the good/bad engineering practices, etc.
[/muse]

#8 Alex007152

    Member

  • Members
  • PipPip
  • 47 posts

Posted 10 July 2006 - 08:14 AM

monjardin said:

You might consider using a std::string instead of a char array to hold the user name input. Also, I think your program will break if the user enters a name with embedded white space.

Thanks for the advice. I've tested the program several times and no problems appeared.

eddie said:

[muse]
You know, it might be interesting/beneficial to make a Wiki post of something like this "game", and then slowly refactor it, using the Wiki history as a way to catalogue the change history.

People could tune in and see the good/bad engineering practices, etc.
[/muse]

I see. I know this is no game, the point in it is to show the user the very very basics. Thanks for the advice, eddie.

#9 jjd

    Member

  • Members
  • PipPip
  • 65 posts

Posted 10 July 2006 - 03:13 PM

Alex007152 said:

Thanks all, i will edit the first post. It's just that i used '1' instead of '0' for this program as i got used to it due to several tutorials but now i've noticed what's going on with it.

As pointed out, 0 is standard for success. The best reason I have heard for this is that there is only one way for the program to succeed, but multiple ways to fail. So returning a non-zero value gives more scope to return meaningful information when the program exits.
hi, i'm a signature viruz, plz set me as your signature and help me spread :)

#10 Jare

    Valued Member

  • Members
  • PipPipPip
  • 247 posts

Posted 14 July 2006 - 02:08 PM

You may want to consider a loop around input options, something like:

bool validInput = true;
int pickRace;
do
{
    cout <<"Please pick your race: \n";
    cout <<"1 - Human\n";
    cout <<"2 - Orc\n";
    cout <<"Pick your race: ";
    cin >>pickRace;
    
    switch (pickRace)
    {
           case 1:
                cout <<"You picked the Human race.\n";
                break;
           case 2:
                cout <<"You picked the Orc race.\n";
                break;
           default:
                cout <<"Error - Invalid input; only 1 or 2 allowed.\n";
                validInput = false;
    }
} while (!validInput);


#11 monjardin

    Senior Member

  • Members
  • PipPipPipPip
  • 1033 posts

Posted 14 July 2006 - 04:10 PM

You forgot to set validInput to true on a correct entry. ;)
monjardin's JwN Meter (1,2,3,4,5,6):
|----|----|----|----|----|----|----|----|----|----|
*

#12 eddie

    Senior Member

  • Members
  • PipPipPipPip
  • 751 posts

Posted 14 July 2006 - 04:15 PM

Personally, I'd do:

bool validInput=false;
int pickRace;
while(!validInput)
{
    cout <<"Please pick your race: \n";
    cout <<"1 - Human\n";
    cout <<"2 - Orc\n";
    cout <<"Pick your race: ";
    cin >>pickRace;
    
    switch (pickRace)
    {
           case 1:
                cout <<"You picked the Human race.\n";
                break;
           case 2:
                cout <<"You picked the Orc race.\n";
                break;
           default:
                cout <<"Error - Invalid input; only 1 or 2 allowed.\n";
                continue;
    }
    validInput = true;
}


#13 eddie

    Senior Member

  • Members
  • PipPipPipPip
  • 751 posts

Posted 14 July 2006 - 04:20 PM

monjardin said:

You forgot to set validInput to true on a correct entry. ;)

His is true to start with, hence the do....while().

#14 monjardin

    Senior Member

  • Members
  • PipPipPipPip
  • 1033 posts

Posted 14 July 2006 - 06:29 PM

eddie said:

His is true to start with, hence the do....while().
If the user enters an incorrect character in Jare's code then you get an infinite loop. validInput never gets set back to true. The break statement inside the switch block has no effect on the do-while loop. Right?
A continue is a different story, hence your code works.
monjardin's JwN Meter (1,2,3,4,5,6):
|----|----|----|----|----|----|----|----|----|----|
*

#15 eddie

    Senior Member

  • Members
  • PipPipPipPip
  • 751 posts

Posted 14 July 2006 - 06:33 PM

Ah, right. Hence why I wouldn't write that code. ;)

On another note, I find in practice I very rarely use do...whiles() actually.

#16 monjardin

    Senior Member

  • Members
  • PipPipPipPip
  • 1033 posts

Posted 14 July 2006 - 06:46 PM

If you take your approach a step further, you don't even need the validInput variable:

int pickRace;

for (;;)

{

    cout <<"Please pick your race: \n";

    cout <<"1 - Human\n";

    cout <<"2 - Orc\n";

    cout <<"Pick your race: ";

    cin >>pickRace;


    switch (pickRace)

    {

    case 1:

        cout <<"You picked the Human race.\n";

        break;

    case 2:

        cout <<"You picked the Orc race.\n";

        break;

    default:

        cout <<"Error - Invalid input; only 1 or 2 allowed.\n";

        continue;

    }

    break;

}


monjardin's JwN Meter (1,2,3,4,5,6):
|----|----|----|----|----|----|----|----|----|----|
*

#17 eddie

    Senior Member

  • Members
  • PipPipPipPip
  • 751 posts

Posted 14 July 2006 - 08:08 PM

You prefer the for( ; ; ), eh? :)

Any reason?

#18 SmokingRope

    Valued Member

  • Members
  • PipPipPip
  • 210 posts

Posted 14 July 2006 - 08:54 PM

Because then you can write;

#define EVER ;;

// ...

for( EVER )
{
    if( someCondition )
        break;
}


#19 eddie

    Senior Member

  • Members
  • PipPipPipPip
  • 751 posts

Posted 14 July 2006 - 08:56 PM

Heh, that's cute. ;)

Granted, defines like that are the tool of the devil. ;)

#20 Reedbeta

    DevMaster Staff

  • Administrators
  • 5310 posts
  • LocationSanta Clara, CA

Posted 14 July 2006 - 09:21 PM

I actually saw that on one of those crappy motivation posters. It said
#define EVER ;;
for (EVER)
{
    learn();
    think();
    grow();
}

How silly is that?
reedbeta.com - developer blog, OpenGL demos, and other projects





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users