亲宝软件园·资讯

展开

MFC实现连连看游戏 MFC实现连连看游戏之消子算法

StriverLi 人气:0

两个位置的图片能否消除,有三种情况:

1.一条直线连接,这种也是最简单的一种消除方法

bool LinkInLine(CPoint p1, CPoint p2) 
{
 conner1.x = conner1.y = -1; // 记录拐点位置
 conner2.x = conner2.y = -1;

 BOOL b = true;
 if (p1.y == p2.y) // 两个点再同一行
 {
  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) // 在同一列
 {
  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 // 不在同一直线
 {
  b = false;
 }
 return b;
}

2.两条直线消除,即经过一个拐点。

两个顶点经过两条直线连接有两种情况,即两个拐点分两种情况。

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);

 // 拐点1
 int x1 = p1.x;
 int y1 = p2.y;
 //拐点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) // 拐点1位置无图片
  {
   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) // 拐点2位置无图片
  {
   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.三条直线消除,即经过两个拐点。

这是可以通过横向扫描和纵向扫描,扫描的时候可以得到连个拐点,判断两个顶点经过这两个拐点后是否能消除

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++) // 扫描行
 {
  b = true;
  if (game->map[p1.x][i] == 0 && game->map[p2.x][i] == 0) // 两个拐点位置无图片
  {
   for (int j = min_x + 1; j < max_x; j++) // 判断连个拐点之间是否可以连接
   {
    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++) // 判断p1和它所对应的拐点之间是否可以连接
    {
     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++) // 判断p2和它所对应的拐点之间是否可以连接
    {
     for (int j = temp_min + 1; j < temp_max; j++)
     {
      if (game->map[p2.x][j] != 0)
      {
       b = false;
       break;
      }
     }
    }
   }
   if (b) // 如果存在路线,返回true
   {
    conner1.x = p1.x;
    conner1.y = i;
    conner2.x = p2.x;
    conner2.y = i;
    return b;
   }
  } 

 }// 扫描行结束


 for (int i = 0; i < MAX_X; i++) // 扫描列
 {
  b = true;
  if (game->map[i][p1.y] == 0 && game->map[i][p2.y] == 0) // 连个拐点位置无图片
  {
   for (int j = min_y + 1; j < max_y; j++) // 两个拐点之间是否可以连接
   {
    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++) // 判断p1和它所对应的拐点之间是否可以连接
    {
     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) // 如果存在路线,返回true
   {
    conner1.y = p1.y;
    conner1.x = i;
    conner2.y = p2.y;
    conner2.x = i;
    return b;
   }
  }

 } // 扫描列结束

 return b;
}

完整源码已上传至我的GitHub

加载全部内容

相关教程
猜你喜欢
用户评论