LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   binding an integer to variables... (https://www.linuxquestions.org/questions/programming-9/binding-an-integer-to-variables-112422/)

xconspirisist 11-04-2003 05:49 PM

binding an integer to variables...
 
read this will proberbly help : http://www.linuxquestions.org/questi...hreadid=111948

hey, again, if you saw my last post about variables, you're proberbly wondering if this particular n00b is trying to do something rather odd with these variables, but there is some good coming of this. Really. :)

I have a function which is being send an integer, from another fuction.

I have 16 different variables. Variable01, Variable02, Variable03, etc.

I have some class thing .. ( ?! ) which uses these variables. I would like to do a function that does the thing with the classes, Variable01.addfruit -- depending on what integer is send the the function will depend on what variable the class uses.

Now your really confused. Hers's code.

location.cpp
Code:

using namespace std;

// Class for the new_location arithlogram
enum { none = 0, north = 1,east = 2,south = 4,west = 8 };

class square {
        private:
                int contents;


        public:
                            bool can_go(int direction) { return (contents & direction) ? true : false; }

                square(void) { contents = none; }
                square(int which) {
                        contents = none;
                        addDirection(which);
                }

                void addDirection(int direction) {
                        if(!can_go(direction)) (contents |= direction);
                        else cout << "You cannot have more than one of the same thing you greedy bastard.\n";
                }
//
//
//                void removeDirection(int direction) {
//                        if(is_inside(direction)) (contents ^= direction);
//                        else cout << "You cannot remove something you do not have ..." << endl;
                }
//
//
//                                void emptyDirections(void) { contents = none; }
//
//            void printDirections(void) {
//                        cout << "This basket has "
//                            << (is_inside(north)) << " north, "
//                            << (is_inside(east)) << " east, "
//                            << (is_inside(south)) << " south, "
//                            << (is_inside(west)) << " west.\n";
//                }
//
//              ~square(void) { /*...*/ }

};

            square sq1;
        square sq2;
                square sq3;
                square sq4;
                square sq5;
                square sq6;
                square sq7;
        square sq8;
                square sq9;
                square sq10;
                square sq11;
                square sq12;
                square sq13;
                square sq14;
                square sq15;
                square sq16;

void load_location( void ) {

                cout<< "Game : locations system loaded\r\n";
               
//                sq1.addDirection(north);
                sq1.addDirection(east);
                sq1.addDirection(south);
//                sq1.addDirection(west);

                sq2.addDirection(north);
                sq2.addDirection(east);
                sq2.addDirection(south);
//                sq2.addDirection(west);
               
                sq3.addDirection(north);
                sq3.addDirection(east);
                sq3.addDirection(south);
//                sq3.addDirection(west);

                sq4.addDirection(north);
                sq4.addDirection(east);
//                sq4.addDirection(south);
//                sq4.addDirection(west);

//                sq5.addDirection(north);
                sq5.addDirection(east);
                sq5.addDirection(south);
                sq5.addDirection(west);

                sq6.addDirection(north);
                sq6.addDirection(east);
                sq6.addDirection(south);
                sq6.addDirection(west);

                sq7.addDirection(north);
                sq7.addDirection(east);
                sq7.addDirection(south);
                sq7.addDirection(west);

                sq8.addDirection(north);
                sq8.addDirection(east);
//                sq8.addDirection(south);
                sq8.addDirection(west);

//                sq9.addDirection(north);
                sq9.addDirection(east);
                sq9.addDirection(south);
                sq9.addDirection(west);

                sq10.addDirection(north);
                sq10.addDirection(east);
                sq10.addDirection(south);
                sq10.addDirection(west);

                sq11.addDirection(north);
                sq11.addDirection(east);
                sq11.addDirection(south);
                sq11.addDirection(west);
               
                sq12.addDirection(north);
                sq12.addDirection(east);
//                sq12.addDirection(south);
                sq12.addDirection(west);
               
//                sq13.addDirection(north);
//                sq13.addDirection(east);
                sq13.addDirection(south);
                sq13.addDirection(west);

                sq14.addDirection(north);
//                sq14.addDirection(east);
                sq14.addDirection(south);
                sq14.addDirection(west);

                sq15.addDirection(north);
//                sq15.addDirection(east);
                sq15.addDirection(south);
                sq15.addDirection(west);

                sq16.addDirection(north);
//                sq16.addDirection(east);
//                sq16.addDirection(south);
                sq16.addDirection(west);


}

