Ok so, I'm new to allegro, and I'm trying to write my first Allegro problem. It's supposed to be one of those sliding puzzles with the one missing piece and you make the picture. My program worked fine when I was just outputting the background and the picture of the squirrel, and it was random, and had the one empty space.

Then I added the function to click a square and get it to move into the empty spot next to it. I don't know if I was even doing it right, but the program runs, and just comes up as a black screen. The escape key doesn't work to shut it down, and every time I try it, I have to ctrl alt delete to close it. I know I added some other stuff when I wrote the play function, but I don't remember what, and what I do remember, I don't see how it would mess the program up.

I've tried isolating the problem, but I'm not getting anywhere. Does it have something to do with my acquire_screen() or release_screen()? I'm sorry I couldn't find where the error was occuring, but if anyone wants to take the time and help, I would really appreciate it.

It is a 3 by 3 picture, so the extra numbers in my arrays are just for safety.

#include <allegro.h>

BITMAP *backgroundSprite;
BITMAP *squirrelSprite;

int column[10];
int row[10];
int x = -1;
int y = -1;
int cursor_x;
int cursor_y;
int currentx;
int currenty;     
int swapper;
int getx;
int gety;
int stuff;

bool numAvail;

void setUpGame();
void printScreen();
void playGame();

int main()
{   
    allegro_init();
    install_keyboard();
    install_mouse();
    set_color_depth(16);
    set_gfx_mode( GFX_AUTODETECT, 640, 480, 0, 0);
  
//for randomizing in the setUpGame() function   
    srand(time(0));    
    
    setUpGame();
    printScreen(); 
       
    while(!key[KEY_ESC])
    {
       playGame();
    }
    
    readkey();
    
    destroy_bitmap(squirrelSprite);
    destroy_bitmap(backgroundSprite);

return 1;
}END_OF_MAIN();

void setUpGame()
{
     numAvail = true;
     
    //fills column[0 - 3] with a randomized number,
    //no two having the same number.
//These were not changed since the program worked
     for(int i = 0; i <= 3; i++ )
     {
             
      do{
            stuff = rand() % 3 + 1;
            
            for(int j = 0; j < i; j++) 
            {
                if( column[j] != stuff )
                    numAvail = true;
                    
                else
                {
                    numAvail = false;
                    break; 
                }  
            }
            
        }while(!numAvail);
         column[i] = stuff;
     }
      
     //fills row[0 - 3] with a randomized number,
    //no two having the same number.
//These were not changed since the program worked
      for(int i = 0; i < 3; i++ )
     {
             
      do{
            stuff = rand() % 3 + 1;
            
            for(int j = 0; j < i; j++) 
            {
                if( row[j] != stuff )
                    numAvail = true;
                    
                else
                {
                    numAvail = false;
                    break; 
                }  
            }
            
        }while(!numAvail);
         row[i] = stuff;   
     }
     
     //for later checking of column/row[x + 1],
     //just for checking purposes.
     column[4] = 4;
     row[4]= 4;
    
    //I don't know where I'm supposed to put these.
    backgroundSprite = load_bitmap("background.bmp", NULL);
    squirrelSprite = load_bitmap("squirrel.bmp", NULL);
 
}

void printScreen()
{
   getx = 0;
   gety = 0;
   
   //Also, not sure where this goes, or how the whole process
   //or aquiring screen really works.
   acquire_screen();
   
   draw_sprite(screen, backgroundSprite, 0, 0);
   
   for( int i = 0; i < 3; i++ )
   { 
        for(int j = 0; j < 3; j++)
        {      
               //This is the lower right corner-- not used
                if(i == 2 && j == 2)
                break;
          
       //Outputs squirrel pieces      
        blit(squirrelSprite, screen,getx,gety,(140*column[i])-30,(140*row[j])-110,140,140);

        //I believe this method of getx and gety worked before
        getx += 140;        
        }
            getx = 0;
            gety += 140;
    }
    
    //Again, unsure
    release_screen();
}


void playGame()
{
    //Also unsure
    show_mouse(screen);
    
     //For recording a click (if I'm using it right)
     if(mouse_b & 1)
     {
         cursor_x = mouse_x;
         cursor_y = mouse_y;    

    //Tells the puzzle piece that the mouse has clicked
     if(cursor_x > 110 && cursor_x < 530 && cursor_y > 30 && cursor_y <450)
     {
         if(cursor_x > 110 && cursor_x < 250)
         {   
             x = 1;
         }
         if(cursor_x > 250 && cursor_x < 390)
         {
            x = 2;
         }
         if(cursor_x > 390 && cursor_x < 530)
         {
            x = 3;
         }
         
         if(cursor_y > 30 && cursor_y < 170)
         {
             y = 1;  
         }
         if(cursor_y > 170 && cursor_y < 310)
         {
             y = 2;
         }
         if(cursor_y > 310 && cursor_y < 450)
         {
             y = 3;  
         }
         
         //For if they click somewhere not defined
         if(x != -1 && y != -1)
         {
               //So we can know the index of the square in question
              for( int i = 0; i < 3; i++ )
              {
                  if(column[i] == x)
                   currentx = i;
                   
                  if(row[i] == y)
                  currenty = i;
              }
         
             //Checks to the left and right, and changes values
             if(column[2] == x - 1 || column[2] == x + 1)
            {        
                swapper = column[2];
                column[2] = column[currentx];
                column[currentx] = swapper;   
            }
            //Checks up and down, and changes values
            if(row[2] == x - 1 || row[2] == x + 1)
            {
                swapper = row[2];
                row[2] = row[currenty];
                row[currenty] = swapper;   
            }
        }
    } 
    }  
}

Ok so I replaced
for(int i = 0; i <= 3; i++ )
in the setUpGame function with
for(int i = 0; i < 3; i++ )
and now the picture displays.

I can probably work it out from here. Sorry for the stupid mistake, and anyone who bothered to try and figure my problem out. If you have any tips or anything though, I would still love to hear them!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.