reduce one loop for generating a maze