《2048》是一款比较流行的数字游戏,最早于2014年3月20日发行。原版2048首先在GitHub上发布,原作者是Gabriele Cirulli,后被移植到各个平台。这款游戏是基于《1024》和《小3传奇》的玩法开发而成的新型数字游戏。
操作指南:
每次可以选择上下左右其中一个方向去滑动,每滑动一次,所有的数字方块都会往滑动的方向靠拢外,系统也会在空白的地方乱数出现一个数字方块,相同数字的方块在靠拢、相撞时会相加。不断的叠加最终拼凑出2048这个数字就算成功。
游戏技巧:
-
最大数尽可能放在角落。
-
数字按顺序紧邻排列。
-
首先满足最大数和次大数在的那一列/行是满的。
-
时刻注意活动较大数(32以上)旁边要有相近的数。
-
以大数所在的一行为主要移动方向
-
不要急于“清理桌面”。
完整代码如下:
logic.py
import random
import constants as c
def new_game(n):
matrix = []
for i in range(n):
matrix.append([0] * n)
matrix = add_two(matrix)
matrix = add_two(matrix)
return matrix
def add_two(mat):
a = random.randint(0, len(mat)-1)
b = random.randint(0, len(mat)-1)
while mat[a][b] != 0:
a = random.randint(0, len(mat)-1)
b = random.randint(0, len(mat)-1)
mat[a][b] = 2
return mat
def game_state(mat):
# check for win cell
for i in range(len(mat)):
for j in range(len(mat[0])):
if mat[i][j] == 2048:
return 'win'
# check for any zero entries
for i in range(len(mat)):
for j in range(len(mat[0])):
if mat[i][j] == 0:
return 'not over'
# check for same cells that touch each other
for i in range(len(mat)-1):
# intentionally reduced to check the row on the right and below
# more elegant to use exceptions but most likely this will be their solution
for j in r