MFC realizes the elimination algorithm of lianliankan game

  • 2020-06-15 09:49:27
  • OfStack

The example of this paper shares the specific code of MFC to realize lianliankan game elimination algorithm for your reference, the specific content is as follows

Can the picture of two positions be eliminated? There are three situations:

1.1 Linear connection, which is also the simplest method of elimination


bool LinkInLine(CPoint p1, CPoint p2) 
{
 conner1.x = conner1.y = -1; //  Record the inflection point position 
 conner2.x = conner2.y = -1;

 BOOL b = true;
 if (p1.y == p2.y) //  The two points are the same 1 line 
 {
  int min_x = min(p1.x, p2.x);
  int max_x = max(p1.x, p2.x);
  for (int i = min_x+1; i < max_x; i++)
  {
   if (game->map[i][p1.y] != 0)
   {
    b = false;
   }
  }
 }
 else if (p1.x == p2.x) //  In the same 1 column 
 {
  int min_y = min(p1.y, p2.y);
  int max_y = max(p1.y, p2.y);
  for (int i = min_y + 1; i < max_y; i++)
  {
   if (game->map[p1.x][i] != 0)
   {
    b = false;
   }
  }
 }
 else //  Is not the same 1 A straight line 
 {
  b = false;
 }
 return b;
}

2. Two straight lines are eliminated, that is, after 1 inflection point.

There are two cases when two vertices are connected by two straight lines, namely two inflection points.


bool OneCornerLink(CPoint p1, CPoint p2) 
{
 conner1.x = conner1.y = -1;
 conner2.x = conner2.y = -1;

 int min_x = min(p1.x, p2.x);
 int max_x = max(p1.x, p2.x);
 int min_y = min(p1.y, p2.y);
 int max_y = max(p1.y, p2.y);

 //  turning point 1
 int x1 = p1.x;
 int y1 = p2.y;
 // turning point 2
 int x2 = p2.x;
 int y2 = p1.y;

 BOOL b = true;
 if (game->map[x1][y1] != 0 && game->map[x2][y2] != 0)
 {
  b = false;
 }
 else
 {
  if (game->map[x1][y1] == 0) //  turning point 1 Position no picture 
  {
   for (int i = min_x + 1; i < max_x; i++)
   {
    if (game->map[i][y1] != 0)
    {
     b = false;
     break;
    }
   }
   for (int i = min_y + 1; i < max_y; i++)
   {
    if (game->map[x1][i] != 0)
    {
     b = false;
     break;
    }
   }
   if (b)
   {
    conner1.x = x1;
    conner1.y = y1;
    return b;
   }

  }


  if (game->map[x2][y2] == 0) //  turning point 2 Position no picture 
  {
   b = true;
   for (int i = min_x + 1; i < max_x; i++)
   {
    if (game->map[i][y2] != 0)
    {
     b = false;
     break;
    }
   }
   for (int i = min_y + 1; i < max_y; i++)
   {
    if (game->map[x2][i] != 0)
    {
     b = false;
     break;
    }
   }
   if (b)
   {
    conner1.x = x2;
    conner1.y = y2;
    return b;
   }
  }
 }

 return b;
}

3.3 Elimination of straight lines, that is, after two inflection points.

This can be done by transverse scanning and longitudinal scanning. When scanning, even inflection points can be obtained to determine whether two vertices can be eliminated after passing these two inflection points


bool TwoCornerLink(CPoint p1, CPoint p2) 
{
 conner1.x = conner1.y = -1;
 conner2.x = conner2.y = -1;

 int min_x = min(p1.x, p2.x);
 int max_x = max(p1.x, p2.x);
 int min_y = min(p1.y, p2.y);
 int max_y = max(p1.y, p2.y);
 bool b;
 for (int i = 0; i < MAX_Y; i++) //  Scan line 
 {
  b = true;
  if (game->map[p1.x][i] == 0 && game->map[p2.x][i] == 0) //  No pictures of the two inflection points 
  {
   for (int j = min_x + 1; j < max_x; j++) //  Determine if an inflection point can be connected 
   {
    if (game->map[j][i] != 0)
    {
     b = false;
     break;
    }
   }

   if (b)
   {
    int temp_max = max(p1.y, i);
    int temp_min = min(p1.y, i);
    for (int j = temp_min + 1; j < temp_max; j++) //  judge p1 And the inflection point that it corresponds to 
    {
     if (game->map[p1.x][j] != 0)
     {
      b = false;
      break;
     }
    }
   }

   if (b)
   {
    int temp_max = max(p2.y, i);
    int temp_min = min(p2.y, i);
    for (int j = temp_min + 1; j < temp_max; j++) //  judge p2 And the inflection point that it corresponds to 
    {
     for (int j = temp_min + 1; j < temp_max; j++)
     {
      if (game->map[p2.x][j] != 0)
      {
       b = false;
       break;
      }
     }
    }
   }
   if (b) //  If a route exists, return true
   {
    conner1.x = p1.x;
    conner1.y = i;
    conner2.x = p2.x;
    conner2.y = i;
    return b;
   }
  } 

 }//  End of scan line 


 for (int i = 0; i < MAX_X; i++) //  Scan the column 
 {
  b = true;
  if (game->map[i][p1.y] == 0 && game->map[i][p2.y] == 0) //  No picture of inflection point position 
  {
   for (int j = min_y + 1; j < max_y; j++) //  Whether two inflection points can be connected 
   {
    if (game->map[i][j] != 0)
    {
     b = false;
     break;
    }
   }

   if (b)
   {
    int temp_max = max(i, p1.x);
    int temp_min = min(i, p1.x);
    for (int j = temp_min + 1; j < temp_max; j++) //  judge p1 And the inflection point that it corresponds to 
    {
     if (game->map[j][p1.y] != 0)
     {
      b = false;
      break;
     }
    }
   }

   if (b)
   {
    int temp_max = max(p2.x, i);
    int temp_min = min(p2.x, i);
    for (int j = temp_min + 1; j < temp_max; j++)
    {
     if (game->map[j][p2.y] != 0)
     {
      b = false;
      break;
     }
    }
   }
   if (b) //  If a route exists, return true
   {
    conner1.y = p1.y;
    conner1.x = i;
    conner2.y = p2.y;
    conner2.x = i;
    return b;
   }
  }

 } //  End of scan column 

 return b;
}

The complete source code has been uploaded to my GitHub


Related articles: