Just a few days ago in learning cocos2d-x, bored to do a similar Diamond dash elimination game, today put online to share with you. I believe that Diamond dash is a game that everyone has played. The game rules are as follows: there is a 7*8 board, each grid has blocks of different colors (five colors are selected here). When you click a square, if the number of blocks connected to it is greater than or equal to three, then, all the connected blocks will be deleted, and the blocks on these blocks will fall down to supplement these blocks. At the same time, after the square falls down, it creates a vacancy on it. Then, a random square is generated to complete the vacancy. The score for each elimination is 2 ^ n, and n is the number of consecutive squares, the game time is 1 minute.
In the game design process, the most important thing is the game logic design. The logic of this game is very clear. First, a Board is randomly generated, and a random color block is placed in the board, there is no limit on the placement of squares (unlike a three-game, the generated checkerboard cannot have the same color as the adjacent three in the row or column). The Code for randomly generated squares is as follows:
[Cpp]
Void GameLogic: init ()
{
Srand (unsigned) time (0 ));
For (int I = 0; I <WIDTH_CNT; I ++)
{
Lack [I] = 0;
For (int j = 0; j <HEIGHT_CNT; j ++)
{
Blocks [I] [j]. moveToY = j;
Blocks [I] [j]. color = rand () % colorCount;
}
}
Memset (visit, false, sizeof (visit ));
TotalScore = 0;
}
Void GameLogic: init ()
{
Srand (unsigned) time (0 ));
For (int I = 0; I <WIDTH_CNT; I ++)
{
Lack [I] = 0;
For (int j = 0; j <HEIGHT_CNT; j ++)
{
Blocks [I] [j]. moveToY = j;
Blocks [I] [j]. color = rand () % colorCount;
}
}
Memset (visit, false, sizeof (visit ));
TotalScore = 0;
}
Then, when you click a square in it, you need to calculate the number of squares of the same color adjacent to the square. Here, we use depth-first traversal to count the number of such squares, the positions of these blocks in the checker are marked with visit.
[Cpp]
Int GameLogic: dfs (int I, int j)
{
Int ret = 1;
Visit [I] [j] = true;
For (int p = 0; p <4; p ++)
{
Int x = I + dir [p] [0];
Int y = j + dir [p] [1];
If (x> = WIDTH_CNT | x <0)
Continue;
If (y> = HEIGHT_CNT | y <0)
Continue;
If (! Visit [x] [y] & blocks [x] [y]. color = blocks [I] [j]. color)
{
Ret + = dfs (x, y );
}
}
Return ret;
}
Int GameLogic: dfs (int I, int j)
{
Int ret = 1;
Visit [I] [j] = true;
For (int p = 0; p <4; p ++)
{
Int x = I + dir [p] [0];
Int y = j + dir [p] [1];
If (x> = WIDTH_CNT | x <0)
Continue;
If (y> = HEIGHT_CNT | y <0)
Continue;
If (! Visit [x] [y] & blocks [x] [y]. color = blocks [I] [j]. color)
{
Ret + = dfs (x, y );
}
}
Return ret;
}
The next step is to remove the marked blocks, which is more difficult in the game, because to achieve the effect of moving blocks, I implement this: the location where each square Falls is related to the number of squares that are eliminated below it, and the number of games that are eliminated under each square is recorded. The current location minus this number is the location where the square needs to be moved, and record the location.
[Cpp]
Void GameLogic: blocksCancel ()
{
For (int I = 0; I <WIDTH_CNT; I ++)
{
For (int j = 0; j <HEIGHT_CNT; j ++)
{
If (visit [I] [j])
{
Lack [I] ++;
}
Else
{
Blocks [I] [j]. moveToY = j-lack [I];
Blocks [I] [j-lack [I]. color = blocks [I] [j]. color;
}
}
}
}
Void GameLogic: blocksCancel ()
{
For (int I = 0; I <WIDTH_CNT; I ++)
{
For (int j = 0; j <HEIGHT_CNT; j ++)
{
If (visit [I] [j])
{
Lack [I] ++;
}
Else
{
Blocks [I] [j]. moveToY = j-lack [I];
Blocks [I] [j-lack [I]. color = blocks [I] [j]. color;
}
}
}
}
After the drop, the blocks above are filled and are also randomly generated.
[Cpp]
Void GameLogic: makeUpBlocks ()
{
Srand (unsigned) time (0 ));
For (int I = 0; I <WIDTH_CNT; I ++)
{
For (int j = HEIGHT_CNT-lack [I]; j <HEIGHT_CNT; j ++)
{
Blocks [I] [j]. color = rand () % colorCount;
}
}
}
Void GameLogic: makeUpBlocks ()
{
Srand (unsigned) time (0 ));
For (int I = 0; I <WIDTH_CNT; I ++)
{
For (int j = HEIGHT_CNT-lack [I]; j <HEIGHT_CNT; j ++)
{
Blocks [I] [j]. color = rand () % colorCount;
}
}
} Before the last click, do not forget to restore the status to the initial state.
Games such:
This is before running
The time is up. The game is over:
Recently, I have been studying another game, Sanxiao game. The logic is slightly more complex than this.