Arrays doesn't work as expected...

This is a discussion on Arrays doesn't work as expected... within the C++ Programming forums, part of the General Programming Boards category; Hi everyone. Before asking anything on the forums, i try to correct the stuff myself, figure it out and if ...

  1. #1
    Novice programmer newn's Avatar
    Join Date
    Aug 2010
    Posts
    59

    Arrays doesn't work as expected...

    Hi everyone. Before asking anything on the forums, i try to correct the stuff myself, figure it out and if i fail - find a guide on it.

    However, this time i don't know why this code doesn't work, like it should:

    Code:
    	int mapX;
    	int mapY;
    	bool move;
    		for(mapX = 0; mapX < 15; mapX ++)
    		{
    			for(mapY = 0; mapY < 20; mapY ++)
    			{
    		if(map[mapX][mapY + 1] == 1)
    	{
    		move = true;
    	}
    	if(map[mapX][mapY - 1] == 1)
    	{
    		move = true;
    	}
    	if(map[mapX + 1][mapY] == 1)
    	{
    		move = true;
    	}
    	if(map[mapX - 1][mapY] == 1)
    	{
    		move = true;
    	}
    	else
    	{
    		move = false;
    	}
    It should assign array values to MapX and MapY. However, it just counts the array slots. It doesn't return the numbers of what is inside those slots of the array. Here is the array:

    Code:
    int map[15][20] =
    {
    	{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 2, 2},
    	{0, 2, 2, 3, 3, 3, 0, 0, 1, 1, 1, 0, 0, 0, 1, 1, 3, 3, 2, 2},
    	{0, 2, 2, 2, 0, 3, 3, 0, 1, 0, 1, 1, 1, 0, 0, 0, 3, 3, 3, 2},
    	{0, 2, 3, 2, 0, 3, 0, 0, 1, 0, 0, 0, 1, 1, 1, 1, 1, 1, 3, 0},
    	{0, 3, 0, 3, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 1, 0, 0, 1, 1, 1},
    	{0, 1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1, 0},
    	{0, 1, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 0},
    	{0, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1, 0},
    	{0, 1, 0, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 1, 1, 0, 1, 0},
    	{0, 1, 0, 0, 1, 0, 0, 0, 1, 1, 0, 1, 1, 0, 0, 0, 1, 1, 1, 0},
    	{0, 1, 0, 0, 1, 1, 1, 0, 0, 1, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0},
    	{0, 1, 0, 0, 0, 0, 1, 0, 0, 1, 1, 1, 0, 0, 0, 1, 0, 1, 1, 1},
    	{0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 0, 1, 1, 1, 1, 0, 0},
    	{0, 3, 2, 3, 2, 2, 3, 3, 2, 3, 0, 0, 1, 1, 1, 0, 0, 1, 1, 1}
    };
    Also, it returns true randomly on some ~random numbers, like 17, 16, 15 for example. Numbers are different on each y array.

    What mistake i haven't noticed? been figuring out this half a day...

    P.S. this should do this:

    Get the number of what is in the array, then if it is particular number, in this case 1, then return true. Otherwise return false.

  2. #2
    Registered User
    Join Date
    Mar 2010
    Posts
    109
    I'm not totally sure what you want to do, but if you put
    Code:
    if(map[mapX][mapY + 1] == 1)
    You are going to exceed the bounds of your array when mapY == 19. The same for mapX.

    It sounds like you want to return the number that is stored at the location in the array, but you are only testing if each location is equal to 1. Also, if you are only returning a bool, how will you know how many places to move?
    Last edited by syzygy; 08-05-2010 at 03:06 PM.

  3. #3
    Novice programmer newn's Avatar
    Join Date
    Aug 2010
    Posts
    59
    Yes, that is correct about what i want to do.

    It returns true, because it checks if the guy can move to that spot. It's an array for the tile map, so it checks for the tiles (numbers), if the tile is equal to 1 (snow), then it returns true, if the tile equal to any other numbers (0, 2, 3, etc...), it returns false. That means the guy cannot move.

    What this code is supposed to do comepletely:

    Check for the number stored in the array, if the number is 1, return true, so playerMove() = 1. The i have another code, which checks if this is true, and if it is, then it lets the player move on a keypress.

    So any ideas how to do it? :/ I still have no idea...

  4. #4
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Still don't understand a thing. There appears to be nested loops checking 4 squares around each square. The final value of move will depend only on what happens during the last iteration (a guess since I can't see where the loop code even ends). In the process you will be accessing array elements out of bound - namely map[-1][mapY] and map[mapX][-1] for sure.

    I may be misunderstanding something, but if you want to check if the guy can move to a given square on the map, don't you have to check just this one tile where it wants to move to (make sure the guy is not trying to leave the map for the first thing)?

    Or are you saying you don't know where the guy is?
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  5. #5
    Novice programmer newn's Avatar
    Join Date
    Aug 2010
    Posts
    59
    Yes, basically i would need to check the tile, where the guy tries to move, but i don't know how to do that. I'm kinda newbie, and trying to do the checking in a newbie way. If you know a better way, please tell me. Anyway, here is the bigger part of the code:

    Code:
    void input()
    {
    		for(int mapX = 0; mapX < 15; mapX ++)
    		{
    			for(int mapY = 0; mapY < 20; mapY ++)
    			{
    		if(map[mapX][mapY + 1] == 1)
    	{
    		move = TRUE;
    	}
    	if(map[mapX][mapY - 1] == 1)
    	{
    		move = TRUE;
    	}
    	if(map[mapX + 1][mapY] == 1)
    	{
    		move = TRUE;
    	}
    	if(map[mapX - 1][mapY] == 1)
    	{
    		move = TRUE;
    	}
    	else
    	{
    		move = FALSE;
    	}
    			}
    		}
    
    	if(key[KEY_UP] && move == TRUE)
    	{
    		player_y --;
    		keypress = 1;
    		press = TRUE;
    	}
    	else if(key[KEY_DOWN] && move == TRUE)
    	{
    		player_y  ++;
    		keypress = 2;
    		press = TRUE;
    	}
    	else if(key[KEY_LEFT] && move == TRUE)
    	{
    		player_x --;
    		keypress = 3;
    		press = TRUE;
    	}
    	else if(key[KEY_RIGHT] && move == TRUE)
    	{
    		player_x  ++;
    		keypress = 4;
    		press = TRUE;
    	}
    	else if(key[KEY_ESC])
    		endgame = 1;
    	else
    		press = FALSE;
    }
    The whole function...

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,013
    Why don't you just check map[player_x][player_y]? Since that's where you want to move, if possible.
    Or rather, you should probably check before incrementing the position (incrementing the position would be bad if you can't move there).
    Also, I don't see endgfame, press, player_x, player_y or move defined anywhere. Globals? Don't do that. Learn to pass by reference instead.
    Also, use true/false, not TRUE/FALSE. They're C90 defines for use with BOOL, which is a Windows type that emulates bools which were not present in C90, but is in C++.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    Novice programmer newn's Avatar
    Join Date
    Aug 2010
    Posts
    59
    Map[player_x][player_y] would have to have the player in the map's array i suggest? I just woke up, maybe i didn't got the idea yet.
    As for checking before incrementing the position, well, that's how i understand it, is what i have done here. I mean it checks for the array map positions, and if it is the position, that is available to move, then let the guy increment the position.

    Okay, since we have started to talk about the structure, i am trying to adjust my coding a bit lately. I have created one ping pong game and creating a game for tile map learning reasons, which may later evolve into some actual game, not just running around. Since my code got very messy and hard to read, after i corrected my ping pong movement and other bugs, that were present at the first "release" at my blog (very few visitors though...),, so i realized, that my code is not so good after all. I am using Allegro library for my games.
    Anyway, at the beginning, i just put everything into the main function. I mean declaring variables, everything. It's hard to find anything that way and stuff like that happens. Then, when i wrote this game, actually still writing, as you can see, one last thing left though - collision. I instead of using main as the place to put the code, used functions instead. A function for every little thing, but not enough still. Next game - Pong with blocks for one guy, i wanted to write all in different .h files. Every function in a different header file. And keep int main() as clean, as possible by doing that. Currently this game only have data file and map in header files...

    Anyway, yes, i was declaring the variables globally, at the top. Can i ask why is that a bad idea? Just wondering. And i will go read something about "passing variables by reference".

    Also, those TRUE/FALSE were in many tutorials of Allegro, instead of true/false. You can use them in integers. Little easier for a beginner to use such things, at least for me. Maybe it isn't a very good idea. I will try to use Boolean, in the next game, where i want to keep the code clean. Can you give me any other advices on how to optimize my code better?

    And here is the code: C++ | #include <allegro.h> #include - Code optimization for the game... - GpVLAA9i - Pastebin.com.

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,271
    Quote Originally Posted by newn
    Anyway, yes, i was declaring the variables globally, at the top. Can i ask why is that a bad idea?
    Global variables tend to make it more difficult for you to reason about your program as the state of a global variable can change when you don't expect it to (because you overlooked something in some function). Consequently, it can be more difficult to maintain your code, since changes that involve the global variable could have ramifications beyond what you can easily see. Furthermore, it may be more difficult to reuse code that uses a global variable.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,013
    Quote Originally Posted by newn View Post
    Also, those TRUE/FALSE were in many tutorials of Allegro, instead of true/false. You can use them in integers. Little easier for a beginner to use such things, at least for me. Maybe it isn't a very good idea. I will try to use Boolean, in the next game, where i want to keep the code clean. Can you give me any other advices on how to optimize my code better?[/url].
    Instead of
    Code:
    BOOL b = TRUE;
    b = FALSE;
    You can do:
    Code:
    bool b = true;
    b = false;
    bool is a built-in type in C++, whereas BOOL is just a datatype that emulates a bool, which was meant for C90, which doesn't have a bool datatype.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  10. #10
    Novice programmer newn's Avatar
    Join Date
    Aug 2010
    Posts
    59
    Okay, i will try to use bools, instead of integers (int asd = TRUE) in the next game, as i said. Thanks for explaining this.

    Any other optimizations for my code? I bet there must be a lot, lol.

    Also, any ideas on the question, for which i posted this thread? I tried to think again, but couldn't think of something smart...

  11. #11
    The larch
    Join Date
    May 2006
    Posts
    3,573
    The logic of checking whether you can move at keypress should look something like this:

    Code:
    	if(key[KEY_UP] && can_move_to(player_x, player_y - 1)
    	{
    		player_y --;
    		keypress = 1;
    		press = TRUE;
    	}
    	else if(key[KEY_DOWN] && can_move_to(player_x, player_y + 1)
    	{
    		player_y  ++;
    		keypress = 2;
    		press = TRUE;
    	}
    	else if(key[KEY_LEFT] && can_move_to(player_x - 1, player_y )
    	{
    		player_x --;
    		keypress = 3;
    		press = TRUE;
    	}
    	else if(key[KEY_RIGHT] && can_move_to(player_x + 1, player_y ))
    	{
    		player_x  ++;
    		keypress = 4;
    		press = TRUE;
    	}
    The can_move would check just one tile in the map

    Code:
    int can_move(int x, int y)
    {
        //range check:
        if (x < 0 || x >= map_width) return FALSE;
    
        if (y < 0 || y >= map_width) return FALSE;
    
        //check tile
        return map[x][y] == 1;
    }
    I left those TRUE and FALSE in, because it is possible that you are actually coding in C, and posted the question on the wrong forum. (Allegro itself is a C library, and I'd expect all the examples to be pure C code.)

    This also assumes that things are global. It appears you don't even have variables for the width and height of the map, and are using magic values 15 and 20 instead? Don't.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  12. #12
    Novice programmer newn's Avatar
    Join Date
    Aug 2010
    Posts
    59
    Yea, i see your way. But the i would need to put the player in the array too. And i have no idea, how to make player player move then (how to make the number in the array to switch places with another number). If you could explain this (and if this is possible in the first place, as google is unable to find such a thing, at least for my search), that would be nice.

    As for C of Allegro, it's a mix as far, as i know. Based on C++ and C. Though other libraries, like DirectX, OpenGL and SDL (all i know, lol) are based only on C++... But are harder and not for a beginner to learn. So i have chosen Allegro, as that was stated in some thread, that this is the best library for a beginner to learn, how to create games.

  13. #13
    Registered User
    Join Date
    Mar 2010
    Posts
    109
    You wouldn't put the player in the array. You would just have the player keep track of his own location. So the player stores his own player_x, player_y, and that's what you test against when you read a key press.

  14. #14
    Novice programmer newn's Avatar
    Join Date
    Aug 2010
    Posts
    59
    Okay, how i understand this:

    The player position (player_x and player_y) is stored in the array and is tracked on which tile the player is. Instead of some map tile number. When he moves, it checks for the next tile, if the tile is movable, if it is, then it allows the character to move.

    I failed at explaining my question then: in the post before this one - how can i store a variable in an array, keep track of it and then restore the map variable... Or would i have to use two arrays... I have no idea actually how can i do something similar to this, and i was unable to find information too.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Manipulating Character arrays in functions.
    By kbro3 in forum C++ Programming
    Replies: 11
    Last Post: 08-16-2008, 03:24 AM
  2. Global arrays shared between multiple source files
    By mike_g in forum C Programming
    Replies: 4
    Last Post: 08-14-2008, 07:29 PM
  3. One pointer that does not work as expected
    By h3ro in forum C++ Programming
    Replies: 13
    Last Post: 07-16-2008, 08:58 AM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 07:39 AM
  5. The Bludstayne Open Works License
    By frenchfry164 in forum A Brief History of Cprogramming.com
    Replies: 8
    Last Post: 11-26-2003, 11:05 AM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21