Public Group

#### Archived

This topic is now archived and is closed to further replies.

This topic is 5357 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Heyas, I am starting a text based game, similar to NetHack just very basic. While browsing through these forums I have noticed people seem to use int arrays as examples of maps a lot. For example:
    1 = Wall
2 = Player

11111
00000
00000
00000
00200

Is there any advantage to using this over using say a char array? I would think using the char array would probably be more beneficial when looking at the code, as you could use more specific characters to represent spots on the map, like W for walls rather than just numbers. Thanks, Chris

##### Share on other sites
Theres no reason not to. Integers are as good as anything else, as are chars. I normally use an unsigned char type for mine. This gives me 256 tiles I could use, and you can still refer to it as an numerical type if you like.
For example, in my current project, I''ve got 16 tiles ATM (still in the early stages) I use unsigned char, as I doubt I''ll end up going above 256. I think the reason people use int rather than char (in the way you''re thinking) is that its easier to handle. but TBH its a matter of preference.

##### Share on other sites
you could use an array of bool''s for walls with 1''s where you cant go and 0''s where you can.

you could put say items or characters, whatever you want, in another array of say int''s or char''s and check both these in the game.

##### Share on other sites
quote:
Original post by witty
Theres no reason not to. Integers are as good as anything else, as are chars. I normally use an unsigned char type for mine. This gives me 256 tiles I could use, and you can still refer to it as an numerical type if you like.
For example, in my current project, I've got 16 tiles ATM (still in the early stages) I use unsigned char, as I doubt I'll end up going above 256. I think the reason people use int rather than char (in the way you're thinking) is that its easier to handle. but TBH its a matter of preference.

Thanks for the replies.

I've got two more questions now. First, say I were to use a 256 character unsigned char array, this would end up being a total of 256 bytes of memory being used( 1 byte per char * 256 chars), right?
I think I remember reading that if you just declare a variable without specifiying whether it was signed or unsigned, it defaulted to signed. However, after looking back through my book(C++ Primer) I can't specifically find this, so please let me know if I am mistaken.
Now for the second question, if I were to only use a char array I would be able ot have a total of 127 tiles, as a signed char can range from -128 to 127, right?

Thanks,
Chris

[edited by - Chryzmo on December 13, 2003 10:05:58 PM]

##### Share on other sites
If it''s just a 2d tiled map that you want to store, than any datatype that''s large enough to support the number of tiles will do. I imagine people like to use ints because they''re a familiar datatype.

As for assignment of a character to an index in an int array, there''s no reason you can''t do something like this:

void foo()
{
int a[42];
a[3] = ''W'';
}

Most compilers that I''ve seen will automatically reduce that ''W'' to its numerical value, 87, and assign that to the index of the array. The same seems to apply to comparisons.

An argument I might make for using an int array over a character array would be tracking what you have on a tile. You might use the first 8 bits of an int to store the basic tile, and use the next 8 bits to store what item is on those tiles.

Example:
WW = Wall
F0 = Floor with nothing
FH = Floor with Health
FP = Floor with player

WW WW WW WW WW
F0 F0 F0 F0 F0
F0 F0 FH F0 F0
F0 F0 F0 F0 F0
F0 F0 FP F0 F0

But the only reason I choose 8 bit chunks here is because chars are easy to manipulate campared to, say, nibbles, and because I''m assuming you wouldn''t need more than 256 kinds of tiles, or 256 kinds of items.

So I suppose the moral of this verbose story is "Choose the datatype that fits."

Hope it helps.

Remember: Silly is a state of Mind, Stupid is a way of Life.
-- Dave Butler

1. 1
2. 2
3. 3
frob
14
4. 4
5. 5
Rutin
12

• 12
• 9
• 57
• 14
• 15
• ### Forum Statistics

• Total Topics
632112
• Total Posts
3004176

×