百行内实现2048 python 不带ui的

2048是一款数字益智游戏,游戏规则如下:
游戏界面是一个44的格子,开始时会有两个数字方块,数字分别为2或4。
使用numpy 随机生成2或4 16 个数字 reshape(4,4) 初始化数字完成
手指滑动屏幕上、下、左、右四个方向,数字方块会沿着滑动方向移动,直到碰到边界或遇到另一个数字方块。
上下按照行检测 左右按照列检测 每次滑动按照该方向往尾部检测到两个相同的合并
如果两个数字方块数字相同,它们会合并成一个数字方块,数字变为原来的两倍。
相同择
2
每次移动后会随机生成一个2或4的数字方块,使得格子中的数字方块数量不变。
每次随机生成合并个数个2或4 填充到 方向尾部由于合并空出的格子中
当格子被数字方块填满,且没有可合并的数字时,游戏结束。
目标是通过不断合并数字方块,得到一个数字为2048的方块,如果达到目标,则游戏胜利。
在游戏过程中,数字方块的合并次数越多,分数越高。
希望这些解释能让你更好地理解游戏规则!

import numpy as np


class Game:
    def __init__(self):
        # 初始化参数

        self.grid_basic = np.zeros([4, 4])
        self.grid_basic[np.random.randint(0, 4, 1), np.random.randint(0, 4, 1)] = np.random.choice([2, 4], 1)
        self.grid_basic[np.random.randint(0, 4, 1), np.random.randint(0, 4, 1)] = np.random.choice([2, 4], 1)
        print(self.grid_basic)

    def up_grid(self):
        # 开始滑动
        # 上 行检测
        for row in range(self.grid_basic.shape[0] - 1):
            grid_basic_bool = self.grid_basic[row] == self.grid_basic[row + 1]
            if sum(grid_basic_bool) > 0:
                print(row, row + 1)
                col = grid_basic_bool.copy()
                grid_basic_bool = grid_basic_bool * 2
                grid_basic_bool[grid_basic_bool == 0] = 1
                self.grid_basic[row] = self.grid_basic[row] * grid_basic_bool
                if row + 2 < self.grid_basic.shape[0]:
                    self.grid_basic[row + 1:row + 3, col] = self.grid_basic[row + 2:, col]

                self.grid_basic[-1, np.random.randint(0, 4, 1)] = np.random.choice([2, 4], 1)
                self.grid_basic[-1, np.random.randint(0, 4, 1)] = np.random.choice([2, 4], 1)
                # print(self.grid_basic)
                break

    def down_grid(self):
        # 下
        # 将数组向左旋转两次 执行上 操作方可  执行后再旋转两次 还原方可
        self.grid_basic = self.flip90_left(self.grid_basic)
        self.grid_basic = self.flip90_left(self.grid_basic)
        self.up_grid()
        self.grid_basic = self.flip90_left(self.grid_basic)
        self.grid_basic = self.flip90_left(self.grid_basic)

    def left_grid(self):
        self.grid_basic = self.flip90_left(self.grid_basic)
        self.grid_basic = self.flip90_left(self.grid_basic)
        self.grid_basic = self.flip90_left(self.grid_basic)
        self.up_grid()
        self.grid_basic = self.flip90_left(self.grid_basic)

    def right_grid(self):
        self.grid_basic = self.flip90_left(self.grid_basic)
        self.up_grid()
        self.grid_basic = self.flip90_left(self.grid_basic)
        self.grid_basic = self.flip90_left(self.grid_basic)
        self.grid_basic = self.flip90_left(self.grid_basic)

    @staticmethod
    def flip90_left(arr):
        new_arr = np.transpose(arr)
        new_arr = new_arr[::-1]
        return new_arr

    def game_over(self):
        current = self.grid_basic.copy()
        game.up_grid()
        game.left_grid()
        game.right_grid()
        game.down_grid()
        if np.sum(current == self.grid_basic) == current.size():
            return "game_over"
        else:
            self.grid_basic = current.copy()

    def game_vinner(self):
        if np.sum(self.grid_basic == 2048) > 0:
            return "game_vinner"


if __name__ == '__main__':
    game = Game()
    game.up_grid()
    game.left_grid()
    game.right_grid()
    game.down_grid()
    print(game.grid_basic)

你可能感兴趣的:(日常,python,numpy,开发语言)