2048,移位功能(向上,向右,向下,向左)

时间:2017-03-20 18:36:56

标签: python

首先抱歉我的英语非常糟糕,我会尽力直截了当 所以,首先,如果你看到一些功能,我可以用一种简单的方式说我, 但真正的问题是,考虑到2048规则,如何进行置换功能

感谢您阅读

    import random

def creatgrid(n,val):
    grille=[]
    interior=[]
    for i in range(n):
        for m in range(n):
            interior+=val,
        grille+=interior,
        interior=[]
    return(grille)

def console(grille):
    res=""
    for i in grille:
        for n in i:
            n=str(n)
            if len(n)==1:
                res+=n+"    "
            if len(n)==2:
                res+=n+"   "
            if len(n)==3:
                res+=n+"  "
            if len(n)==4:
                res+=n+" "
        print(res)
        print("")
        res=""

def is_in_test(x,grille):
    global test
    for i in grille:
        for n in i:
            test=bool(str(x)==str(n))

def win(grille):
    appartient(2048,grille)
    if test:
        res="gagné"
    else:
        res="perdu"
    return(res)

def full(grille):
    appartient(0,grille)
    if test==False:
        print("pleine")
def max(g):
    res=0
    maxi=2
    for i in grille:
        for n in i:
            if int(n)>=maxi:
                res=n
            maxi=maxi**2
    return(res)

def cases(grille,val):
    global cases
    cases=[]
    x=0
    y=0
    for i in grille:
        for n in i:
            if str(val)==str(n):
                cases+=[x,y],
            y+=1
        y=0
        x+=1
    return(cases)

def empty(grille):
    vides=cases(grille,0)
    return(vides)


def addRandom(grille,val):
    coord=lescases(grille,0)
    coord=random.choice(coord)
    res=[]
    interior=[]
    x=0
    y=0
    for i in grille:
        for n in i:
            if x==coord[0] and y==coord[1]:
                interior+=str(val),
            else:
                interior+=n,
            y+=1
        res+=interior,
        interior=[]
        y=0
        x+=1
    grille=res
    return(grille)

def init(n):
    global grille
    d1=random.choice(["4","2"])
    d2=random.choice(["4","2"])
    grille=creeGrille(n,0)
    grille=ajoutAlea(grille,d1)
    grille=ajoutAlea(grille,d2)
    return(grille)

def haut(grille):

0 个答案:

没有答案