How to think, when you program?

This is a discussion on How to think, when you program? within the A Brief History of Cprogramming.com forums, part of the Community Boards category; Greetings! I have done some basic PHP and C++ programming, like a guestbook and a small text adventure. However, I ...

  1. #1
    Interested Newbie
    Join Date
    Sep 2004
    Location
    Sweden
    Posts
    51

    How to think, when you program?

    Greetings!

    I have done some basic PHP and C++ programming, like a guestbook and a small text adventure.

    However, I felt when I did the text adventure that I programmed wrong and old. The program was running from the top of the code and all the way to the bottom, very undynamic in my opinion.

    This might be the case, that "real" programming is actually like this but I doubt it. That's why I would like to hear what you think, how would you organize a small text adventure?

    Thank you,

    // "Interested Newbie"

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,821
    This is a small idea of how you might join a number of rooms together
    Using a huge 3D array

    For each "door" ( int next[4]; ), you could add a number of properties, say
    Code:
    enum {
      DOOR_NORMAL,
      DOOR_MAGIC_BLOCKED,  // needs a spell to remove the magic
      DOOR_TRAP,  // takes you to a random place
    }
    So for example, where you would previously do this
    [code]room_num = rooms[room_num].next[SOUTH];]/code]

    You would then have
    Code:
    if ( rooms[room_num].door_type[SOUTH] == DOOR_MAGIC_BLOCKED ) {
      printf( "The way is magically blocked!\n" );
    else if ( rooms[room_num].door_type[SOUTH] == DOOR_TRAP ) {
      room_num = rand() % MAX_ROOMS;
    } else {
      room_num = rooms[room_num].next[SOUTH];
    }
    In your spell casting code, you'd have
    Code:
    rooms[room_num].door_type[SOUTH] = DOOR_NORMAL;
    To each room, add
    - a list of objects you can use
    - a list of characters you can interact with

    Objects would be broken down into major and minor types
    Major types: weapons, armour, spells, potions, keys, amulets, ...
    Minor types: sword, axe, bow, ...

    Each major type has attributes say damage, range, accuracy. Use these to decide how much damage is done to an opponents.
    Eg. sword has high damage and low accuracy, a bow has low damage and high accuracy.

    So an example attack with a bow might read
    You shoot and hit the ogre
    You shoot and hit the ogre
    You shoot and miss, the ogre draws nearer
    You shoot and hit the ogre
    You shoot and hit the ogre
    You shoot and hit the ogre, the ogre is dead!

    Whereas with a sword, it might read (because of the low accuracy, and close range)
    You swing and miss the ogre, the ogre hits you
    You swing and miss the ogre, the ogre hits you
    You swing and miss the ogre, the ogre hits you
    You swing and miss the ogre, the ogre hits you
    You strike a solid blow and the ogre is no more!

    Characters can be categorised as friendly, neutral or hostile. Friendly characters will tell you things, give you things, and perhaps help you in battles.

    The whole point is to make sure you design the appropriate data structures. As you can see from my original example, all of the information is in the rooms array (which can be read from a file at runtime). All the code knows is how to get from room to room (in essence, a simple assignment).
    Code:
    direction = whichDirection();  // ask which direction to move in
    room_num = rooms[room_num].next[direction];
    printf( "You are in the %s\n", rooms[room_num].name );
    Make a small program around that, and just practice walking around the small map provided.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Interested Newbie
    Join Date
    Sep 2004
    Location
    Sweden
    Posts
    51
    Thanks alot! Very interesting and long post indeed!

    However, I was thinking more like if I was doing right when my programs goes about like this:
    Code:
    int main()
    {
              intro();
              charcreator();
              battle();
    
    }
    ?

    And again thanks for the great post!

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    847
    Quote Originally Posted by Tynnhammar
    Code:
    int main()
    {
              intro();
              charcreator();
              battle();
    
    }
    There'sn't nothing wrong with that if you want the game to end after one battle (assuming battle() returns after one battle) or you could use a game loop wich continues untill the player choses to end.

  5. #5
    Interested Newbie
    Join Date
    Sep 2004
    Location
    Sweden
    Posts
    51
    Well, it was just an example, but I think I had my question answered anyway, thanks!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Issue with program that's calling a function and has a loop
    By tigerfansince84 in forum C++ Programming
    Replies: 9
    Last Post: 11-12-2008, 01:38 PM
  2. Need help with a program, theres something in it for you
    By engstudent363 in forum C Programming
    Replies: 1
    Last Post: 02-29-2008, 01:41 PM
  3. Replies: 4
    Last Post: 02-21-2008, 10:39 AM
  4. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 11:04 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21