【GamePlay】Match 3 Core Algorithm

Preface

Summary of the core algorithm of match-3 games

demand analysis

Here we only talk about the most classic gameplay, which is to eliminate three connected ones of the same color. Other gameplays, such as elimination of the whole row, elimination of the same color in the whole screen, elimination with combos, etc., will be ignored for now.

A match-3 game process:

  1. The stage is randomly generated at the beginning, containing blocks of different colors.
  2. If three blocks of the same color are connected after dragging, they will be eliminated. If there are not, you cannot drag.
  3. Eliminate score calculation and limit the time of a game.

Game flow

Simplified game flow control pseudocode

void Start(){
    
    
	生成舞台;
}

void Update(){
    
    
	if(gameOver){
    
    
	    return;
	}
	if(时间结束){
    
    
		结束UI;
		gameOver = true;
	}
}

In the update, only the game time is judged, the player only has to drag the input, and there are UI events to control it.

Stage generation

First, the size of the stage must be determined, for example, 8*8, so 64 slots must be created in advance.

There must be a slot type, or object type. Note that the object type and object color are different.

Object types can include empty type, ordinary color type, leprosy type, etc., which can provide a variety of gameplay. To simplify, there are only empty types and ordinary color types.

Randomly create objects of different common color types in 64 slots.

The generation animation must be determined. It will be simpler to generate it in place. The more common one is to fall from above.

After generation, the elimination logic must be adjusted first. The specific logic will be discussed later.

全部检测消除(){
    
    
	foreach(方块 in 舞台){
    
    
		if(方块 可消除){
    
    
			获得可消除方块列表,destroy每一个方块,在原位置新建一个空类型方块;
		}
	}
	if(有消除行为){
    
    
		填充;
		全部检测消除;
	}
}

There are also different filling methods. It is the simplest to randomly generate fillings in place, but in common match 3, the top ones fall down, and the newly generated ones are on top.

The falling filling method requires filling line by line, which means it may be filled multiple times.

填充(){
    
    
	for(从倒数第二行开始,遍历每一行)
		for(遍历该行的每一个方块){
    
    
			if(该方块下方的方块类型是空类型){
    
    
				该方块下移,该方块原本位置替换为空类型
			}
		}
	填充第一行;
	if(还有空的){
    
    
		填充;
	}
}

Enter and eliminate

The input of the game is only mouse dragging. The UI events OnMouseEnter, OnMouseDown, and OnMouseUp can be used to determine the currently dragged object and the drag destination.

In this way, you need to add a script to each block object to monitor these three events:
nMouseDown gets the block currently being dragged,
OnMouseEnter gets the current dragging position, and
OnMouseUp indicates the end of dragging and makes elimination judgment.

To determine whether it can be eliminated, check whether the corresponding rows and columns of the two blocks changed after dragging satisfy >= 3 blocks of the same color.

If it can be eliminated, adjust all elimination logic and refill.
The pseudo code is as follows

if(方块1可消除 || 方块2可消除){
    
    
	获得方块1可消除列表,destroy每一个方块,在原位置新建一个空类型方块。
	获得方块2可消除列表,destroy每一个方块,在原位置新建一个空类型方块。
	计算分数;
	填充;
}

The whole thing is roughly like this, with slight differences, but not much difference. The algorithm is like this, but there are also animations, sound effects, etc.

Guess you like

Origin blog.csdn.net/LvPartner/article/details/127821457