void new_location( characterData * ch, int location, int direction ) {

        switch ( direction ) {

                case 1: // North

                        if (sq1.can_go (north)) {
                               
                                // update to north

                        }
                                       
                        break;

                case 2: // East

                        break;

                case 4: // South
               
                        break;
                       
                case 8: // West

                        break;
        }


}


okey, the new_location function. see its getting the location integer ? well, I want the sq# ( sq1. in the example ) to refur to whatever the integer is the fucntion is being sent.

How do I do that ?

ToniT 11-04-2003 07:47 PM

I don't understand what are you trying to do, but anyways (independently what you do) it seems that you need an array of squares instead of variables named with a number in it.

That is, something like
Code:

square sq[16];
sq[0].addDirection(east);
//....


dakensta 11-04-2003 08:01 PM

Why don't you pass the square as the location, or better still add a current location square to your characterData, which will be updated by the new_loaction function if the move is possible.

Code:

enum direction { none=0, north=1, south=2, east=4, west=8 };

struct characterData
{
  square location;
  // other variables (struct used to keep it simple)
};

void new_location( characterData *ch, direction dir )
{
  if( ch->location.can_go( dir ) ) ; // update location
  else ;                                        // don't move?
}

Quick question: how are you planning to work out which squares are connected to which?

jhorvath 11-04-2003 10:10 PM

yet another crap example that could use heavy modification and optimizations applied :)
..i sure hope for your sake you're learning something...

Code:

#include <iostream>
using namespace std;

#define MIN_SIZE 4
#define MAX_SIZE 8

class tile {
        private:
                tile * above;
                tile * right;
                tile * left;
                tile * below;
        protected:
        public:
                tile(void) { above = right = left = below = NULL; }
                ~tile(void) { /*...*/ };

                friend class tmap;

                void * operator new[](size_t s) { return malloc(sizeof(tile) * s); }
                void operator delete[](void * p) { free(p); }
};

class tmap {
        private:
                tile * coord;
                int width,height;
        protected:
        public:
                tmap(int x, int y) {
                        if(x < MIN_SIZE || x > MAX_SIZE) width = MIN_SIZE; else width = x;
                        if(y < MIN_SIZE || y > MAX_SIZE) height = MIN_SIZE; else height = y;

                        coord = new tile[width * height];
                }

                int Width(void) { return width; }
                int Height(void) { return height; }
                tile * Coord(int i) { return &coord[i]; }

                const tile * get_above(int i) const { return coord[i].above; }
                const tile * get_right(int i) const { return coord[i].right; }
                const tile * get_left(int i) const { return coord[i].left; }
                const tile * get_below(int i) const { return coord[i].below; }

                ~tmap(void) {
                        delete[] coord;
                }
};

class player {
        private:
                tile * at;
        protected:
        public:
};

int main() {
        tmap MAP(0,0);

        for(register int i = 0; i < (MAP.Width() * MAP.Height()); i++) {
                cout << "MAP.coord[" << i << "] = " << MAP.Coord(i) << endl;
        }

        cout << endl;
        for(register int k = 0; k < (MAP.Height()); k++) {
                for(register int j = 0;j < (MAP.Width()); j++) {
                        cout << MAP.Coord(k * MAP.Width() + j) << " ";
                }
                cout << endl;
        }

        cout << endl;
        for(register int i = 0; i < (MAP.Width() * MAP.Height()); i++) {
                cout << "MAP.coord[" << i << "].above = " << MAP.get_above(i) << endl;
                cout << "MAP.coord[" << i << "].right = " << MAP.get_right(i) << endl;
                cout << "MAP.coord[" << i << "].left  = " << MAP.get_left(i) << endl;
                cout << "MAP.coord[" << i << "].below = " << MAP.get_below(i) << endl << endl;
        }

        // this part i leave to you ...set the tiles to point to the tiles ABOVE,RIGHT,LEFT,and BELOW
        // ...or NULL if none,

        return 0;
}

..get some docs on tile / sprite engines..and read , read, read :)

[EDIT]
i think i fixed it now ...it's still garbage, but just so you get the idea :) ..also, you probably already figured this out ..but much of the interface is missing entirely...:)
[/EDIT]

jhorvath 11-05-2003 11:48 AM

bump ....sorry :)


All times are GMT -5. The time now is 12:36 AM.