efficient general algorithm for merging tiles in game 2048

I have already checked many posts such as the one here, but I don't think they have what I look for. I want to create a merging function for the game 2048, and I want it the algorithm to be as general as possible: it should work for the traditional 4x4 2048, as well as the less common 3x3, 5x5 and so on. I also want to use only the most basic functions such as if-else (I am actually coding in a microprocessor using assembler language, so I can't use any fancy routines or functions in python). Are there any efficient, elegant solutions?

I started with the simpler 3x3 version of it, and for a swipe right, I just start checking empty tiles from the right, and then check neighbouring tiles for same numbers. I end up having 8 different cases, and once I have found out which case it belongs to, I can do the moving/combine. The problem with this is that I will need to look figure out the possible cases for 4x4 and 5x5 as well, and the problem become very complicated very fast. It is also hard to re-use the code written for 3x3 to deal with 4x4 and 5x5.

I also tried checking for empty tiles, tiles with same number, starting from the rightmost tiles (again for swipe right), and then loop it once (maybe need to loop more than twice for bigger size grids?) This approach should work for any number of game size (3x3, 4x4, 5x5, etc), but it seems that it is a bit inefficient and I suspect there are some better ways of doing it.

So are there are elegant ways of doing the checking and merging in the game 2048?

链接地址: http://www.djcxy.com/p/40214.html

上一篇: 在2048年的实现中,如何确定哪些图块移动和合并?

下一篇: 在2048年游戏中合并拼贴的高效通用算法