Thread: Text based game

  1. #1
    Registered User
    Join Date
    Mar 2009
    Posts
    39

    Text based game

    I am working on a very simple text based game, but I am having a problem.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    int main(int argc, char *argv[])
    {    /*variables*/
        char name[101], help, action;
        int php=50, ncphp=50;
        srand(time(NULL));
         /*end variables*/
         
        printf("Please enter your name: ");
        fgets (name, 101 , stdin);
        printf("Hello, %s" , name);
        printf("Have you played this game before? (Y or N) ");
        while((help = tolower(getchar())) && help != 'y' && help != 'n');
                   if(help == 'n'){
                   printf("Type the letter of the action you wish to perform, the object of the game is to kill the guy.");
                   }
        do{
        printf("What would you like to do?\n[A]ttack\n[H]eal\n[R]un\n");
        }
    }
    I am getting a syntax error on the last '}'.
    Can someone tell me what the problem is so far?
    PS. Yes, I do know this will end after the last printf; I'm not done yet.

  2. #2
    Registered User Maz's Avatar
    Join Date
    Nov 2005
    Location
    Finland
    Posts
    194
    your do seems to lack of a condition.

  3. #3
    Registered User
    Join Date
    Mar 2009
    Posts
    39
    I didn't think do needed a condition. How can I make it so it executes that code no matter what?

    Edit: nvm, I fixed the code.
    Last edited by wipeout4wh; 03-25-2009 at 08:18 PM.

  4. #4
    Registered User
    Join Date
    Mar 2009
    Posts
    39
    Also, what code would I use to clear the console screen?

    Edit: nvm, got this too.
    Last edited by wipeout4wh; 03-25-2009 at 08:21 PM.

  5. #5
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    You have a semi-colon at the end of your while( ... ) line of code. Your indentation makes it look like you wanted the following 2 lines of code to run inside the while loop.

    But it's not going to do it with that semi-colon in the way.

    The easiest way to clear the console screen is to just printf("\n\n\n\n\n\\n\n\n\n\n\n..."), about 30 newline's.

    The elegant way is to use the console API, which is part of Windows, rather than C, and I have never messed with it.

  6. #6
    Registered User
    Join Date
    Mar 2009
    Posts
    39
    I figured out both of my other questions, but now I have a new problem.
    BTW you can just type system("cls"); to clear screen.


    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    int main(int argc, char *argv[])
    {    /*variables*/
        char name[101], help, action, play;
        int php = 50, npchp = 50, dam, heal;
        srand(time(NULL));
         /*end variables*/
    
        printf("Please enter your name: ");
        fgets (name, 101 , stdin);
        printf("Hello, %s" , name);
        printf("Have you played this game before? (Y or N) ");
        while((help = tolower(getchar())) && help != 'y' && help != 'n');
                   system("cls");
                   if(help == 'n')
                   printf("Enter the letter of the action you wish to perform, the object of this game is\nto kill the guy before he kills you.\n");
        while(play == 'y'){
        printf("\nWhat would you like to do?\n[A]ttack\n[R]un away\n");
               while((action = tolower(getchar())) && action != 'a' && action != 'h' && action != 'r');
                      if(action == 'a'){
                                system("cls");
                                dam = (rand()%5)+1;
                                printf("You hit the guy for %i damage.\n", dam);
                                npchp = npchp - dam;
                                printf("The guy now has %i health\n\n", npchp);
                                if(npchp < 1){
                                         system("cls");
                                         printf("Congratulations, %sYou killed the guy!\n", name);
                                         printf("Would you like to play again? (Y or N)\n");
                                         while((play = tolower(getchar())) && play != 'y' && play != 'n');
                                                     if(play == 'n')
                                                     return 0;
                                                     if(play == 'y')
                                                     return 1;
                                }
                                dam = (rand()%5)+1;
                                printf("The guy hits you for %i damage.\n", dam);
                                php = php - dam;
                                printf("You now have %i health\n\n", php);
                                if(php < 1){
                                       system("cls");
                                       printf("The guy killed you, you lose!");
                                       printf("Would you like to play again? (Y or N)\n");
                                       while((play = tolower(getchar())) && play != 'y' && play != 'n');
                                                   if(play == 'n')
                                                   return 0;
                                                   if(play == 'y')
                                                   return 1;
                                }
                      }
                      if(action == 'r'){
                                system("cls");
                                printf("You run away.\nYou didn't kill the guy, you lose.");
                                       printf("Would you like to play again? (Y or N)\n");
                                       while((play = tolower(getchar())) && play != 'y' && play != 'n');
                                                   if(play == 'n')
                                                   return 0;
                                                   if(play == 'y')
                                                   return 1;
        }
    }
    Here is my code, I do not have enough "}" but I can't figure out where.
    Last edited by wipeout4wh; 03-25-2009 at 09:08 PM. Reason: new code

  7. #7
    Registered User
    Join Date
    Mar 2009
    Posts
    39
    Wow...nvm, I figured that out too, but is there a code to return to the beginning of the script? I thought return 1; would, but it doesn't

  8. #8
    Tha 1 Sick RAT
    Join Date
    Dec 2003
    Posts
    271
    you need to revise your program properly. You need to construct your logic properly. place the whole program in a logical loop and check for the exit condition at the end of the of the program each time.
    A hundred Elephants can knock down the walls of a fortress... One diseased rat can kill everyone inside

  9. #9
    Registered User
    Join Date
    Mar 2009
    Posts
    39
    I would practically have to rewirite the whole thing to do that, isn't there just one command I can use?

  10. #10
    int x = *((int *) NULL); Cactus_Hugger's Avatar
    Join Date
    Jul 2003
    Location
    Banks of the River Styx
    Posts
    902
    Quote Originally Posted by wipeout4wh View Post
    I would practically have to rewirite the whole thing to do that, isn't there just one command I can use?
    No, you wouldn't. You just need to learn functions and how to structure a program better. Once you learn functions, it should be easy enough to move most of your code out of main into a sort of "game" function, and call that repeatedly from main.
    (And from there, break that enormous function apart into better functions. Trust me - you do not want to build an entire program in a single function.)
    long time; /* know C? */
    Unprecedented performance: Nothing ever ran this slow before.
    Any sufficiently advanced bug is indistinguishable from a feature.
    Real Programmers confuse Halloween and Christmas, because dec 25 == oct 31.
    The best way to accelerate an IBM is at 9.8 m/s/s.
    recursion (re - cur' - zhun) n. 1. (see recursion)

  11. #11
    Registered User
    Join Date
    Mar 2009
    Posts
    39
    So how would I write that, and still have it ask to play again in the same places?
    If I just put that whole thing in a loop, it will try to finish the code before restarting.

  12. #12
    Registered User
    Join Date
    Mar 2009
    Posts
    39
    Ok, I figured out how to make it restart without a loop. I'm done with the game now, but how can I assign an icon to the .exe?

  13. #13
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    To do it internally, your icon needs to be linked into the executable. Most Windows IDE's should have a GUI method of doing that. So be aware that for that reason, in addition to the system("cls") call, your code will only work on Microsoft systems. You might not even care - but its worth mentioning.

    Of course, windows can do it externally, but if you distributed your program, I'm almost certain that the icon doesn't go with it.

    edit: You have a lot of "nevermind" posts. Try planning things out more, and trying to find an answer on your own before you post. If you have to make several corrections to a post, you can also edit it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Text based game
    By beene in forum Game Programming
    Replies: 10
    Last Post: 05-12-2008, 07:52 PM
  2. New Project, text game, design stage.
    By Shamino in forum Game Programming
    Replies: 9
    Last Post: 05-23-2007, 06:39 AM
  3. easiest way to make text based game?
    By oceanrats in forum C++ Programming
    Replies: 7
    Last Post: 08-13-2005, 02:17 PM
  4. Saving a text game
    By Unregistered in forum C++ Programming
    Replies: 4
    Last Post: 03-27-2002, 01:33 PM
  5. Text Based Game
    By drdroid in forum C++ Programming
    Replies: 2
    Last Post: 02-18-2002, 06:21 PM