need a little help with a program i am making. should be easy for you masters.

i am learning c++ and i think so far i am doing pretty well. i think i learn better when i have an objective so i decided to make a little game, kinda an old single user dungeon style game cos i like those. anyway i have a bunch of parts made up some problem have come up i have managed to get through them and fix all the major bugs and snags. but i have come to one part and i have a bug in it and i just cant get the damn thing to work right. here is the code.

that is a pretty big block o' code. anyway the bug is that i want the damn thing to stop when your or the monster's health is less then or equal to 0. as i am sure you people know that in a game if you have 0 health you are dead and when you are dead you cant really fight all that well. but the problem arises only some times, evey couple of times i go through it my health will keep falling down to like -14, -20, -34. and i cant stop the damn thing. i really like puzzles and my favorit part of programing is fixing all the bugs, its kinda a giant puzzle. and puzzles == fun. so if you people could maybe go through this and tell me how the hell to fix this bug i would love you for it.

also i have one more question that maybe some one could awcer for me. so far i am only making the templets for things like random encounters, combat systems, traveling, random loot, tand things like that. but i want to know the best way to connect them. should i make them into a project or as one huge *** single code monster. and if the acwer == use the project thing could you please give me a little hint on how the hell that works... thanks.

> the bug is that i want the damn thing to stop when your or the monster's health is less then or equal to 0

Have another look at the while and think about whether that expression will evaluate TRUE or FALSE in each of the four cases H>0 M>0, H<=0 M>0, H>0 M<=0, H<=0 M<=0, bearing carefully in mind the OR truth table.

> also i have one more question...

if you play a lot of these games you'll know that the simpler ones use randomisation to determine what happens where and when (e.g. wump, but even there the maze is setup at random at the start and you then find your way around it), but in the larger ones the whole thing is much more designed, so that you always know for example that you're going to hit a Balrog in the Mines Of Moria; you're never going to encounter him in Rivendell. There may be a degree of randomisation but that is more likely to be confined to the strengths of individual enemies, the effectiveness of each of your attacks, the amount of treasure each dead Orc drops, etc etc.

So what this means is that you'll need a database of what happens where, into which you'll code the overall design of the game. How you code the database is up to you; you could just use flat files or perhaps use one of the existing database products. Then as the user moves from point to point within the "maze" you lookup what's in the database at that point.

This of course means that you'll have to design the game itself and the starting point will need to be some sort of map. Dead simple one:

Code:

1 5
: :
: :
START- - -3- - -4- - -END
:
:
2

So we have 6 points. We start at START and the exits are N,S,E. Maybe there's some stuff to pickup here. You need some way to remember what you've picked up (inventory).
Let's say 1 is a trap and you drop dead - bottomless pit perhaps, and 2 contains a monster that you have to kill but nothing else Seasoned players will know there's no point heading south from START.
Maybe 3 contains some kind of puzzle you have to solve to open the way east from 4. So at 4 you'd check if the problem was solved and say "the exits are NORTH, WEST" if not, or "NORTH,WEST,EAST" if it was.
Let's say there's another monster at 5.
Maybe END is really the end or you have some supermonster/superpuzzle to dispatch before you win.

So the database/data structure will need lots of links; START needs 3 exits, to 1,2,3. 3 has 2 exits to START,4. You need to think about how to represent the two-way link between 3&4, and in the design if these really are two way links; maybe if 3->-4 is one way then if you don't solve the puzzle before moving east you're completely stuffed. And so on.
Each point will need a number of attributes. What is there and how it benefits (or otherwise) you.

xpi0t0s i dont think i have hurd from you in this forum yet. and its always good to meet some one of skill. i am the locksmith ^^ nice to meet you. i think that you have some good ideas there. i was thinking about doing it that way but have no idea how, i think i need to learn a little more before i can tackle that one. but i have been making almost a template for all the things that will be in the game, such as shops, the character gen, and even screwing around with the combat systems as you can see. i think i am going to scrap that idea. i have no idea what i was thinking on that one. i wont one that will give you a little more option... like use items, run, magic... i think i must have had something wrong with me to think that last one was a good idea... well it gave me something to do on my spare at school. ^^, lol. well i am really not sure, not sure?! i mean damn clueless about this data base thing, could i maybe do it like; (using you map from above.)

The data structure is just a set of data that describes the game design, and what you would aim to do typically is to make the game code as generic as possible and put the game design into the database. Giving my layout some thought, I came up with the following possible data structures. This is very extensible so you could easily use it as the basis for a real game:

Code:

