View Single Post
Old 09-6-2015, 04:48 AM   #31
AutotelicBrown
Under the scarlet moon
FFR Simfile AuthorD7 Elite KeysmasherFFR Veteran
 
AutotelicBrown's Avatar
 
Join Date: Jan 2014
Age: 31
Posts: 921
Default Re: [University - C++ Programming] Help With Assignment (Very Basic Stuff)

You got it slightly backwards, here is what should've been (at this point I don't think there is a problem giving it solved). Note the comments after the code explaining what is being done.

Code:
1   MAP_BUFFER  tileMap;
2   for (int row = 0; row < MAP_HEIGHT; ++row)  {
3     for (int col = 0; col < MAP_WIDTH; ++col) {
4       if ( row == 0 || row == MAP_HEIGHT - 1 || col == 0 || col == MAP_WIDTH - 1 ) {
5         tileMap[row][col] = WALL;
6       } else
7         tileMap[row][col] = EMPTY;
8       }
9     }
10  }
Comments by line:
Line 2 and 3 - unsigned for loop index don't make any difference most of the time, so int is cleaner. Not going into the details of it, but using ++row is the preferrable practice in C++ (but not in C).
Line 4 - The whole logical check can be done together and it is a chain of logical OR ('||' operand), which means if any of those four conditions is true, you execute the if block. The nested for loops makes the pair (row, col) go through all possible map indexes [0..19][0..29], so you want to check if they are on the border which is the beginning and end of the respective indexes. Note that C arrays are 0 indexed, so an array of size 20 goes from [0] to [19], which is why you need the -1 there and why the for loop test uses '<' and not '<='.
Line 5 - Tile is indeed border, assign WALL to it.
Line 6 - You get into else block if none of the tests inside the if evaluates to true which means it is not a border tile.
Line 7 - Tile is not border, assign EMPTY to it.

Would normally omit those brackets to make things cleaner but chose the explicit way to make it more obvious what is being done.

And my piece of advice for future programming assignments: the program will do exactly what you tell it to, so be always sure what each line you write is doing instead of simply guessing its meaning. All the basic syntax is easily available in any C/C++ documentation and if you need more nuanced explanation you should search on stackoverflow.com.

I usually use this for C/C++ reference and in your case it might be useful to check the tutorial with all the basic language structures here. I know that some details might not be so easy to understand without grasping the computer architecture behind the language abstraction but you should be able to handle all the basic programming without that knowledge.

Last edited by AutotelicBrown; 09-6-2015 at 04:49 AM..
AutotelicBrown is offline   Reply With Quote