C++连连看判定图形消除算法

我们玩过的连连看游戏,通过选定两个图形相同的元素,判定其是否可在三次转弯内连接起来,若能,则消去,若不能,则不可消去,直至最后全部消除。

本算法中不包括关于死锁状态判定。

// 连连看.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include 
#include 
#include 
 
 
using namespace std;
 
struct Point
{
  int x;
  int y;
};
 
int pushonelinespot(Point a,Point b,int c[6][6], queue &g)  //将a点可直线到达的点压入队列g中
{
  int i;
  Point temp;
  for(i=1;;i++)  //向下检测
   { if((a.x+i)<=5&&c[a.x+i][a.y]==0)
      { 
    temp.x=a.x+i;temp.y=a.y;  
     g.push(temp);}  
     else if(c[a.x+i][a.y]!=0&&(a.x+i)<=5)
      {if((a.x+i)==b.x&&a.y==b.y) return 1;
       else break;}
     else break;
    }
   for(i=-1;;i--)   //向上检测
   { if(c[a.x+i][a.y]==0&&(a.x+i)>=0)
      { temp.x=a.x+i;temp.y=a.y;  
       g.push(temp);}
     else if(c[a.x+i][a.y]!=0&&(a.x+i)>=0)
      {if((a.x+i)==b.x&&a.y==b.y) return 1;
       else break;}
     else break;
    }
   for(i=1;;i++)   //向右检测
   { if(c[a.x][a.y+i]==0&&(a.y+i)<=5)
      { temp.x=a.x;temp.y=a.y+i;  
       g.push(temp);}
     else if(c[a.x][a.y+i]!=0&&(a.y+i)<=5)
      {if(a.x==b.x&&(a.y+i)==b.y) return 1;
       else break;}
     else break;
    }
   for(i=-1;;i--)  //向左检测
   { if(c[a.x][a.y+i]==0&&(a.y+i)>=0)
      { temp.x=a.x;temp.y=a.y+i;  
       g.push(temp);}
     else if(c[a.x][a.y+i]!=0&&(a.y+i)>=0)
      {if(a.x==b.x&&(a.y+i)==b.y) return 1;
       else break;}
     else break;
    }
   return 0;
}
 
bool shortestline(Point a,Point b,int c[6][6])
{
  if(c[a.x][a.y]==c[b.x][b.y])
  {
  Point temp;
  queue X,Y,Z;
  int i;
  i=pushonelinespot(a,b,c,X);
  if(i==1) return 1;
  while(!X.empty())
   { temp=X.front();X.pop();
    i=pushonelinespot(temp,b,c,Y);
    if(i==1) return 1; }  //第一次转弯
   while(!Y.empty())
    { temp=Y.front();Y.pop();
     i=pushonelinespot(temp,b,c,Z);
     if(i==1) return 1;}  //第二次转弯
 cout<<"转弯路径大于两次"<>value)           //输入控制
    {
      for(i=0;i<6;i++)
       {for(j=0;j<6;j++)
         c[i][j]=value;}
    } //初始化二维数组;//while*/ 
 for(i=0;i<6;i++)
    {
   for(j=0;j<6;j++)
   cout<<"    "<>a.x>>a.y>>b.x>>b.y;
   if(a.x>0&&a.x<5&&a.y>0&&a.y<5&&b.x>0&&b.x<5&&b.y>0&&b.y<5)
   { 
 if(a.x==b.x&&a.y==b.y) {cout<<"不可输入相同坐标,请重新输入:"< 
 

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

你可能感兴趣的:(C++连连看判定图形消除算法)