Thread: Pointer Passing Problems/ General Confusion

  1. #1
    Apprentice Swordsman's Avatar
    Join Date
    Apr 2007
    Posts
    38

    Pointer Passing Problems/ General Confusion

    Hi, I'm nearly finished on this little project (a maze game) I've been working on, but have come up against a stumbling block. I'll try to explain it the best I can without just posting all of the code.

    Right, it all started with a simple few IF statements in the key function:

    Code:
    #define PLAYER '@'
    #define MONSTER '#'
    #define EMPTY ' '
    #define OWALL '£'
    #define IWALL '&'
    
    void keyTest()
    {
    .......
    .......
    while(!done)
    	{
            switcher = _getch(); 
            switch(switcher)
            {
            case UPKEY:
                                
                  if(board[x][y - 1] == OWALL)
    	        break;
    				
    	if(board[x][y - 1] == EXIT)
    	{
                    system("CLS");
                    gotoxy(70, 25);
    	         printf("YOU WIN!!!");
    	         done = TRUE;
    	        _getch();
    	        break;
    	}
    			
                  if(board[x][y - 1] == MONSTER)
    	{             
                   system("CLS");
                   gotoxy(70, 25);
    	     printf("YOU LOSE!!!");
    	     done = TRUE;
    	       _getch();
    	      break;
    	}
    	else
    		{
    		board[x][y] = EMPTY;
    		printf("%c", EMPTY);
    		y--;
    		board[x][y] = PLAYER;
    		gotoxy(x, y);
    		printf("%c", PLAYER);
                  }
                  break;
    .......
    .......
    }
    The board[x][y] is a char array that I'm using to try and randomly generate content (instead of drawing ASCII art in a text file and using a pointer).

    Now the problem I had there was that the collision detection wasn't really performing and was detecting a monster hit when I was nowhere near it. I eventually figured that because I was using gotoxy for the screen positioning, the array co-ordinates were different from the values of X and Y.

    To get around this, I used this:

    Code:
    void initBoard()
    {
    .......
    .........
    for(i = 0 ; i< XRES; i++)
    	{
    		for(j = 0; j< YRES; j++)
    		{
    			if((board[i][j]) == PLAYER)
    			{
    				x = i; 
    				y = j;
    			}
    		}
    	}
    .....
    .......
    }
    The next problem was passing X and Y from the initBoard function into the keyTest function. Long story short, my main function looked like this:

    Code:
    int main(void)
    {
    conWindow();
    /* Seed rand */
    srand(time(NULL));
    title();
    initBoard();
    gotoxy(2, 2);
    keyTest();
     return 0;
    }
    which meant that I had to not only pass a value into the keyTest function from the initBoard function, but also had to give it another value from main. This was messing up my program.
    So my somewhat contrived solution was to pass the values from the initBoard function, into the main function, then from there into the keyTest function, which I achieved like this:

    Code:
    void initBoard()
    {
    .......................................
    main(&x, &y);
    ..................
    }
    
    //////////////////////////////////////////////////////////
    int main(int *ref, int *ref2)
    {
    .........
    ref = &x;
    ref2 = &y;
    keyTest(&ref,&ref2);
    .........
    }
    
    /////////////////////////////////////////////////////
    
    void keyTest(int *pass, int *pass2)
    {
    ..........
    	x = &pass;
    	y = &pass2;
    ..........
    }
    As you can probably tell, now I'm just really confused and I know that has to be a better way of doing this but can't think of a way around it.

    NOTE: The ............... inside the code is to signify that there is more code there, but I'm not sure that it's relevant.

    Any help/comments/musings would be greatly appreciated.

    Thanks

    SM
    Last edited by Swordsman; 05-01-2007 at 09:48 AM. Reason: Indentation came out badly

  2. #2
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Well, the standard already lays out exactly how main() needs to be defined. One way to achieve what you're trying to do might be:

    Declare x and y inside main(), pass pointers to them to your initBoard() function. Then pass them (their values, not pointers) from main() to your keyTest() function:
    Code:
    void initBoard(int *x, int *y)
    {
    .............
    			if((board[i][j]) == PLAYER)
    			{
    				*x = i; 
    				*y = j;
    			}
    ..............
    }
    int main(void)
    {
      int x, y;
      initBoard(&x, &y);
      keyTest(x, y);
      return 0;
    }
    If you understand what you're doing, you're not learning anything.

  3. #3
    Apprentice Swordsman's Avatar
    Join Date
    Apr 2007
    Posts
    38
    Thanks very much for that, I can't believe that I didn't think about that first.
    Much cleaner and tidier now, cheers
    Last edited by Swordsman; 05-02-2007 at 05:15 AM.

  4. #4
    Apprentice Swordsman's Avatar
    Join Date
    Apr 2007
    Posts
    38
    I actually posted this as an edit to the last post before I realised that nobody would see that, so I apologise for the bump.

    One thing though, my initial thoughts were wrong. I wanted to do this so that I could line up the board array and the screen position (it has an outer wall, so the array co-ordinates of 0,0 would be a wall where the screen co-ordinates of 0,0 are outside of the maze).

    Unfortunately, my logic seems to be wrong on this one and I still keep hitting 'invisible monsters'.
    Here's the movement and collision detction code:

    Code:
    void keyTest()
    {
    
    int switcher;
    int done;
    
    done = FALSE;
    
    while(!done)
    	{
            switcher = _getch(); 
            switch(switcher)
            {
            case UPKEY:
                                
                  if(board[x][y - 1] == OWALL)
    	        break;
    				
    	if(board[x][y - 1] == EXIT)
    	{
                    system("CLS");
                    gotoxy(70, 25);
    	         printf("YOU WIN!!!");
    	         done = TRUE;
    	        _getch();
    	        break;
    	}
    			
                  if(board[x][y - 1] == MONSTER)
    	{             
                   system("CLS");
                   gotoxy(70, 25);
    	     printf("YOU LOSE!!!");
    	     done = TRUE;
    	       _getch();
    	      break;
    	}
    	else
    		{
    		board[x][y] = EMPTY;
    		printf("&#37;c", EMPTY);
    		y--;
    		board[x][y] = PLAYER;
    		gotoxy(x, y);
    		printf("%c", PLAYER);
                  }
                  break;
    After this, the left, right and down are done in exactly the same way, with only the x/y+- changed. I'm completely out of ideas now for why this isn't working, can anybody help out please?
    This seems to be the last obstacle in my quest to finish this and I really want to get cracking on all of the 'decorations'

    Thanks,

    SM

  5. #5
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Is your array maybe not being initialized correctly? I imagine that you print the entire screen at some point early on. Does that show the monsters correctly? Do you maybe have an "= MONSTER" test instead of "== MONSTER" for one of the other directions?
    If you understand what you're doing, you're not learning anything.

  6. #6
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Code:
    #define PLAYER '@'
    #define MONSTER '#'
    #define EMPTY ' '
    #define OWALL '&#163;'
    #define IWALL '&'
    Walls are usually '#'s, and monsters are usually letters . . . </roguelike>

    Also, I'm not sure if the pound sign is ANSI standard. http://www.fileformat.info/info/unic...00a3/index.htm It may exist in the extended ASCII. You'd have to check.

    It seems to me that your code could make use of a switch statement, and definitely a function too, as mentioned.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  7. #7
    Apprentice Swordsman's Avatar
    Join Date
    Apr 2007
    Posts
    38
    I'll try this, thanks DWKs

    I'll let you know happens

  8. #8
    Apprentice Swordsman's Avatar
    Join Date
    Apr 2007
    Posts
    38
    I've got this cracked now I think.

    I had 2 major problems that I had to fix:

    Firstly, in the code up there, else if seemed to be the way instead of just if statements.
    Secondly, I seeded rand in the main function and passed it to every other function which was causing problems. Now I have seeded it in every function that needed it, things seem to be working.

    Thanks to the guys that helped me out

    Oh, one final question, I'm trying to do this:

    Code:
    if(board[i][j] == IWALL && wall == 1)
    			{
       ------->                        board[i+1][j] = IWALL || board[i][j+1] = IWALL;
    			               wall = 1+rand()&#37;5;
                }
    I'm sure your seeing what I'm trying to do there, but my compiler keeps throwing an error. Can someone please give me the correct syntax for this? Google keeps telling me about binary numbers or if statements.

  9. #9
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Code:
    if(board[i][j] == IWALL && wall == 1)
    			{
       ------->                        board[i+1][j] = IWALL || board[i][j+1] = IWALL;
    			               wall = 1+rand()%5;
                }
    I'm sure your seeing what I'm trying to do there, but my compiler keeps throwing an error. Can someone please give me the correct syntax for this?

    Google keeps telling me about binary numbers or if statements.
    Maybe you want an if statement, inside of the first if statement?

    Code:
    if(board[i][j] == IWALL && wall == 1)
    {
         if (board[i+1][j] == IWALL || board[i][j+1] == IWALL)
    	 wall = 1+rand()%5;
    }
    Adak

  10. #10
    Apprentice Swordsman's Avatar
    Join Date
    Apr 2007
    Posts
    38
    Well, I'm trying to say that if board[i][j] == IWALL,
    make board[i+1][j] OR board[i][j+1] = IWALL.

    I'm using this to look through the array for IWALLs, then place another next to them on either the X or Y axis. If that makes even a lick of sense.

    You should only seed rand once, at the beginning of your program.
    Dammit!
    Thought I had the answer there. Why should I only seed it once (I'm using the system clock)?

    Thanks again guys

  11. #11
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by Swordsman View Post
    Well, I'm trying to say that if board[i][j] == IWALL,
    make board[i+1][j] OR board[i][j+1] = IWALL.

    I'm using this to look through the array for IWALLs, then place another next to them on either the X or Y axis. If that makes even a lick of sense.



    Dammit!
    Thought I had the answer there. Why should I only seed it once (I'm using the system clock)?

    Thanks again guys
    Because once the (pseudo) Random number generator is "seeded", it has a starting point. From then on, it will deliver it's best shot at random number selections. Seeding it again, is a waste of time & code - it can not improve it's performance in any way.

    I'm using this to look through the array for IWALLs, then place another next to them on either the X or Y axis. If that makes even a lick of sense.
    If it makes sense to you, it makes sense to me - i've never seen your game at all, so have only a slightly out of focus, idea of it.

    OK, so our algo for this is:
    Code:
    Scan every array element
       if an IWALL is found then {
          get a random number
          
          if that random number is even,   
              put another IWALL adjacent to the first one, along the x axis
          else  /* the random number is odd */
              put another IWALL adjacent to the first one, along the y axis
          
       } /* end of if an IWALL */
    } /* end of scan
    That's how I understand what you're saying now. Scan would be a for loop, usually.

    Sound about right?

    Adak
    Last edited by Adak; 05-05-2007 at 08:21 AM.

  12. #12
    Apprentice Swordsman's Avatar
    Join Date
    Apr 2007
    Posts
    38
    Sounds perfect, thanks Adak

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Pointer confusion
    By rits in forum C Programming
    Replies: 3
    Last Post: 03-12-2009, 08:00 AM
  2. Replies: 12
    Last Post: 01-02-2009, 07:24 AM
  3. Following CTools
    By EstateMatt in forum C Programming
    Replies: 5
    Last Post: 06-26-2008, 10:10 AM
  4. Passing a pointer to two-dimension array in a function
    By E_I_S in forum C++ Programming
    Replies: 11
    Last Post: 06-19-2008, 09:57 AM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM