Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C++ (http://www.go4expert.com/forums/cpp/)
-   -   need a little help with a program i am making. should be easy for you masters. (http://www.go4expert.com/forums/little-help-program-i-easy-masters-t15112/)

cpulocksmith 12Nov2008 10:26

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

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.
Code:

#include <iostream>
using namespace std;

int monhealth=20;
int health=30;
int monster;//the monster you are fighting
int you;//the player
int monarmor = 14;
int yourarmor = 16;
int monstr = 2;
int ystr = 2;
int mondamage;
int damage;
int attack;

int main()


{
              cout<<"you are in combat?! what the hell!!!"<<endl;
              cout<<endl;
              system("pause");
    do {
    srand(static_cast<unsigned int>(time(0)));
              monster = rand()%20+1;
              mondamage = rand()%6+1;
              damage = rand()%6+1;
              attack = rand()%20+1;
              cout<<endl;
              cout<<endl;
              cout<<"----------------------------------------------------"<<endl;
              cout<<"monsters attack is "<< monster + monstr<<endl;
              cout << "the monster swings an attack at you and ";// will change for other monsters
              if (monster > yourarmor)
              {
                          cout <<"and racks it across your leg "<<endl;//will add other stuff for other monsters 
                          health = health - mondamage;
                          cout<<endl;
                          }
                          else if (monster + monstr < yourarmor || monster + monstr == yourarmor)
                          {
                                cout<<"you manage to dodge the monsters attack"<<endl;
                                cout <<endl;
                                }
                                else if (monster == 1)
                                {
                                    cout <<"the monster hits himself in the leg"<<endl;
                                    monhealth = monhealth - mondamage;
                                    }
                                cout<<endl;
                                cout<<endl;
                                cout<<"----------------------------------------------------"<<endl;
                                cout<<"your health is currently  " <<health<<"  and it is now time for your attack."<<endl;
                                cout<<"press anykey to roll your attack"<<endl;
                                cout<<endl;
                                system("pause");
                                cout<<endl;
                                cout<<endl;
                                cout<<"you swing your sword at the beast and ";
                                if (attack + ystr > monarmor)
                                {
                                          cout<<"you strike him square in the cheast"<<endl;
                                          monhealth = monhealth - damage;
                                          }
                                          else if (attack + ystr < monarmor)
                                          {
                                                cout<<"you miss, shit..."<<endl;
                                                }
                                                else if (attack = 1)
                                                {
                                                    cout<<"you smash yourself in the leg"<<endl;
                                                    health =health - damage;
                                                    }
}while ( health > 0 || monhealth > 0 );
if (health < 0)
{
          cout<<"you are dead, shity hu?"<<endl;
          }
else if (monhealth < 0)
{
    cout<<"you have killed the monster ^^, you should be proud."<<endl;
    }
else if (monhealth < 0 && health < 0)
{
    cout<<"you swing a mighty blow at the monster and sink it deep into its";
    cout<<"chest, but it stabs you in the belly at she same time. you are both";
    cout<<"dead"<<endl;
    }
    cout<<endl;                                           
    cout<<endl;
    cout<<endl;
                                                   
                                                   
                                         
                                   
                               
                                    system("pause");
                                    return 0;
                                    }

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.

...locksmith...

xpi0t0s 12Nov2008 16:19

Re: need a little help with a program i am making. should be easy for you masters.
 
> 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.

Loads for you to think about there :-)

xpi0t0s 12Nov2008 16:22

Re: need a little help with a program i am making. should be easy for you masters.
 
You could try using one of the Fighting Fantasy game books as the design. I thought about doing just that back in the days of my C64 but there was nowhere near enough memory to accomplish that.

cpulocksmith 14Nov2008 09:39

Re: need a little help with a program i am making. should be easy for you masters.
 
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.)
Code:

here = start;
cin>>direction;
if (direction == north)
{
here = 1;
}
else if (direction == south)
{
here = 2;
}
if (here == 1)
{
cout<<"you fall to your death in the pit";
}
else if (here == 2)
{
cout<<"a kolblod kicks you in the nutts";
}

{

is that something like you meant or did i take that completely in the wrong way.

(and yes i know the code wont work it is just a rough though i am just using to organize my thoughts.)

xpi0t0s 14Nov2008 15:59

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

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"
};


xpi0t0s 14Nov2008 16:08

Re: need a little help with a program i am making. should be easy for you masters.
 
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).

Code:

void init()
{
        for (int i=0; i<3; i++) monster_dead[i]=0;
        for (int i=0; i<2; i++) solved[i]=0;
}

void fight_monster(int monster_id,int *player_strength, int *player_skill)
{
        printf("Strength: YOU: %d  MONSTER: %d\n",*player_strength,monster_strength[monster_id]);
        printf("Skill: YOU: %d MONSTER: %d\n",*player_skill,monster_skill[monster_id]);

        // stub: expand this
        *player_strength/=2;
        if (!*player_strength)
        {
                printf("Whoops you just died\n");
        }
        else
        {
                printf("A nasty battle ensues and you are injured but win\n");
                printf("As you depart you notice the monster rematerialise.  Oh dear...\n");
        }
}

// strip trailing whitespace
void strip_trws(char *cmd)
{
        int i;
        for (i=0; cmd[i]; i++) ;
        for (i--; isspace(cmd[i]); i--) cmd[i]=0;
}

// convert to uppercase
void upcase(char *str)
{
        for (int i=0; str[i]; i++)
                if (str[i]>='a' && str[i]<='z')
                        str[i]+='A'-'a';
}

void go4e_15112()
{
        int game_exit=0;
        int pos=0; // player position
        int strength=10, skill=20;
        init();
        while (!game_exit)
        {
                // display the description
                printf("%s",descriptions[pos]);

                // any monsters to kill?
                int mid=monsters[pos];
                if (mid && !monster_dead[mid])
                        fight_monster(mid, &strength, &skill);

                // any traps?
                switch (traps[pos])
                {
                case 0: // nothing
                        break;

                case 1: // bottomless pit. Text has been handled in the description
                        strength=0;
                        break;
                }

                // any puzzles?
                switch (puzzles[pos])
                {
                case 0: // nothing
                        break;

                case 1: // MENE MENE TEKEL PARSIN. Name and two numbers?  (Daniel, 5, 25)
                        {
                                char name[32], num1[32], num2[32];
                                printf("Enter name: ");
                                fgets(name,30,stdin); strip_trws(name);
                                printf("Enter first number: ");
                                fgets(num1,30,stdin);
                                printf("Enter second number: ");
                                fgets(num2,30,stdin);

                                if (!strcmp(name,"Daniel") && (atoi(num1)==5) && (atoi(num2)==25))
                                {
                                        printf("Correct!\n");
                                        solved[1]=1;
                                }
                                else printf("Sorry, try again next time you're here\n");
                        }
                        break;
                }

                // Don't bother displaying directions and taking input if we're dead.
                if (strength<=0) game_exit=1;
                else
                {
                        // Display available directions
                        // char *directions="0:12X3 1:X0X3 2:0XXX 3:XX04 4:5X3Y 5:X4XX 6:XX4X"; // NSWE
                        char *dir=&directions[pos*7+2];
                        printf("Exits are to the ");
                        if (dir[0]>='0' && dir[0]<='6') printf("North ");
                        if (dir[1]>='0' && dir[1]<='6') printf("South ");
                        if (dir[2]>='0' && dir[2]<='6') printf("West ");
                        if (dir[3]>='0' && dir[3]<='6') printf("East ");
                        if (pos==4 && solved[1]) printf("East "); // maybe a better way of handling this generically?

                        // Handle player input
                        printf("State your command: ");
                        char cmd[1024];
                        fgets(cmd,1020,stdin);
                        strip_trws(cmd);
                        upcase(cmd);
                        int newpos=-1;
                        // Very simple parser
                        if (pos==0 && !strcmp(cmd, "TAKE AXE"))
                        {
                                // we should check (a) if we've already got the axe; (b) for inventory overflow
                                // If you just keep taking axes you'll end up with lots of axes and a crash.
                                inv[inv_next++]=1; // We should really use a symbolic constant here.  inv[inv_next]=AXE; is more readable.
                        }
                        else if (pos==6 && !strcmp(cmd,"ENTER TELEPORT"))
                        {
                                printf("You enter the teleport, blah blah, you win!\n");
                                game_exit=1;
                        }
                        else if (!strcmp(cmd,"NORTH") || !strcmp(cmd,"N"))
                        {
                                char c=dir[0];
                                if (c>='0' && c<='6') newpos=c-'0';
                                else printf("Sorry, no exit in that direction\n");
                        }
                        else if (!strcmp(cmd,"SOUTH") || !strcmp(cmd,"S"))
                        {
                                char c=dir[1];
                                if (c>='0' && c<='6') newpos=c-'0';
                                else printf("Sorry, no exit in that direction\n");
                        }
                        else if (!strcmp(cmd,"WEST") || !strcmp(cmd,"W"))
                        {
                                char c=dir[2];
                                if (c>='0' && c<='6') newpos=c-'0';
                                else printf("Sorry, no exit in that direction\n");
                        }
                        else if (!strcmp(cmd,"EAST") || !strcmp(cmd,"E"))
                        {
                                char c=dir[3];
                                if (c>='0' && c<='6') newpos=c-'0';
                                else if (pos==4 && solved[1]) newpos=6;
                                else printf("Sorry, no exit in that direction\n");
                        }
                        else if (!strcmp(cmd,"INV"))
                        {
                                if (!inv_next) printf("You have no items\n");
                                else
                                {
                                        printf("You have the following items:\n");
                                        for (int i=0; i<inv_next; i++)
                                        {
                                                switch (inv[i])
                                                {
                                                case 1: printf("An axe\n"); break;
                                                default: printf("A bug in the software\n"); break;
                                                }
                                        }
                                }
                        }
                        // Did we move?
                        if (newpos!=-1) pos=newpos;
                }
        }
}


