Skip to main content
gamedev.net
🔒 Locked

Text Based Game(Need Help)

Started by ARC inc Aug 26, 2008 at 9:23 AM 8 replies 1.4k views
Original Post
ARC inc
ARC inc
Alright, I am creating a text based game called Ever Life. Right now I have the start up thingy that tells you your playing Ever Life Code
//Text Based Game
#include <iostream>
#include <windows.h>
using namespace std;

int main()
{
    SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),10);
    
    //Initialzation of Program
    cout << "********************************************************\n";
    cout << "********************     Ever Life  ********************\n";
    cout << "********************  Version 1 Test *******************\n";
    cout << "********************************************************\n";
    
    system("Pause");
    
    return 0;
}

Now what I want to do is after the telling of what your playing I'd like to make it able to know if the player wants to start a new game. I know it would have to be a function but I am not 100% sure on how to get the function to work the way it needs to work. like all I can think of how to start it is this
int new_game
            cout << "Wish to start a new game?\n";
            cin  <<  new_game;
After that I am just lost, so I need some help! Thanks in advanced ^_^ Also I was wondering if it's possible to but somesort of selection screen thingy in a console program? Like my example is a bootloader you can use your keyboard to move over certin selection things an then hit enter when you highlight the right thing.
Mizipzor
Mizipzor
One thing; you need to flip the << on std::cin, like this:

int new_game = 0;cout << "Wish to start a new game?\n";cin  >>  new_game;


Send stuff to cout to print to screen, cin sends stuff from the screen (or rather, the input buffer) to a variable.

If you want just a single character rather than a whole word, lookup _getch().

Now for the second question, yes its quite possible to do some more advanced rendering. For example, you can put the cursor at a specified place before printing. That, combined with commands like clearing the entire screen lets you do some fancy things.

But maybe that is a bit advanced for you right now, maybe you should try to do a text game without the above mentioned systems to grasp the basics?

Good luck! :)
ARC inc
ARC inc
Thanks for the fix, I just slapped it together. But still does not answer my question I am still trying to get it to know when the player types YES into the console it will begin a new game or if they type NO it will terminate the program.

Also I lookedup _getch() didn't help me much.

Thanks in advanced

also about the system thingy thanks for the info an yes I do relize it's above my head just wanted to know if it was possible.
joshuanrobinson2002
joshuanrobinson2002
First of all, I would give the user a set of valid responses (I like to use numbers, myself) that way they don't have to guess and you don't have to worry about the fact that "Yes" and "yes" and "y" and "Y" are not going to be the same thing. Just a recommendation feel free to ignore me completely.

So you'd have something like:

[source lang "cpp"]int new_game = 0;cout << "Wish to start a new game?" << endl;cout << "1. Yes" << endl;cout << "2. No" << endl;cin >> new_game;


To check valid input you'll need to use a conditional statement. Like an if-else or switch block.

[source lang "cpp"]if (new_game == 1){   //Do new game stuff here}else if (new_game == 2){   //exit?}else{   //User didn't give a valid response.}


I think that'll work, but you really should look into conditional statements. You're going to need them something fierce for almost every program you'll ever write. Whatever resource you're using to learn C++ should cover them.
kiwibonga
kiwibonga
I wouldn't recommend doing cin >> (some int) ever, as invalid input causes the program to go into an infinite loop... You're much better off storing all input in a std::string and then converting that to an int:

int command;std::string command_string;std::cout << "Would you like to start a new game?\n";std::cout << "1. Yes\n2. No\n\n> ";std::cin >> cmdstr;command = atoi(cmdstr.c_str()); // atoi takes a char pointer and returns an intcmdstr.clear();


If you get command == 0, it means the user either entered 0 or invalid input; so don't assign any commands to "0" and you should be fine.

This, by the way is one of the cases where using goto statements is probably the best way to go about things:

(label here)
get input
check if the input is something you expected
if not, jump back to the label, optionally printing something about the command being invalid so the user knows why he's being asked to enter something again...
Gage64
Gage64
Quote:
Original post by kiwibonga
This, by the way is one of the cases where using goto statements is probably the best way to go about things:

(label here)
get input
check if the input is something you expected
if not, jump back to the label, optionally printing something about the command being invalid so the user knows why he's being asked to enter something again...


This is easy to do without a goto:

bool valid = false;do {    std::cout << "Would you like to start a new game?\n";    std::cout << "1. Yes\n2. No\n\n";    int response;    cin >> response;    if ( (response == 1) || (response == 2) )        valid = true;    else        std::cout << "Invalid input, try again\n";} while (!valid);


I don't think recommending the use of a goto to a beginner is a particularly good idea.
Moonshoe
Moonshoe
I think it would be better if you just made new_game a char instead of int, because at least this way, if the user entered a letter or number, you can still catch it as an error, rather than the program going into an infinite loop if the user entered a character.

Another possible thing you can do is use while (cin.get() !='\n') after getting a character. This clears the input buffer after the first character is taken. So, if the user enters "yes" instead of "y," it will only take the "y" and clear out anything else after it.

So, you could try something like this.
#define CLRINPUT while (cin.get() !='\n')char new_game;cout << "Wish to start a new game? (Y/N): " << endl;cin >> new_game;CLRINPUT;new_game = static_cast<char>(toupper(new_game));// Check to see if input is valid before doing stuffwhile(new_game != 'Y' && new_game != 'N'){cout << "Invalid input.  Enter Y or N: ";cin >> new_game;CLRINPUT;}if(new_game == 'Y'){// Start a new game, etc.}if(new_game == 'N'){// Exit? Continue from last save?}
ARC inc
ARC inc
Quote:
Original post by Moonshoe
I think it would be better if you just made new_game a char instead of int, because at least this way, if the user entered a letter or number, you can still catch it as an error, rather than the program going into an infinite loop if the user entered a character.

Another possible thing you can do is use while (cin.get() !='\n') after getting a character. This clears the input buffer after the first character is taken. So, if the user enters "yes" instead of "y," it will only take the "y" and clear out anything else after it.

So, you could try something like this.
*** Source Snippet Removed ***


Thanks that worked perfectly!
ARC inc
ARC inc
Ok, need some new help now same project just different thing thought it would be best to put the question in here so anyways.

I am trying to make a small quiz system in my text based game it's really simple I know how to make the system it self but I want to use Header files and a seprate cpp file for the quiz then use(I am guessing here) threading to call the quiz parts when it's ready.

as of now here is the code for my main.cpp
//Text Based Game#include <iostream>#include <windows.h>using namespace std;int main(){    SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),10);        //Initialzation of Program    cout << "********************************************************\n";    cout << "********************     Ever Life  ********************\n";    cout << "********************  Version 1 Test *******************\n";    cout << "********************************************************\n";        #define CLRINPUT while (cin.get() !='\n')        char new_game;    cout << "Do you wish to start a new game? (Y/N:) " << endl;    cin >> new_game;    CLRINPUT;        new_game = static_cast<char>(toupper(new_game));        //check to see if input is vaild before doing stuff    while(new_game != 'Y' && new_game !='N')    {    cout << "Invaild input. Enter Y or N: ";    cin >> new_game;    CLRINPUT;}if(new_game == 'Y'){     cout << "Welcome to Ever Life!\n";     cin >> new_game;}if(new_game == 'N'){    cout << "Have a nice day!\n";    cin >> new_game;} if(new_game == 'C'){     system("Close");   }      return 0;}


I know how to start a header file

quiz.h
#indefine QUIZ_H#define QUIZ_H//main code here#endf


but thats all I really can get to work, I am kinda stuck so help would be apprechiated!

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.