int traps[]={0,1,0,0,0,0,0}; // 1=bottomless pit
// Directions encoded as what locations are NSWE per position. X=no exit that way.
// Since we only have single digit location IDs we can just use a single character 0-6.
// START=0 and END=6. Y is X or 6 depending if the puzzle at 3 is solved; we'll handle that in code.
char *directions="0:12X3 1:X0X3 2:0XXX 3:XX04 4:5X3Y 5:X4XX 6:XX4X"; // NSWE
const int MAXINV=20;
int inv[MAXINV];
int inv_next=0; // Inventory. inv_next is next free slot, let's say we can only have 20 items.
int monsters[]={0,0,1,0,0,2,0}; // what monsters are at what positions. 1=imp, 2=balrog. 0=no monster
int monster_strength[]={0,10,20}; // monster info for battle scenes
int monster_skill[]={0,10,100}; // ditto
int puzzles[]={0,0,0,1,0,0,0}; // what puzzles are at what positions
int solved[2]; // init to zero in the program. When we solve puzzle N, set solved[N] to 1.
int benefits[]={1,0,0,0,0,0,2}; // what player benefits are at what positions. 1=axe, 2=exit teleport
char *descriptions[]={ // Position descriptions
"Waking from the teleport you are in a bit of a daze etc blah blah set the scene and so on.\n"
"There is an axe here.\n",
"You trip and fall into a bottomless pit. Since it's bottomless what you'll\n"
"actually die of is thirst, rather than hitting the bottom, although you might\n"
"knock yourself out on the sides. Your game is over.\n",
"Whoops! As you walk through the forest you accidentally step on a sleeping Imp's\n"
"tail and wake him up. He's not impressed, so now you have to fight\n",
"You find a piece of paper with the words MENE MENE TEKEL PARSIN written on them.\n"
"It is asking for a name and two numbers. What are they?\n",
"Nothing interesting\n",
"Oh, I dunno. A Balrog, perhaps.\n",
"You found the exit teleport!\n"
};

And some code. Drop this into your favourite hello world program and just call go4e_15112().
As you see it's all fairly straight forward, there's just a lot of it.
Actually I didn't use benefits[], I just coded them directly. If you have a look at the way I process commands like NORTH, SOUTH, EAST and WEST you'll see what I mean by using generic code and implementing the details in the data structure. The code handles the possibility of going in any of the four directions from any point(which is what is meant by generic); it's the contents of the data structure that determines which way you can go. So let's say you have a new position 7 south of 3 with again "nothing interesting". Add "nothing interesting" to descriptions[] and change directions to:
char *directions="0:12X3 1:X0X3 2:0XXX 3:X704 4:5X3Y 5:X4XX 6:XX4X 7:3XXX"; // NSWE
So in this way we have extended the game with no code changes at all (well, ok, technically we have but if directions and descriptions were read in from a file instead then all we would have changed was that file).

It might be nicer to handle different parts of the main function in other functions. For example have a traps function, and a puzzles function and so on. Then the main function will be more readable and can focus on the overall program logic, leaving the details of the different parts of the game to sub-functions.
So you might end up with something like:

ah, i think i am starting to understand what you are saying. i took your map and i made this little test. i have no combat system made yet so you will have to use your imagination s little but i think as a first test it is not too bad. and as a bonus i have not found any bugs in it ^^. so take a look and tell me what you think. abd please if you do find an bugs or error tell me. oh, and also if you think i should do somthing differently then please do tell, k?

here is the code...

Code:

