#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    1
    Rep Power
    0

    Is this the most efficient it can be?


    Sort of new to these forums so but somethings has been "bugging" me.

    I am making a text based game(for kicks), but the only way to navigate seems to require hard coding for each room.

    Code:
    //East Move
            if(strncmp(north, cmd, 5)==0){
                switch (room) {
                    case 0:
                        room = 2;
                        printf("You have moved to the east\n");
                        break;
                        
                    case 1:
                        printf("You cant move east\n");
                        break;
                        
                    case 2:
                        printf("you are already in the East\n");
                        break;
                        
                    case 3:
                        room = 0;
                        printf("You have moved to the center\n");
                        break;
                        
                    case 4:
                        printf("You cant move east\n");
                        break;
                        
                    default:
                        break;
                }
            }

    I keep feeling like there might be a way to do this with arrays dynamically, but I'm to new to c to tell.

    Does anyone know of examples of this or ideas for improvement?

    Thanks.
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    71
    Rep Power
    2
    Originally Posted by DarkAngels
    Sort of new to these forums so but somethings has been "bugging" me.

    I am making a text based game(for kicks), but the only way to navigate seems to require hard coding for each room.

    Code:
    //East Move
            if(strncmp(north, cmd, 5)==0){
                switch (room) {
                    case 0:
                        room = 2;
                        printf("You have moved to the east\n");
                        break;
                        
                    case 1:
                        printf("You cant move east\n");
                        break;
                        
                    case 2:
                        printf("you are already in the East\n");
                        break;
                        
                    case 3:
                        room = 0;
                        printf("You have moved to the center\n");
                        break;
                        
                    case 4:
                        printf("You cant move east\n");
                        break;
                        
                    default:
                        break;
                }
            }

    I keep feeling like there might be a way to do this with arrays dynamically, but I'm to new to c to tell.

    Does anyone know of examples of this or ideas for improvement?

    Thanks.
    Well anything done statically will probably be done more efficiently than dynamically. Calling for more memory from the operating system is not the best way to achieve efficiency.
  4. #3
  5. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,376
    Rep Power
    1871
    Consider something like my 3 room adventure
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper
  6. #4
  7. I'm Baaaaaaack!
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Jul 2003
    Location
    Maryland
    Posts
    5,538
    Rep Power
    243
    My sig has a writeup regarding performance, it might be worth your while to read.

    Switch statements tend to be exceptionally fast as implemented (often done with jumps) so adding on isn't really a performance consideration. Having said that, you would be hard pressed to measure the increased performance in any sort of real-world situation, so you should focus on maintainability rather than absolute performance. Having given this exact same topic some deep thought (I am myself embarking on a game development effort, see http://sol-biotech.com/wordpress/201...s-just-a-game/ if you are curious) I decided to use a database to store the information, actions and state rather than code it all. That allows for dramatically increased flexibility and maintainability at the cost, of course, of some performance. However, since these games are all waiting for human input, speed is largely irrelevant unless you are intent on freeing up cycles for some other process running on the machine.

    Once you have done this for a while, you realize that maintenance is at least 90% of the cost of any project and I can promise you, even if you lack database skills at present, maintenance of a set of tables is an order of magnitude easier than maintaining the same information in program logic.

    My blog, The Fount of Useless Information http://sol-biotech.com/wordpress/
    Free code: http://sol-biotech.com/code/.
    Secure Programming: http://sol-biotech.com/code/SecProgFAQ.html.
    Performance Programming: http://sol-biotech.com/code/PerformanceProgramming.html.
    LinkedIn Profile: http://www.linkedin.com/in/keithoxenrider

    It is not that old programmers are any smarter or code better, it is just that they have made the same stupid mistake so many times that it is second nature to fix it.
    --Me, I just made it up

    The reasonable man adapts himself to the world; the unreasonable one persists in trying to adapt the world to himself. Therefore, all progress depends on the unreasonable man.
    --George Bernard Shaw
  8. #5
  9. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,837
    Rep Power
    480

    "Ulysses!"


    I made it through zork to the endgame. My map of thirsty passages is long gone. The structure I present here won't handle that maze in which heading east then west doesn't return you to where you started. Salem's post at 02:35 AM is a linked list that will follow many sorts of craziness.

    DarkAngels, obviously you'd have to write code that writes code to change the layout, and recompile.

    Here's yet another solution. The advantage of this is that the map is obvious in the file, and it's easy to change.
    Code:
    #include<time.h>
    #include<stdio.h>
    #include<stdlib.h>
    
    /*
      This code works for castles having 1 or 2 floors
      or castles without stairs.  The castle must show exterior  walls.
    
      Mean of about 690 random steps to termination,
      which is usually wealth.
      An exact figure can be found by Markov chain.
    */
    
    static char*castle[2][7] = {
      {                    /*{  FIRST FLOOR       */
        "wwwwwwww",	       /*  "wwwwwwww",        */
        "w   w sw",	       /*  "wO  w sw",      k,j,i start point at O */
        "w   w  w",	       /*  "w   w  w",        */
        "w      w",	       /*  "w      w",        */
        "w    www",	       /*  "w    www",      w marks wall */
        "wwwww",           /*  "wwwww"},          */
        NULL
      },
    
      {		       /*{  SECOND FLOOR      */
        "wwwwwwww",	       /*  "wwwwwwww",        */
        "w     sw",	       /*  "w     sw",      s stairs */
        "www wwwwwwwww",   /*  "www wwwwwwwww",   */
        "w          xw",   /*  "w          xw", x death trap */
        "w   wtwwwwwww",   /*  "w   wtwwwwwww", t treasure */
        "wwwww ww",	       /*  "wwwww ww",      cell never reached */
        "wwwwwwwwwww"      /*  "wwwwwwwwwww"      */
      }		       /*}                    */
    };
    
    static int location[3];
    
    #define I (location[0])
    #define J (location[1])
    #define K (location[2])
    
    #define C (castle[K][J][I])
    
    int rand4(void) {
      /*
        return a random number [0-3] using
        16 bits per each call to rand()
      */
      static int n = 0, r;
      int r4;
      if (! (n%8))
        r = rand();
      r4 = r & 3;
      ++n;
      r >>= 2;
      return r4;
    }
    
    int feel(char direction,char goal) {
      char c;
      switch (direction) {
      case 'n': --J; c = C; ++J; break;
      case 'e': ++I; c = C; --I; break;
      case 's': ++J; c = C; --J; break;
      case 'w': --I; c = C; ++I; break;
      default:
        fputs("\nprogram error\n",stderr);
        exit(1);
      }
      return c == goal;
    }
    
    char step_drunkenly(void) {
      char c;
      do c = "nesw"[rand4()];
      while (feel(c,'w'));
      return c;
    }
    
    void move(void) {
      switch (step_drunkenly()) {
      case 'n': --J; break;
      case 'e': ++I; break;
      case 's': ++J; break;
      case 'w': --I; break;
      default:
        fputs("\nprogram error\n",stderr);
        exit(1);
      }
      K += ('s' == C) * (1 - 2*K);	/* on stairs, take them */
    }
    
    int dead(void) {
      return 'x' == C;
    }
    
    int wealthy(void) {
      return 't' == C;
    }
    
    int incomplete(void) {
      return ! (dead() || wealthy());
    }
    
    void report(int n) {
      /*
        print a report in standard form.
        The number of steps displays first
        to simplify statistical analysis
      */
      char*s;
      if (n < 0) s = "starved";
      else if (dead()) s = "never seen again";
      else if (wealthy()) s = "Ya ha deedle deedle, bubba bubba deedle deedle dum.";
      else s = "program failure";
      printf("%d steps: %s\n",n,s);
    }
    
    int main() {
      int turns;
      srand(time(NULL));
      K = 0, J = 1, I = 1;
      for (turns = 0; (0 <= turns) && incomplete(); ++turns)
        move();
      report(turns);
      return 0;
    }
    [edit]tag apologies[/edit]

    Comments on this post

    • mitakeet disagrees : Where are your code tags?
    Last edited by b49P23TIvg; October 4th, 2012 at 12:22 PM.
    [code]Code tags[/code] are essential for python code and Makefiles!

IMN logo majestic logo threadwatch logo seochat tools logo