Thread: Having problems with 2d arrays

  1. #1
    Registered User
    Join Date
    Nov 2003

    Having problems with 2d arrays


    I am making a simple text based game, basically, it is a 20x20 board and i am keeping track of the player with 2 integers (x and y). I describe the terrain at each turn using a 20x20 character array (M for mountains, W for water etc)

    Now i have to add enemies to the game and keep track of what enemies are in each square using a 2d array and randomly assign monsters to each square (lots of the squares will be empty, but some may have up to 3 monsters)

    I am totally confused how to do this, i was thinking of having a 2d character array to keep track of the type of enemy in each square (O for orcs etc) but they have to be random and vary in number for each square and i am unsure how to keep track of all this information...

    Not sure if thats making much sense....

    Thanks in advance

  2. #2
    Me -=SoKrA=-'s Avatar
    Join Date
    Oct 2002
    Create a struct to hold all the data you might need for a square, and crate the 2D array of that square. eg.
    struct square
    	/*This will hold the number of enemies we
    	*assign to this particular square
    	int NumberOfEnemies;
    	*enemy_t would be a user-defined type with
    	*all the data needed for the enemy. It's a
    	*pointer as we will use it to create a
    	*dynamically-sized array with all the
    	enemy_t* enemy;
    	*GroundType will tell us if it's water, mountain
    	*or whichever type we want to use.
    	*It would probably be better to use an enum
    	*for this job.
    	char GroundType;
    SoKrA-BTS "Judge not the program I made, but the one I've yet to code"
    I say what I say, I mean what I mean.
    IDE: emacs + make + gcc and proud of it.

  3. #3
    Disturbed Boy gustavosserra's Avatar
    Join Date
    Apr 2003
    In my opinion, this might seem complicated for beginners. But try this:
    Build a matrix of the stl vector. Vector is easy to use once that you got how it works:
       vector<int> board[20][20];
    You could use integers to represent everthing in your world:
    1 - Water
    2 - Mountain
    3 - Orc
    4 - Elf
    So on...
    Nothing more to tell about me...
    Happy day =)

  4. #4
    Registered User
    Join Date
    Nov 2003
    Thanks for the replies, much appreciated
    Im a newbie to c++ and so havent done vectors, pointers yet, just the basics and 2d and 3d arrays...covered structs a little bit but not in much detail
    will read up on structs to see if i can understand it more
    thx again

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    The edge of the known universe
    Or combine both answers into
    vector<square> board[20][20];
    board[0][0].GroundType = 1;
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. 2D Array's, assigning chars.
    By gman89 in forum C Programming
    Replies: 9
    Last Post: 04-26-2008, 11:03 PM
  2. 2D arrays programming, please help
    By mfskratch in forum C Programming
    Replies: 4
    Last Post: 11-01-2007, 12:41 PM
  3. 2D arrays and storing characters
    By John_L in forum C Programming
    Replies: 4
    Last Post: 10-13-2007, 12:17 PM
  4. Initialising 2D and 3D arrays
    By fry in forum C++ Programming
    Replies: 5
    Last Post: 08-01-2002, 04:34 AM
  5. 2d arrays
    By snowy101 in forum C++ Programming
    Replies: 8
    Last Post: 07-08-2002, 04:05 PM