1
用数组存储麻将 Store mahjongs in array
首先,搓麻将之前要设法用矩阵储存关卡阵型,每关对应一个三维数组,三个维度依次代表层-行-列。为了表示微软麻将块,可让每块麻将在一层数组中占2x2的空间,用数字代表麻将的左上角,0代表无麻将。
First, we need to use a 3D-array to store the layout of a level. Its dimensions are floor, row and column. To express Shanghai Mahjong, we can assume that each mahjong holds a 2x2 area in a floor of array. A number in array represents the left-upper corner of a mahjong, but 0 means void.
[[0,0,1,0,0],
[2,0,0,0,0],
[0,0,0,4,0]
[0,3,0,0,0],
[0,0,0,0,0]]
判断麻将渲染顺序 Render mahjongs in correct order
在相邻的麻将中,右边的麻将应该比左边的先渲染,上面的比下面的先渲染。麻将的阵型可能很复杂,不易直接确定渲染顺序,但可以通过冒泡排序来确定这一顺序。不过很显然的是,底层麻将总是比顶层先渲染。
Among the neighbours of each mahjong, the left one is always rendered after the right one, and the upper one is always rendered after the lower one. The layout of mahjong may be too complex to determine the rendering order directly, but such order can be found easily by bubble sorting. At the same time, it is obvious that the top layer of mahjongs are always rendered after the bottom layer.
判断麻将能否拿起 Decide whether a mahjong can be chosen
然后确定每个麻将块能否被消除。只要麻将块的左邻三格和右邻三格都至少有一个麻将,它就会被夹住不能拿;若它顶层九邻格有麻将块,也会被压住不能拿。
To match mahjongs you must decide whether it can be chosen first. If a mahjong has both left and right neighbour, it will be blocked. If it has a top neighbour, it will also be blocked.
确保关卡有解 Ensure a level is solvable
最后要保证洗牌后有解。首先从能拿的麻将块中随机挑出两个,将它们刷上同一图案;再将这两块麻将移走,从剩余麻将块中能拿的部分里挑两个并刷上同一图案,如此往复,直到所有麻将块都刷上图案。麻将块的总数应是偶数,若三消(N消)则应是3(N)的倍数。若麻将块的花色已确定就不用考虑,比如道具牌。
Finally, to ensure a level is solvable, you can randomly pick a pair of mahjong from all matchable mahjongs in a temporary layout copied from the original layout, and give them a pair of same pattern. Then, remove the pair from the temporary layout and repeat it, until all mahjongs have pattern. If the rule is match-N, the total number of mahjongs should be divided by N. If a pattern of mahjong is already set in the layout, such as bonus mahjong, it should be ignored when deciding the patterns of mahjong.
Leave a comment
Log in with itch.io to leave a comment.