Principle_of_Computing Assignment 1: 2048_Merge

"""
Merge function for 2048 game.
"""

def merge(line):
    """
    Function that merges a single row or column in 2048.
    """
    aline=[]
    for i in range(len(line)):
        if line[i]!=0:
            aline.append(line[i])
    n=len(line)-len(aline)
    i=1
    while i<=len(aline)-1:
        if aline[i-1]==aline[i]:
            aline[i-1]=aline[i-1]*2
            aline[i]=0
            i=i+2
        else:
            i=i+1
    for i in range(len(aline)):
        if aline[i]==0:
            j=i
            while j<len(aline)-1:
                aline[j]=aline[j+1]
                j=j+1
            aline[len(aline)-1]=0
    n=len(line)-len(aline)
    if n>0:
        for i in range(n):
            aline.append(0)
    return aline
阅读更多
文章标签: coursera python
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