2016-11-24 66 views
2

我试图在pygame中使用python制作2048游戏。如果没有给出参数,我希望函数stackright和stackleft在self.data上运行,并且如果参数中给出了矩阵,则返回一个stackrighted或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

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

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

如果没有给出参数,这样的话,那么默认值是无

,那么你知道,如果matrix值为无则主叫方还没有指定的值,所以你用self.data做点什么。但如果他确实指定了一个值(else),那么这意味着调用者指定了一个值,并且您可以对该矩阵执行某些操作。

或者,如果你想使用它们作为相同的值,你可以做到以下几点:

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

现在data是你希望它是

1

像这样的事情可以做的伎俩什么:

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

您可以**kwargs,如果你希望能够通过自己的名字来称呼这些参数也替换*args。在传递参数与None默认值

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

这样做的好处是,它简化了它,当参数的数量增加:你甚至可以同时使用。

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(矩阵2)将在矩阵2上进行操作(同时将分数添加到数据中,除非您更改了上述部分)