今日代码

本文介绍了使用Python实现的四个基本操作函数,针对2048游戏矩阵进行增删乘运算,提升数字并检查游戏状态。通过right(), left(), down(), up()函数实现游戏规则,游戏胜利条件和结束判断。
摘要由CSDN通过智能技术生成

1.今日练习

import random
import sys
def right(nums):
    for i in [0,1,2,3]:
        j = 3
        while j >= 0:
            if nums[i][j] == 0:
                j -= 1
            elif j > 0 and nums[i][j] == nums[i][j-1]:
                nums[i][j] *= 2
                nums[i][j-1] = 0
                j -= 2
            elif j > 1 and nums[i][j-1] == 0 and nums[i][j] == nums[i][j-2]:
                nums[i][j] *= 2
                nums[i][j - 2] = 0
                j = -1
            elif j == 3 and nums[i][j] == nums[i][0] and nums[i][j-1] == 0 \
                    and nums[i][j-2] == 0:
                nums[i][j] *= 2
                nums[i][0] = 0
                j = -1
            else:
                j -= 1
        j = 0
        n = []
        while j <= 3:
            if nums[i][j] != 0:
                n.append(nums[i][j])
                nums[i][j] = 0
            j += 1
        m = len(n)
        for i1 in n:
            nums[i][3-m+1] = i1
            m -= 1
def left(nums):
    for i in [0, 1, 2, 3]:
        j = 0
        while j <= 3:
            if nums[i][j] == 0:
                j += 1
            elif j < 3 and nums[i][j] == nums[i][j+1]:
                nums[i][j] *= 2
                nums[i][j+1] = 0
                j += 2
            elif j < 2 and nums[i][j+1] == 0 and nums[i][j] ==nums[i][j+2]:
                nums[i][j] *= 2
                nums[i][j + 2] = 0
                j = 4
            elif j == 0 and nums[i][j] == nums[i][3] and nums[i][j+1] == 0\
                and nums[i][j+2] == 0:
                nums[i][j] *= 2
                nums[i][3] =0
                j = 4
            else:
                j += 1
        j = 0
        n = []
        while j <= 3:
            if nums[i][j] != 0:
                n.append(nums[i][j])
                nums[i][j] = 0
            j += 1
        m = 0
        for i1 in n:
            nums[i][m] = i1
            m += 1
def down(nums):
    for j in [0,1,2,3]:
        i = 3
        while i>=0:
            if nums[i][j] == 0:
                i -= 1
            elif i> 0 and nums[i][j] == nums[i-1][j]:
                nums[i][j] *= 2
                nums[i-1][j] = 0
                i -= 2
            elif i>1 and nums[i-1][j] == 0 and nums[i][j] == nums[i-2][j]:
                nums[i][j] *= 2
                nums[i-2][j] = 0
                i -= 1
            elif i == 3 and nums[i][j] == nums[0][j] and nums[i-1][j] == 0\
                and nums[i-2][j] == 0:
                nums[i][j] *= 2
                nums[0][j] = 0
                i = -1
            else:
                i -= 1
        i = 0
        n = []
        while i <= 3:
            if nums[i][j] != 0:
                n.append(nums[i][j])
                nums[i][j] = 0
            i += 1
        m = len(n)
        for i1 in n:
            nums[3-m+1][j] = i1
            m -= 1
def up(nums):
    for j in [0,1,2,3]:
        i = 0
        while i <= 3:
            if nums[i][j] == 0:
                i += 1
            elif i<3 and nums[i][j] == nums[i+1][j]:
                nums[i][j] *= 2
                nums[i + 1][j] = 0
                i += 2
            elif i < 2 and nums[i+1][j] == 0 and nums[i][j] == nums[i+2][j]:
                nums[i][j] *= 2
                nums[i + 2][j] = 0
                i += 1
            elif i ==0 and nums[i][j] ==nums[3][j] and nums[i+1][j] == 0\
                and nums[i+2][j] == 0:
                nums[i][j] *= 2
                nums[3][j] = 0
                i = 4
            else:
                i += 1
        i = 0
        n = []
        while i <= 3:
            if nums[i][j] != 0:
                n.append(nums[i][j])
                nums[i][j] = 0
            i += 1
        m = 0
        for i1 in n:
            nums[m][j] = i1
            m += 1
def addnum(nums):
    s = []
    for i in range(4):
        for j in range(4):
            if nums[i][j] == 0:
                s.append((i, j))
            if nums[i][j] == 2048:
                print("游戏胜利!")
                sys.exit()
    if len(s) == 0:
        print("游戏结束!")
        sys.exit()
    x = len(s)
    n = random.randint(0, x-1)
    ij = s[n]
    i = int(ij[0])
    j = int(ij[1])
    nums[i][j] = random.choice([2, 4])
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值