I have this code :

void __fastcall TForm1::DrawGrid1SelectCell(TObject *Sender, int ACol,
      int ARow, bool &CanSelect)
{

  int temp,tempR=0, tempC=0, r=0, c=0;
  bool empty = false;
  CanSelect = false;

        //checking for empty cell - up
  if (ARow > 0){
    if (MyArray [ARow-1][ACol]==11){
      tempR = ARow-1;
      tempC = ACol;
      empty = true;
    
      }
  }
        //checking for empty cell - right
  if (ACol < 3){
    if (MyArray [ARow][ACol+1]==11){
      tempR = ARow;
      tempC = ACol+1;
      empty = true;
      moves++;
      }
  }
        //checking for empty cell - down
  if (ARow < 2){
   if (MyArray [ARow+1][ACol]==11){
     tempR = ARow+1;
     tempC = ACol;
     empty = true;
     moves++;
     }
  }
        //checking for empty cell - left
  if (ACol > 0){
    if (MyArray [ARow][ACol-1]==11){
      tempR = ARow;
      tempC = ACol-1;
      empty = true;
   
      }
  }
        //move tile by swapping empty cell with selected cell
  if (empty == true){
    r = tempR;
    c = tempC;
    tempR = ARow;
    tempC = ACol;
    ImageList1->Draw(DrawGrid1->Canvas,c*50, r*50, MyArray[tempR][tempC], true);
    ImageList1->Draw(DrawGrid1->Canvas, tempC*50, tempR*50, 11, true);
    temp = MyArray[tempR][tempC];
    MyArray[r][c] = temp;
    MyArray[tempR][tempC] = 11;

 }

This code is for moving one cell in DrawGrid (Borland C++ Builder ).
I want to know how can i modify this code that it will work for all the cells.

I am not exactly sure what you are asking, but I think you should try using recursion to call the function for all the surrounding cells.

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.