我正在開發一個使用pygame的N-Queen模擬。如何將python列表動態地傳遞給另一個python文件
class NQ:
def __init__(self,n):
self.size = n
self.columns = [] * self.size
self.places = 0
self.backtracks = 0
def place(self, startRow=0):
if len(self.columns) == self.size:
return self.columns
else:
for row in range(startRow, self.size):
if self.isSafe(len(self.columns), row) is True:
self.columns.append(row)
self.places += 1
return self.place()
else:
lastRow = self.columns.pop()
self.backtracks += 1
return self.place(startRow=lastRow + 1)
def isSafe(self, col, row):
for threatRow in self.columns:
threatCol = self.columns.index(threatRow)
if row == threatRow or col == self.columns.index(threatRow):
return False
elif threatRow + threatCol == row + col or threatRow - threatCol == row - col:
return False
return True
def process(n):
nqueens = NQ(n)
nqueens.place(0)
return nqueens.columns
我也有另一個文件中的pygame的步驟繪製棋盤這需要一個列表作爲輸入,並相應地把它們。 如果我想顯示皇后的運動,我如何從遞歸code
程序動態地傳遞列表,以便可以看到確切的回溯程序。 謝謝
將兩個程序同時運行?如果沒有,你可以[列出](http://docs.python.org/2/library/pickle.html)列表並將它保存在磁盤上,pygame過程可以取消數據並執行任何操作。 –
另一個Python文件?你的意思是[模塊](http://docs.python.org/2/tutorial/modules.html)?也許你會簡單地導入模塊並調用函數?請澄清我是否誤解了你。 –