I have studied the source code of Lianlian to watch the game in the past two days and felt that it is quite simple. It mainly determines whether the two selected pictures can be eliminated. I referred to the source code on the Internet (sorry, I don’t remember the downloaded URL at that time, so I deeply apologize to the original author!), and then sorted out the core code as follows and shared it with everyone. It should be noted that this is just the code of the core algorithm, and the code of interface design and operation has been omitted.
#include <> #include <> //Picturesclass picture { public: int type;//There are n types of pictures, from 0 to n-1 bool visible;//Is the picture visible int x;//The horizontal coordinate of the image position int y;//The coordinates of the image position}; //The whole picture consists of 8 rows and 10 columns, and each cell is a small pictureconst int pNum = 10; const int pType = 8; static picture p[pType][pNum]; //Enter new levelvoid 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; } } } //Process the remaining last picture 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; } } } //Draw lines between points a and bvoid drawLine(picture a,picture b) { } //Judge whether the pictures a and b can be connected by a straight line (there are 0 corners between a and b)bool matchDirect(picture a,picture b) { if(!( == || == )) return false; //When the horizontal coordinates of a and b are the same bool yMatch = true; if( == ) { if( > ) { for(int i = + 1;i < ;++i) { if(p[][i].visible == true) yMatch = false; } } if( > ) { for(int i = + 1;i < ;++i) { if(p[][i].visible == true) yMatch = false; } } } //When the vertical coordinates of a and b are the same bool xMatch = true; if( == ) { if( > ) { for(int i = + 1;i < ;++i) { if(p[i][].visible == true) xMatch = false; } } if( > ) { for(int i = + 1;i < ;++i) { if(p[i][].visible == true) xMatch = false; } } } return (xMatch && yMatch); } //Judge whether pictures a and b can be connected by a corner polylinebool matchOneCorner(picture a,picture b) { if (p[][].visible == false && matchDirect(a,p[][]) && matchDirect(p[][],b)) { drawLine(a,p[][]); drawLine(p[][],b); return true; } if (p[][].visible == false && matchDirect(a,p[][]) && matchDirect(p[][],b)) { drawLine(a,p[][]); drawLine(p[][],b); return true; } return false; } //Judge whether pictures a and b can be connected by two corners of the folded linesbool matchTwoCorner(picture a,picture b) { int i,j; for(i = - 1,j = ;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 = + 1,j = ;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 = ,j = - 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 = ,j = + 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 whether a and b can be connected, the condition is that the types of a and b are the same, and the number of corners of the connection between a and b is <= 2bool match(picture a,picture b) { if( != ) 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 Game"Learn to understand
The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.