A matrix of numbers

Started by
7 comments, last by Zahlman 17 years, 11 months ago
Hi fellas. I'm trying to build me one of those matrices with integers in it, and then do some simple collision testing. I've had the program up and running but then I decided that I could do with some more cases than just one. Thing is, if I do a switch-case with the tilesystem (as seen below) it doesn't work. It ignores my new settings (i.e. keeping all the numbers at 0 when they really should be a mixture of ones and zeros). Would appreciate any help I can get (as usual). Thanks!

levelCollissionX = levelCollissionY = 0;
        short int tilesystem[15][20] = {
        {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
        {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
        {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
        {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
        {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
        {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
        {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
        {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
        {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
        {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
        {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
        {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
        {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
        {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
        {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
        };
    switch(levelNumber)
    {
        case 0:
         levelCollissionX = levelCollissionY = 0;
        short int tilesystem = {
        {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
        {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
        {1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1},
        {1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1},
        {1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1},
        {1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1},
        {1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1},
        {1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1},
        {1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1},
        {1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1},
        {1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1},
        {1,1,1,1,1,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1},
        {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
        {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
        {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
        };
        
        break;
    } 

Advertisement
You are redeclaring tilesystem in your switch scope, therefore it will only exist with a mix of 0's and 1's within the switch.

should be like this:

short int tilesystem = ...;switch (...){ case 0: ...         tilesystem = ....;}
check out the scope, you are basically doing a scoped redeclaration.
Nope doesn't seem to work. I've tried declaring it in a header file as well. But it doesn't seem to like being declared somewhere else. I get an error saying I miss a '{' before the matrix or something...
Any ideas?
Quote:Original post by Aglet
Nope doesn't seem to work. I've tried declaring it in a header file as well. But it doesn't seem to like being declared somewhere else. I get an error saying I miss a '{' before the matrix or something...
Any ideas?


provide the full source code, showing the problem you are experiencing, preferably downstripped to a short file that will compile as a single unit.
Aglet's co-worker here. Well actually, posting the whole sourcecode downstripped would be .. a lot of megs. This is the only thing that is bugging us, as we can't seem to declare the 2D Matris and then initialising it to something.
This code down here gives an error. I just wonder how it should be done (lets just forget about the switch-case for now)

short int tilesystem[15][20];

tilesystem = {
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
};

ERROR

short int tilesystem[15][20];

tilesystem[15][20] = {

doesn't work

short int tilesystem[][];

tilesystem = {

doesn't work
You can't separate the declaration and initialization like that. If you want to initialize it with all those values you have to do it right as you declare it.

For example
// this works because I declare and initialize at the same timeshort correct_tile[2][2] ={	{0,1},	{0,5},};//But trying something like this is not correctshort invalid_tile[2][2];invalid_tile={	{0,1},	{0,5},};
moe.ron
Easiest solution:
levelCollissionX = levelCollissionY = 0;short int tilesystem[15][20] = {	{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},	{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},	{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},	{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},	{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},	{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},	{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},	{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},	{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},	{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},	{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},	{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},	{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},	{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},	{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}};switch(levelNumber){	case 0:	{		levelCollissionX = levelCollissionY = 0;		short int newtilesystem[15][20] = {			{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},			{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},			{1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1},			{1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1},			{1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1},			{1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1},			{1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1},			{1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1},			{1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1},			{1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1},			{1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1},			{1,1,1,1,1,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1},			{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},			{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},			{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}		};		// or memcpy if using C not C++		std::copy(newtilesystem, newtilesytem + (sizeof(newtilesystem) / sizeof(newtilesystem[0])), tilesystem);        		break;        }}

I.e. just declare a new array using initialization syntax and copy it over.

Σnigma
As indicated, the original problem is one of scope. The given syntax is only valid for *initialization*, not *assignment*; these are different things in C++. The switch statement introduces a new scope, so if you say "short int tilesystem" etc. in there, that declares a different variable from the outside one, which is then initialized to the other values, and then it falls out of scope at the end of the switch statement. (Note that individual *cases* do *not* introduce new scopes - they are case *labels* after all, not blocks.) And if you try to assign with the same syntax to the existing variable, it doesn't compile, because assignment is not initialization.

Enigma's solution is probably the simplest way of just making it work. You could also just assign 1's to individual array slots in the usual way: "tilesystem[x][y] = 1;" a whole bunch of times. As you can imagine, that's ridiculously tedious and error-prone. There is also the option of using Boost::Assign to make it a little easier and friendlier to write this kind of array re-assignment.

However, you *should not do things this way*. An experienced developer would never do this except as a quick hack for testing. What you want to do is put the data into a file, and read an appropriate file according to the levelNumber setting. This has numerous benefits:

- You don't need to recompile when you change something.
- You can add a tiny bit more information to the file, plus a little extra support in the code, and suddenly not be limited to a particular level "size" (each level could be different, even).
- You don't need to add the extra braces and so on to your data file. (The simplest solution, in C++, would probably use spaces instead of commas.)

If you don't know how to do this, feel free to post again in For Beginners; I'll be much nicer then ;)

This topic is closed to new replies.

Advertisement