xpi0t0s 14Nov2008 16:19

Re: need a little help with a program i am making. should be easy for you masters.
 
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:
Code:

void go4e_15112a()
{
        int game_exit=0;
        int pos=0; // player position
        int strength=10, skill=20;
        init();
        while (!game_exit)
        {
                handle_description(pos);

                if (!game_exit) handle_monsters(pos);
                if (strength<=0) game_exit=1;

                if (!game_exit) handle_traps(pos);
                if (strength<=0) game_exit=1;

                if (!game_exit) handle_puzzles(pos);

                if (!game_exit) display_directions(pos);

                if (!game_exit) handle_input(&pos); // &pos because this function might change it
        }
}

with the rest of the go4e_15112() code shifted out to these functions.

cpulocksmith 15Nov2008 09:42

Re: need a little help with a program i am making. should be easy for you masters.
 
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 hope you think it is good.

xpi0t0s 15Nov2008 15:13

Re: need a little help with a program i am making. should be easy for you masters.
 
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:

> while (!game_exit)

and how much easier it will be to find that than to find all a's.

cpulocksmith 16Nov2008 12:32

Re: need a little help with a program i am making. should be easy for you masters.
 
i dont really understand something about some of the code you gave as a example.

int traps[]={0,1,0,0,0,0,0}; // 1=bottomless pit

ok so on 2 there will be a pit. but how does the game know that the pit is on 2. and what the stats are for the trap. say we make 2= a arrow trap. and we do this.

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

so on 4 there will be an arrow trap, right? so would i go.

if traps[] =2{
health=health-3//to do 3 damage or what ever
}
somthing like that?


All times are GMT +5.5. The time now is 11:41.