import random SIZE = 4 map = [[0 for i in range(SIZE)] for i in range(SIZE)] score = 0 # 展示矩阵 def show(M): print("n" + "使用W A S D控制方向", end="") print("n" + " " * 25 + "Score: " + "{:}".format(score), end="") for i in range(SIZE): print("n") for j in range(SIZE): print("{: >6}".format(M[i][j]), end="") print("n") def add(M): cnt = 0 while True: p = random.randint(0, SIZE * SIZE - 1) if M[p // SIZE][p % SIZE] == 0: x = random.choice([2, 2, 2, 4]) M[p // SIZE][p % SIZE] = x cnt += 1 if cnt == 2: break return M def over(M): for r in range(SIZE): for c in range(SIZE): if M[r][c] == 0: return False for r in range(SIZE): for c in range(SIZE - 1): if M[r][c] == M[r][c + 1]: return False for r in range(SIZE - 1): for c in range(SIZE): if M[r][c] == M[r + 1][c]: return False return True def caculate(M): global score changed = False for a in M: b = [] last = 0 for v in a: if v != 0: if v == last: s = b.pop() * 2 b.append(s) score += s last = 0 else: b.append(v) last = v b += [0] * (SIZE - len(b)) # 弥补本行剩下的元素 for i in range(SIZE): if a[i] != b[i]: changed = True a[:] = b return M, changed def rotate90(M): M = [[M[c][r] for c in range(SIZE)] for r in reversed(range(SIZE))] return M def moveUp(): global map map = rotate90(map) if caculate(map)[1]: map = add(map) map = rotate90(map) map = rotate90(map) map = rotate90(map) def moveRight(): global map map = rotate90(map) map = rotate90(map) if caculate(map)[1]: map = add(map) map = rotate90(map) map = rotate90(map) def moveDown(): global map map = rotate90(map) map = rotate90(map) map = rotate90(map) if caculate(map)[1]: map = add(map) map = rotate90(map) def moveLeft(): global map if caculate(map)[1]: map = add(map) map = add(map) show(map) while not over(map): cmd = input() if cmd == "w": moveUp() if cmd == 's': moveDown() if cmd == 'a': moveLeft() if cmd == 'd': moveRight() show(map)
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)