Collision detection inconsistencies I've been wondering about simple collision in C++ for a while now, and I've tried to make up my own code.

The code posted below works when walls are placed in a perfect rectangle around the player. However, once you randomly place them in the array, as in the code, weird inconsistencies seem to occur. Sometimes the player collides, sometimes the player doesn't.

If anyone would be as kind as to point out what the error is, I'd be much appreciated. In a clear step by step manner, if you would: I'm very new to this and chances are small that I can make the logical steps of thought necessary to successfully implement your suggestions.

#include "string.h" 
#include "surface.h" 
#include "stdlib.h" 
#include "template.h" 
#include "game.h" 

using namespace Tmpl8; 

int pacmanPosX = 32; 
int pacmanPosY = 32; 
int indexX; 
int indexY; 

bool colR; 
bool colL; 
bool colU; 
bool colD; 


Sprite pacman (new Surface( "assets/pacmanLeft.png"), 1);  

Surface* tileSet[3];  

int levelMap[8][8] =  
{{1,1,1,1,1,1,1,1}, 
 {1,0,0,0,0,0,0,1}, 
 {1,0,0,0,0,1,0,1}, 
 {1,0,0,1,0,0,0,1}, 
 {1,0,0,0,1,0,0,1},  
 {1,1,0,0,1,1,1,1}, 
 {1,0,0,0,0,0,1,1}, 
 {1,1,1,1,1,1,1,1}}; 

void Game::Init()  
{  
   // put your initialization code here; will be executed once  
   tileSet[0] = new Surface( "assets/pacmanFloor.png" ); // Sets up data for tileSet  
   tileSet[1] = new Surface( "assets/pacmanWall.bmp" );  
   tileSet[2] = new Surface( "assets/pacmanCookie.png" ); 

}  

void Game::Tick( float a_DT )  
{  
    m_Screen-&gt;Clear( 0 );  

     for (indexX = 0; indexX &lt; 8; indexX++) 
    { 
        for (indexY = 0; indexY &lt; 8; indexY++) 
        { 
            int tile = levelMap[indexY][indexX]; 
            tileSet[tile]-&gt;CopyTo( m_Screen, indexX * 32, indexY * 32 ); 
        } 
    } 



    pacman.Draw(pacmanPosX, pacmanPosY, m_Screen); 


    //Defining collision requirements for walls// 

    if(levelMap[(pacmanPosX+32)/32][pacmanPosY/32] == 1) //Right collision 
    { 
        colR = true; 
    }else colR = false; 

    if(levelMap[(pacmanPosX-2)/32][pacmanPosY/32] == 1) //Left collision 
    { 
        colL = true; 
    }else colL = false; 

    if(levelMap[pacmanPosX/32][(pacmanPosY-2)/32] == 1) //Upward collision 
    { 
        colU = true; 
    }else colU = false; 

    if(levelMap[pacmanPosX/32][(pacmanPosY+32)/32] == 1) //Downward collision 
    { 
        colD = true; 
    }else colD = false; 


    //Setting Up Controls// 

    if (GetAsyncKeyState(VK_RIGHT) &amp;&amp; colR == false){pacmanPosX += 2;} 
    if (GetAsyncKeyState(VK_LEFT) &amp;&amp; colL == false){pacmanPosX -= 2;} 
    if (GetAsyncKeyState(VK_UP) &amp;&amp; colU == false){pacmanPosY -= 2;} 
    if (GetAsyncKeyState(VK_DOWN) &amp;&amp; colD == false){pacmanPosY += 2;} 

    Sleep( 10 );  
}
Bug in your code
Here you are accessing the levelMap by 'Y' then 'X'
        int tile = levelMap[indexY][indexX]; 

In all other places you are accessing the levelMap by 'X' then 'Y'
if(levelMap[(pacmanPosX+32)/32][pacmanPosY/32] == 1) //Right collision 

So it looks like you are running around a maze that has been printed incorrectly on the screen. and thus it looks like collision detection is not working (it probably is) but the detection does not match the visual cues.
Reviewing your code:
These expressions
if(levelMap[(pacmanPosX+32)/32][pacmanPosY/32] == 1) //Right collision 
{ 
    colR = true; 
}else colR = false; 

Can be simplifies too:
colR = (levelMap[(pacmanPosX+32)/32][pacmanPosY/32] == 1); //Right collision 

While these
if (GetAsyncKeyState(VK_RIGHT) &amp;&amp; colR == false){pacmanPosX += 2;} 

can be simplified to:
if (GetAsyncKeyState(VK_RIGHT) &amp;&amp; !colR)
{    pacmanPosX += 2;
} 

Putting the statement on the same line as the test makes it harder to debug (when stepping through with the debugger) as you can not see when then the statements are executed. By putting it on its own line stepping through with the de-bugger does become easier as you will see it skip the statement when the condition fails.
Global state is the easy way to do things.
int pacmanPosX = 32; 
int pacmanPosY = 32; 
int indexX; 
int indexY; 

bool colR; 
bool colL; 
bool colU; 
bool colD; 

But in the long run makes your code more brittle and harder to update. Pass things as parameters to make it easier to maintain and extend your code (or create an object that wraps the information).
Also it looks like some of these properties would be better held in arrays rather than individual variables.
Edit base on comments:
This only detects a collision if the top right corner hits a wall.
colR = (levelMap[(pacmanPosX+32)/32][pacmanPosY/32] == 1); //Right collision 

Change too:
colR =    (levelMap[(pacmanPosX+33)/32][(pacmanPosY+00)/32] == 1)  // Top right
       || (levelMap[(pacmanPosY+33)/32][(pacmanPosY+31)/32] == 1); // bottom right

// Use +33 because moving left you use -2
// When you move right +32 puts you in the first pixel of the next block
//                     +33 puts you in the second pixel just like move left detection

// Use +31 on the Y access because this is still inside the current sprite.

Note If you have a hole in the wall that is exactly one block wide you need to hit it dead center to pass through the gap. To give you some wiggle room you can move the corners in slightly.
colR =    (levelMap[(pacmanPosX+33)/32][(pacmanPosY+02)/32] == 1)  // Top right
       || (levelMap[(pacmanPosY+33)/32][(pacmanPosY+29)/32] == 1); // bottom right

Here you do not need to hit the whole in the wall dead center and you get some wiggle room passing through.