类函数中的可选参数

时间:2016-11-24 05:01:00

标签: python python-2.7

我正在尝试使用python在pygame中制作一个2048游戏。如果没有给出参数,我希望函数stackright和stackleft对self.data进行操作,如果在参数中给出了矩阵,我希望返回一个stackedighted或stacklefted矩阵。我会去做这个。我正在使用python 2.7 这是我的代码:

import random
def getnull(): # returns null matrix
    data = [
    [0,0,2,2],
    [0,4,0,2],
    [0,2,0,0],
    [2,0,2,0]
    ]
    return data

class Data:
    def fillrandom(self): #updates data by adding 2/4 randomly
        exit = False
        while not exit:
            y = random.randint(0,3)
            x = random.randint(0,3)
            if self.data[y][x] == 0:
                if random.randint(1,10) == 1:
                    self.data[y][x] = 4
                    exit = True
                else:
                    self.data[y][x] = 2
                    exit = True
    def alignright(self):   #
        list1 = [[],[],[],[]]
        for i in range(4):          # per row loop
            for count in range(4):   # per column loop
                if self.data[i][count] != 0:
                    list1[i] += [self.data[i][count]]
            list1[i] = [0]*(4-len(list1[i])) + list1[i]
        self.data = list1
    def alignleft(self):
        list1 = [[],[],[],[]]
        for i in range(4):          # per row loop
            for count in range(4):   # per column loop
                if self.data[i][count] != 0:
                    list1[i] += [self.data[i][count]]
            list1[i] = list1[i] + [0]*(4-len(list1[i]))
        self.data = list1
    def printstate(self):
        print(self.data[0])
        print(self.data[1])
        print(self.data[2])
        print(self.data[3])
        print("-------")
    def checkfilled(self):
        for count in range(4):
            for i in range(4):
                if self.data[count][i] == 0:
                    return False
        return True
    def stackright(self):
        for i in range(4):
            if self.data[i][3] == self.data[i][2]:
                if self.data[i][1] == self.data[i][0]:
                    self.data[i][3] = self.data[i][3] *2
                    self.score += self.data[i][3]
                    self.data[i][2] = self.data[i][1] *2
                    self.score += self.data[i][2]
                    self.data[i][0] , self.data[i][1] = 0,0
                else:
                    self.data[i][3] = self.data[i][3] *2
                    self.score += self.data[i][3]
                    self.data[i][2] = self.data[i][1]
                    self.data[i][1] = self.data[i][0]
                    self.data[i][0] = 0
            elif self.data[i][2] == self.data[i][1]:
                    self.data[i][2] = self.data[i][2] *2
                    self.score += self.data[i][2]
                    self.data[i][1] = self.data[i][0]
                    self.data[i][0] = 0
            elif self.data[i][1] == self.data[i][0]:
                self.data[i][1] = self.data[i][1] *2
                self.score += self.data[i][1]
                self.data[i][0] = 0
    def stackleft(self):
        for i in range(4):
            if self.data[i][0] == self.data[i][1]:
                if self.data[i][2] == self.data[i][3]:
                    self.data[i][0] = self.data[i][0] *2
                    self.score += self.data[i][0]
                    self.data[i][1] = self.data[i][2] *2
                    self.score += self.data[i][1]
                    self.data[i][3] , self.data[i][2] = 0,0
                else:
                    self.data[i][0] = self.data[i][0]*2
                    self.score += self.data[i][0]
                    self.data[i][1] = self.data[i][2]
                    self.data[i][2] = self.data[i][3]
                    self.data[i][3] = 0
            elif self.data[i][1] == self.data[i][2]:
                    self.data[i][1] = self.data[i][1] *2
                    self.score += self.data[i][1]
                    self.data[i][2] = self.data[i][3]
                    self.data[i][3] = 0
            elif self.data[i][2] == self.data[i][3]:
                self.data[i][2] = self.data[i][2] *2
                self.score += self.data[i][2]
                self.data[i][3] = 0
    def alignup(self):
        col = [[],[],[],[]]
        for i in range(4): #per col loop
            for count in range(4): #per row loop
                if self.data[count][i] != 0:
                    col[i] += [self.data[count][i]]
            col[i] = col[i] + [0]*(4-len(col[i]))
        for i in range(4):      # convert column to row
            for count in range(4):
                self.data[count][i] = col[i][count]
    def aligndown(self):
        col = [[],[],[],[]]
        for i in range(4): #per col loop
            for count in range(4): #per row loop
                if self.data[count][i] != 0:
                    col[i] += [self.data[count][i]]
            col[i] = [0]*(4-len(col[i])) + col[i]
        for i in range(4):       # convert column to row
            for count in range(4):
                self.data[count][i] = col[i][count]
    def __init__(self):
        self.data = getnull()
        self.score = 0
