# C++: Strange arithmetic error in adjacent tile checking code

I have a tilesheet from which I draw wall tiles. Each wall tile on the sheet is enumerated from XXXX to ULRD, where each place corresponds to whether there is an adjacent wall above, to the left, to the right, or below the cell in question.

I also have an integer variable called whatWallmapImageIndex. How it supposedly works is

1) If the cell is not on the top edge of the world and there is an adjacent wall cell above, index +=1000.

2) If the cell is not on the left edge of the world and there is an adjacent wall cell to the left, index += 100.

3) If the cell is not on the right edge of the world and there is an adjacent wall cell to the right, index += 10.

4) If the cell is not on the bottom edge of the world and there is an adjacent wall cell below, index += 1.

Really simple, right? Each "tens" place should correspond to a direction being true or false, so I'd expect that a cell that has adjacent wall tiles in every direction but left (UXLR) would be 1011.

Now this four-way adjacency check does not work, but I cannot figure out why. In this picture, nearly every corner and 3-way wall tile is incorrectly being drawn as the top left corner wall section.

That tile is the 0th position on the tile sheet, and I've confirmed by doing console output on the player character's cell that the drawing index (wallmapImageIndex[ whatever the player's cell ID is ]) is 0 also. That suggests that there is something wrong with the switch statement used to determine the drawing index (see code sample), but the first ten of the cases do work as expected while the six do not.

I've put console output in all the cases that do not work - None of them output the message.

I simply cannot find the error anywhere. Why do cases 1010 through 1111 not work?

void Generator::Commit()
{
/// ### Detail and finalize the layout for output                ###

for(int i = 0; i < areaCellWidth*areaCellHeight; i++)
{

int whatFloormapImageIndex = 0000; /* Whether adjacent cells are floor tiles ,
* represented in the form 0 0 0 0.  (1 = true)
*                         U L R D           */
int whatWallmapImageIndex = 0000;  /* Whether adjacent cells are wall tiles ,
* represented in the form 0 0 0 0.  (1 = true)
*                         U L R D           */

///                     ########### DETAIL THE WALLS ##########

if(genLayout[i] > GEN_CELL___WALL_MARKER_BEGIN && genLayout[i] < GEN_CELL___WALL_MARKER_END) // Cell is a type of wall // ***Will be more nuanced later.***
{

wallmap[i] = WT_WALL_IMPASSABLE;
if(i >= areaCellWidth)// Not on the top row
{
if(genLayout[i-areaCellWidth] > GEN_CELL___WALL_MARKER_BEGIN && genLayout[i-areaCellWidth] < GEN_CELL___WALL_MARKER_END) // Check above row for wall's existence
{
whatWallmapImageIndex += 1000; // U = 1
}
}

if(i%areaCellWidth > 0) // Not the left-edge column
{
if(genLayout[i-1] > GEN_CELL___WALL_MARKER_BEGIN && genLayout[i-1] < GEN_CELL___WALL_MARKER_END) // Check to the left for wall's existence
{
whatWallmapImageIndex += 0100; // L = 1
}
}

if(i%areaCellWidth < areaCellWidth-1) // Not the right-edge column
{
if(genLayout[i+1] > GEN_CELL___WALL_MARKER_BEGIN && genLayout[i+1] < GEN_CELL___WALL_MARKER_END) // Check to the right for wall's existence
{
whatWallmapImageIndex += 0010; // R = 1
}
}

if(i/areaCellWidth < areaCellHeight-1) // Not on the bottom row
{
if(genLayout[i+areaCellWidth] > GEN_CELL___WALL_MARKER_BEGIN && genLayout[i+areaCellWidth] < GEN_CELL___WALL_MARKER_END) // Check below for wall's existence
{
whatWallmapImageIndex += 0001; // D = 1
}
}

switch(whatWallmapImageIndex)
{
case 0000: wallmapImageIndex[i] = SI_XXXX_WALL; break; // index correct
case 0001: wallmapImageIndex[i] = SI_XXXD_WALL; break; // index correct
case 0010: wallmapImageIndex[i] = SI_XXRX_WALL; break; // index correct
case 0011: wallmapImageIndex[i] = SI_XXRD_WALL; break; // index = 0 - (But XXRD is supposed to be 0 anyway)
case 0100: wallmapImageIndex[i] = SI_XLXX_WALL; break; // index correct
case 0101: wallmapImageIndex[i] = SI_XLXD_WALL; break; // index correct
case 0110: wallmapImageIndex[i] = SI_XLRX_WALL; break; // index correct
case 0111: wallmapImageIndex[i] = SI_XLRD_WALL; break; // index correct
case 1000: wallmapImageIndex[i] = SI_UXXX_WALL; break; // index correct
case 1001: wallmapImageIndex[i] = SI_UXXD_WALL; break; // index correct
case 1010: wallmapImageIndex[i] = SI_UXRX_WALL; std::cout << "Message that never shows up at index " << i << ": " << whatWallmapImageIndex << std::endl; break; // Error - index = 0
case 1011: wallmapImageIndex[i] = SI_UXRD_WALL; std::cout << "Message that never shows up at index " << i << ": " << whatWallmapImageIndex << std::endl; break; // Error - index = 0
case 1100: wallmapImageIndex[i] = SI_ULXX_WALL; std::cout << "Message that never shows up at index " << i << ": " << whatWallmapImageIndex << std::endl; break; // Error - index = 0
case 1101: wallmapImageIndex[i] = SI_ULXD_WALL; std::cout << "Message that never shows up at index " << i << ": " << whatWallmapImageIndex << std::endl; break; // Error - index = 0
case 1110: wallmapImageIndex[i] = SI_ULRX_WALL; std::cout << "Message that never shows up at index " << i << ": " << whatWallmapImageIndex << std::endl; break; // Error - index = 0
case 1111: wallmapImageIndex[i] = SI_ULRD_WALL; std::cout << "Message that never shows up at index " << i << ": " << whatWallmapImageIndex << std::endl; break; // Possible error, but it's very rare to see a 1111 cell
}
}

}

generationPhaseComplete = true;
#ifdef D_GEN_PHASE_CHECK

You've run into octal literals. A number beginning with 0 is interpreted as base 8, so whatWallmapImageIndex += 0010; is equivalent to whatWallmapImageIndex += 8;. Try removing the leading zeros as a start.