C++ achieve lianliankan game core code

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

These two days, I have studied the source code of 1 lianlian.com game. I feel it is quite simple, mainly judging whether the two selected pictures can be eliminated. I refer to the source code on the Internet (sorry, I don't remember the url downloaded at that time, I hereby express my deep apologies to the original author!) , then put the core code together as follows, and share with everyone. It should be noted that this is only the code of the core algorithm, the code of interface design and operation has been omitted.


#include <stdlib.h>
#include <time.h>
// Class pictures 
class picture
{
public:
 int type;// The number of the picture, total n , from 0 to n-1
 bool visible;// Is the picture visible 
 int x;// The horizontal position of the picture 
 int y;// The heald coordinates of the picture position 
};
// The whole figure by 8 line 10 Column composition. Each cell is 1 A small picture 
const int pNum = 10;
const int pType = 8;
static picture p[pType][pNum];
// Enter the new 1 guan 
void newStage()
{
 srand(time(0));
 int i,j;
 for(i = 0;i < pType;++i)
 for(j = 0;j < pNum;j++)
 p[i][j].visible = false;
 int x,y;
 for (i = 0;i < pType - 1;++i)
 for(j = 0;j < pNum;++j)
 {
 bool re = true;
 while (re)
 {
 x = rand() % pType;
 y = rand() % pNum;
 if (p[x][y].visible == false)
 {
 p[x][y].type = i;
 p[x][y].visible = true;
 p[x][y].x = x;
 p[x][y].y = y;
 re = false;
 }
 }
 }
 // Deal with the rest of the last 1 Kind of pictures 
 for (i = 0;i < pType;++i)
 for(j = 0;j < pNum;++j)
 {
 if (p[i][j].visible == false)
 {
 p[i][j].type = pType - 1;
 p[i][j].visible = true;
 p[i][j].x = i;
 p[i][j].y = j;
 }
 }
}
 
// in a , b Draw a line between two points 
void drawLine(picture a,picture b)
{
 
}
// Judge pictures a and b Is it possible to pass 1 The lines are connected ( a and b There is between 0 A corner) 
bool matchDirect(picture a,picture b)
{
 if(!(a.x == b.x || a.y == b.y))
 return false;
 //a , b The x-coordinate is the same 
 bool yMatch = true;
 if(a.x == b.x)
 {
 if(a.y > b.y)
 {
 for(int i = b.y + 1;i < a.y;++i)
 {
 if(p[a.x][i].visible == true)
 yMatch = false;
 }
 }
 if(b.y > a.y)
 {
 for(int i = a.y + 1;i < b.y;++i)
 {
 if(p[a.x][i].visible == true)
 yMatch = false;
 }
 }
 }
 //a , b And the y-coordinate is the same 
 bool xMatch = true;
 if(a.y == b.y)
 {
 if(a.x > b.x)
 {
 for(int i = b.x + 1;i < a.x;++i)
 {
 if(p[i][a.y].visible == true)
 xMatch = false;
 }
 }
 if(b.x > a.x)
 {
 for(int i = a.x + 1;i < b.x;++i)
 {
 if(p[i][a.y].visible == true)
 xMatch = false;
 }
 }
 }
 return (xMatch && yMatch);
}
// Judge pictures a and b Between whether can pass 1 Two corners of the polyline connected 
bool matchOneCorner(picture a,picture b)
{
 if (p[a.x][b.y].visible == false && matchDirect(a,p[a.x][b.y]) && matchDirect(p[a.x][b.y],b))
 {
 drawLine(a,p[a.x][b.y]);
 drawLine(p[a.x][b.y],b);
 return true;
 }
 if (p[b.x][a.y].visible == false && matchDirect(a,p[b.x][a.y]) && matchDirect(p[b.x][a.y],b))
 {
 drawLine(a,p[b.x][a.y]);
 drawLine(p[b.x][a.y],b);
 return true;
 }
 return false;
}
// Judge pictures a and b Whether can be connected through two corners of the polyline 
bool matchTwoCorner(picture a,picture b)
{
 int i,j;
 for(i = a.x - 1,j = a.y;i >= 0;--i)
 {
 if(p[i][j].visible == true)
 break;
 else if(matchOneCorner(b,p[i][j]))
 {
 drawLine(a,p[i][j]);
 return true;
 }
 }
 for (i = a.x + 1,j = a.y;i < pNum;++i)
 {
 if(p[i][j].visible == true)
 break;
 else if(matchOneCorner(b,p[i][j]))
 {
 drawLine(a,p[i][j]);
 return true;
 }
 }
 for(i = a.x,j = a.y - 1;j >= 0;--j)
 {
 if(p[i][j].visible == true)
 break;
 else if(matchOneCorner(b,p[i][j]))
 {
 drawLine(a,p[i][j]);
 return true;
 }
 }
 for(i = b.x,j = b.y + 1;j < pType;++j)
 {
 if(p[i][j].visible == true)
 break;
 else if(matchOneCorner(b,p[i][j]))
 {
 drawLine(a,p[i][j]);
 return true;
 }
 }
 return false;
}
// judge a and b Whether it's connected or not a and b Of the same type, and a and b The number of corners between the lines <=2 a 
bool match(picture a,picture b)
{
 if(a.type != b.type)
 return false;
 if(matchDirect(a,b))
 {
 drawLine(a,b);
 return true;
 }
 else if(matchOneCorner(a,b))
 return true;
 else if(matchTwoCorner(a,b))
 return true;
 return false;
}

For more exciting content about C++ mini games, please click on the topic: C++ classic mini games to learn more


Related articles: