Create 2D game map with images

What logic should i put ? what should inside my if to prevent this problem ?