Getting the value of an array position.

This is a discussion on Getting the value of an array position. within the C++ Programming forums, part of the General Programming Boards category; Hi everyone. I will try my best to explain this in english, please ask if i fail: I have a ...

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

    Getting the value of an array position.

    Hi everyone. I will try my best to explain this in english, please ask if i fail:

    I have a tile map like that (let's say a small one for somplicity):

    Code:
    map[3][3] =
    {
    {0, 0, 1},
    {1, 0, 0},
    {0, 1, 0}
    }
    I want the guy to be able to walk only on zero tiles. How can i get what is the array value? If it is one or if it is zero? I need to do that check, to let the guy walk on the next time or to stop the guy from moving into that direction.

    So question in simple words: how can i check what is the next value of the array, one or zero?

    Thanks for help, and please ask, if you don't understand what i need, i will give you a bigger example with more code.

  2. #2
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    1,625
    Your guy will probably have a x and y placement, right?

    Code:
    For Right:
    if (map[y][x+1] == 0)
    
    For Left:
    if (map[y][x-1] == 0)
    
    For Up:
    if (map[y-1][x] == 0)
    
    For Down:
    if (map[y+1][x] == 0)
    But be careful not to go over the array's limits
    Devoted my life to programming...

  3. #3
    Registered User
    Join Date
    May 2010
    Posts
    84
    Quote Originally Posted by Sipher View Post
    Your guy will probably have a x and y placement, right?

    Code:
    For Right:
    if (map[y][x+1] == 0)
    
    For Left:
    if (map[y][x-1] == 0)
    
    For Up:
    if (map[y-1][x] == 0)
    
    For Down:
    if (map[y+1][x] == 0)
    But be careful not to go over the array's limits
    for that you could make the x and y of the map a constant and then you could make 2 checks, i to see if the tile is 1 and one to check if the player is leaving the map.

    for example:

    For Down:
    if ((map[y+1][x] == 0) && (y+1 < yconst))

    or something like that

  4. #4
    Novice programmer newn's Avatar
    Join Date
    Aug 2010
    Posts
    59
    Thanks for your answers, helped me a lot. Now trying to figure out how to use the for loop properly for this...

    Anyway, thanks for help.

    P.S. Sipher, i like the quote in your signature, lol.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Code review
    By bennywhere in forum C Programming
    Replies: 16
    Last Post: 10-20-2009, 09:00 PM
  2. Returning an object with a dynamic pointer
    By maxsthekat in forum C++ Programming
    Replies: 11
    Last Post: 09-16-2009, 01:52 PM
  3. Replies: 16
    Last Post: 05-29-2009, 07:25 PM
  4. Personal Program that is making me go wtf?
    By Submeg in forum C Programming
    Replies: 20
    Last Post: 06-27-2006, 12:13 AM
  5. Template Array Class
    By hpy_gilmore8 in forum C++ Programming
    Replies: 15
    Last Post: 04-11-2004, 11:15 PM

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