#include <iostream>
using namespace std;
int here = 1;
int dir;
int a = 1;
int main()
{
cout<<"this is a test of the new game idea... it is very complecated and i bet it will fail"<<endl;
do{
cout<<endl;
cout<<endl;
cout<<"you find yourself standing in on a path, from here the path brakes"<<endl;
cout<<"off in three ways. the path is a simple dirt path maybe an old "<<endl;
cout<<"traders path. the exits are 1)north 2)east 3)south."<<endl;
cout<<"witch way should i go?"<<endl;
cin>>dir;
if(dir == 1){
here = 3;
cout<<"you went north"<<endl;
}
else if (dir == 2)
{
here = 4;
cout<<"you went east"<<endl;
}
else if(dir == 3){
here = 2;
cout<<"you went south"<<endl;
}
if (here == 2)
{
cout<<endl;
cout<<"you follow the path till it comes till it comes to its end at a cliff face."<<endl;
cout<<"you hear a nosie and turn to look... you see a small humanoid lizard that stands about 3 foot tall, he holds up a short spear and yells somthing in a crud luangage that you cannot understand."<<endl;
cout<<endl;
cout<<endl;
cout<<"this is where i would have the combat thing stand but i do not have it done yet.(note to self, make the combat thing.)"<<endl;
cout<<endl;
cout<<endl;
cout<<"exits now are 1)north"<<endl;
cin>>dir;
}
else if (here == 3)
{
cout<<endl;
cout<<"you walk down the path for about ten min, all is peacful and quite. birds churp from up in the trees. you can smell the sweet sent of wild flowers that inhabit the forest floor around you. all of a sudden you step into a hidden pit trap and you brake both your legs, there you spend 2 weeks untill you relize it was a low level illusion spell."<<endl;
cout<<endl;
cout<<endl;
cout<<"exits now are 1)south"<<endl;
cin>>dir;
}
else if (here == 4)
{
cout<<endl;
cout<<"you start on the path to begain your adventure."<<endl;
cout<<endl;
cout<<endl;
cout<<"exits now are 1)west 2)east"<<endl;
cin>>dir;
if (dir == 1){
here = 1;
}
else if (dir == 2){
here = 5;
}
}
if (here == 5){
cout<<endl;
cout<<"you continue down the path, you can see smoke off in the distance to the east."<<endl;
cout<<"exits are 1)west 2)east"<<endl;
cin>>dir;
}
if (dir == 1){
here = 4;
}
else if (dir == 2){
here = 6;
}
if (here == 6){
cout<<endl;
cout<<"you find a small incampment that looks like it had been able to support about 25 people. all the tents are ablaze, and all the supplys look to have been rifled through. as you explore the camp you find foot prints that look too large to be human leading north also what looks to be bloody drag marks leading the same way"<<endl;
cout<<"the exits are 1)west 2)north."<<endl;
cin>>dir;
if(dir == 2){
cout<<endl;
cout<<"you go north"<<endl;
cout<<endl;
cout<<endl;
here = 7;
}
cout<<"----------------------------------------------------"<<endl;
if(dir == 1){
cout<<endl;
cout<<"you go back west"<<endl;
cout<<endl;
cout<<endl;
cout<<"----------------------------------------------------"<<endl;
}
if (here == 7){
cout<<endl;
cout<<"you head north following the tracks and blood. you keep an eye out for what ever did this. you walk for around ten min and you find your self standing in front of a cliff."<<endl;
a=2;
}
}
}while (a == 1);
cout<<endl;
cout<<endl;
cout<<endl;
system("pause");
return 0;
}

I haven't run it yet; short on time this morning. Yes it's good, but coding the whole game up in this way will lead to (a) a very complicated program with lots of repeated code that's difficult to debug and (b) it's not easy to extend the game. Compare what you would have to do to add position 7 and the small amount I had to do, which was a change to data only and no code change. Also you have combat at position 2, now suppose you've repeated that code at 20 other locations, then you find a bug in one of them; you have to find all 21 occurrences of that bug and fix them all, as opposed to my generic combat routine where the bug will only need to be fixed once.

Imagine how large your program will be if you extend this up to a typical adventure game with hundreds of nodes (FF gamebooks often have over 500 numbered paragraphs).

> while (a == 1)

Oh, and use more sensible variable names. Try looking through 100,000 lines of code for a variable named "a" and you'll be swearing at the original programmer long before you get even halfway. cf:

No, C counts from ZERO, not 1, so int traps[]={0,1,0,0,0,0,0}; puts the bottomless pit at 1, not at 2.
Compare int arr[]={0,1,2,3,4,5} which creates a 6 item array with each element equal to its index.

The game knows that the pit is at 1 because we just lookup traps[pos] where pos is the player's position. If pos is 1 then traps[pos] is 1, otherwise traps[pos]=0. Essentially I'm using the array index as one part of a 1-1 mapping 0->0; 1->1; 2->0; 3->0 etc.

> int traps[]={0,1,0,2,0,0,0}; // 1=bottomless and pit 2= arrow trap

Similarly no, this puts the bottomless pit at 1 and the arrow trap at 3, and the code if an arrow trap just reduces your health by 3 whenever you visit it would be:

!1: remember the difference between ASSIGNMENT = and COMPARISON ==. BASIC uses = for both and deduces the meaning from the context, because it doesn't allow an assignment within an expression context, but in C an assignment is a valid expression and so the two different operators are necessary. An assignment as an expression is usually a shortcut; you can shorten:

sir, is there anyway you could post a working runnable example of what you are tinking. i think i could get more out of it if i could see it work, and be able to screw around with it a little. i think that would help a lot in my understanding of this.

so it did not work. i use dev-cpp, if that means anything. could it be just that we are using different compilers and your has a little bit of a nich to do stuff a little different then mine does? or somthing like that. i really have no idea.^^,

Oh, that just means you've compiled it as a Windows program instead of as a DOS program. The entry point for Windows programs is WinMain, and mine of course is a console application. None of the printfs will work if it's a Windows function; you need to use considerably more complicated GUI drawing tools if you want to write it as a Windows program (Hint: you don't).