data = Data()
data.aligndown()
data.printstate()
print(data.score)



while True:
    pass

3 个答案:

答案 0 :(得分:3)

您可以拥有默认参数。例如:

def func(self, matrix=None):
    if matrix is None:
        #do stuff with self.data
    else:
        #do stuff

这样,如果没有给出参数,则默认值为None

那么你知道如果matrix的值为None,那么调用者没有指定一个值,所以你用self.data做了一些事情。但如果他确实指定了一个值(else),则表示调用者指定了一个值,你可以对矩阵做一些事情。

或者,如果您想将它们用作相同的值,您可以执行以下操作:

def func(self, matrix=None):
    if matrix is None: matrix = self.data
    #do stuff with the variable 'data'

现在data是您想要的任何内容

答案 1 :(得分:1)

这样的事情可以解决问题:

def stackright(self, *args):
    if len(args) == 0:
        #do things with no arguments
    else:
        #do things with arguments
        print(args[0], args[1], ...)

如果您希望能够按名称调用参数,也可以将*args替换为**kwargs。你甚至可以同时使用两者:

def f(self, madatoryArgument1, mandatoryArgument2, *args, **kwargs)

这比使用None默认值传递参数的优点是,当参数数量增加时,它会简化它。

答案 2 :(得分:0)

看起来你要问的是,你的功能看起来像这样:

def stackright(self, data=None):
    if data is None: data = self.data
    for i in range(4):
        if data[i][3] == data[i][2]:
            if data[i][1] == data[i][0]:
                data[i][3] = data[i][3] *2
                self.score += data[i][3]
                data[i][2] = data[i][1] *2
                self.score += data[i][2]
                data[i][0] , data[i][1] = 0,0
            else:
                data[i][3] = data[i][3] *2
                self.score += data[i][3]
                data[i][2] = data[i][1]
                data[i][1] = data[i][0]
                data[i][0] = 0
        elif data[i][2] == data[i][1]:
                data[i][2] = data[i][2] *2
                self.score += data[i][2]
                data[i][1] = data[i][0]
                data[i][0] = 0
        elif data[i][1] == data[i][0]:
            data[i][1] = data[i][1] *2
            self.score += data[i][1]
            data[i][0] = 0
    return data
def stackleft(self, data=None):
    if data == None: data = self.data
    for i in range(4):
        if data[i][0] == data[i][1]:
            if data[i][2] == data[i][3]:
                data[i][0] = data[i][0] *2
                self.score += data[i][0]
                data[i][1] = data[i][2] *2
                self.score += data[i][1]
                data[i][3] , data[i][2] = 0,0
            else:
                data[i][0] = data[i][0]*2
                self.score += data[i][0]
                data[i][1] = data[i][2]
                data[i][2] = data[i][3]
                data[i][3] = 0
        elif data[i][1] == data[i][2]:
                data[i][1] = data[i][1] *2
                self.score += data[i][1]
                data[i][2] = data[i][3]
                data[i][3] = 0
        elif data[i][2] == data[i][3]:
            data[i][2] = data[i][2] *2
            self.score += data[i][2]
            data[i][3] = 0
    return data

然后调用data.stackright()将对自身数据进行操作,但调用data.stackright(matrix2)将对matrix2进行操作(同时将分数添加到数据中,除非您更改上面的部分)