2016-11-24 64 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上進行操作(同時將分數添加到數據中,除非您更改了上述部分)