Hello, I'm relatively new to c++ and I am trying to program a roguelike. I've successfully created one before in MATLAB and I am now experienceing some frustration trying to make a similar game in c++. Here is my code so far:
/* Read numerical grid and assign graphics */
lvl_1();
for (int row = 0;row<mapRows;row++)
{
for (int col = 0;col<mapCols;col++)
{
if (grid[row] [col] == 6)
{
graphics[row] [col] = '%';
}
}
}
/* output graphics */
for (int row = 0;row<mapRows;row++)
{
cout << setw(25) << right;
for (int col = 0;col<mapCols;col++)
{
cout << graphics[row] [col];
}
cout << endl;
}
cout << endl << "Command:\t";
cin.get();
return 0;
}
Basically my problem is that I cannot wrap my head around how to use 2-D arrays in c++. All I need to do is assign a map of ID numbers to an array, read those and assign the appropriate tile to a char array, and output the char array. What am I doing wrong in initializing the int array? How can I get this to work correctly and efficiently?
grid[mapRows] [mapCols] =
Error. This is effectively grid[20] [40]
You trying to assign an array initializer list to int. Outside array bounds.
Use loops to fill your (already existing) array with 6, like:
1 2 3
for(int row = 0; row < mapRows; row++)
for(int col = 0; col < mapCols; col++)
grid[row][col] = 6
I would use a loop to fill it, but the problem is I will be designing each level manually. I need some sort of organized, visual way to do this. The 6 is only a test. Basically each level of the dungeon will have its own function, only called when the user first enters it. The ID#s are then changed when the user walks around. How can I do this, while keeping everything in an organized 20*40 grid?