LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-04-2003, 05:49 PM   #1
xconspirisist
Member
 
Registered: Dec 2002
Location: United Kingdom
Distribution: Ubuntu
Posts: 276

Rep: Reputation: 30
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 ?

Last edited by xconspirisist; 11-04-2003 at 05:52 PM.
 
Old 11-04-2003, 07:47 PM   #2
ToniT
Senior Member
 
Registered: Oct 2003
Location: Zurich, Switzerland
Distribution: Debian/unstable
Posts: 1,357

Rep: Reputation: 47
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);
//....
 
Old 11-04-2003, 08:01 PM   #3
dakensta
Member
 
Registered: Jun 2003
Location: SEUK
Distribution: Debian & OS X
Posts: 194

Rep: Reputation: 35
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?
 
Old 11-04-2003, 10:10 PM   #4
jhorvath
Member
 
Registered: Sep 2002
Location: OH, USA
Distribution: 2.6.16-1.2096_FC5 #1
Posts: 245

Rep: Reputation: 30
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]

Last edited by jhorvath; 11-05-2003 at 02:13 PM.
 
Old 11-05-2003, 11:48 AM   #5
jhorvath
Member
 
Registered: Sep 2002
Location: OH, USA
Distribution: 2.6.16-1.2096_FC5 #1
Posts: 245

Rep: Reputation: 30
bump ....sorry
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Integer Numbers in C Mercurius Programming 24 10-20-2005 09:54 AM
Integer To a String in C Bean101 Programming 2 05-27-2004 04:46 AM
64 bit integer fahad153 Programming 9 08-26-2003 02:03 AM
Shel scripting: variables pointing to variables and case Dark_Helmet Programming 5 06-08-2003 11:07 AM
length of integer variables daztheladd Programming 5 11-26-2002 07:29 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:59 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration