PDA

View Full Version : How to think, when you program?



Tynnhammar
05-21-2005, 05:45 AM
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"

Salem
05-21-2005, 06:35 AM
This is a small idea of how you might join a number of rooms together
http://cboard.cprogramming.com/showthread.php?t=21974&highlight=RPG

For each "door" ( int next[4]; ), you could add a number of properties, say


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

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


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).


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.

Tynnhammar
05-21-2005, 06:51 AM
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:


int main()
{
intro();
charcreator();
battle();

}


?

And again thanks for the great post!

Quantum1024
05-21-2005, 06:59 AM
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.

Tynnhammar
05-21-2005, 09:05 AM
Well, it was just an example, but I think I had my question answered anyway, thanks! :)