使用Python的康威生活游戏没有正确更新

时间:2016-10-08 04:27:21

标签: python

请耐心等待,因为我正在学习Python课程。我正在重新创造Conway在Python中的生活游戏。我正在努力弄清楚如何更新电路板(嵌套阵列) - 我正在尝试一个只有原始值的电路板和另一个具有更新值的电路板。我这样做的目的是防止更新的细胞影响其他细胞。即使我用一个值而不是另一个更新一个电路板,它们最终会有相同的值。

在代码中,1代表活细胞,0代表死细胞。主循环位于底部。课程CheckState接受两个主板upboardboard。正如您在函数rules中看到的那样,board未更新。但是,upboard接受函数liveordie中的更新值。但是,当我打印出两个嵌套数组时,它们完全相同。请看下面我的代码。我认为包括整个代码将有助于澄清我刚才试图解释的内容。

"""The game of life

Any live cell with fewer than two live neighbours dies, as if caused by under-population.
Any live cell with two or three live neighbours lives on to the next generation.
Any live cell with more than three live neighbours dies, as if by over-population.
Any dead cell with exactly three live neighbours becomes a live cell, as if by reproduction.
"""


cellCoords=[] #this list of original cells 

class setup:
    def __init__(self,size,cells):
        """input is all integers. size is height and width of gameboard, cells refers to the number of living cells the user designates to start the board game"""
        self.size=size
        self.cells=cells
    def createBoard(self):
        """input integers width and height. Output is a board of 0s"""
        a=[]
        for i in range(self.size):
            a.append([])
            for j in range(self.size):
                a[i].append(0)
        return a

    def updateBoard(self):
        print('update now')
        """initial board update to begin the game"""
        original=self.createBoard()
        print('create board')
        for i in range(self.cells): #number of cells = number of cell coordinates
            row=int(input('what row is the living cell in?'))
            column=int(input('what column is the living cell in?'))
            original[row][column]=1
        return original #the first gameBoard

class printBoard:
    """printing the board"""
    def __init__(self,upboard,size):
        self.upboard=upboard
        self.size=size
    def pBoard(self):
        graf=''
        for i in range(self.size):
            graf+='\n'
            for j in range(self.size):
               graf+=str(self.upboard[i][j])
        print(graf)


class checkState:
    """this class will update the neighboring cells according to Jon Conway's rules of the game of life"""
    def __init__(self,board, upboard, size):#updateBoard will provide the initial living cells, but this will selfupdate
        self.board=board
        self.upboard=upboard
        self.size=size

    def rules(self): #cycle through array coordinates of the board and apply rules to each coordinate
        a=self.board
        b=self.upboard
        print(a)
        neighborcells=[[i,j] for j in range(len(a[i])) for i in range(len(a))]
        #print('neighborcells are '+str(neighborcells)) #a list of board coordinates

        ###FIRST LOOP###
        for x in range(len(neighborcells)):
            totalalive=0
            ninecells=[[neighborcells[x][0]+w,neighborcells[x][1]+h] for h in range(-1,2) for w in range(-1,2)]
            print('\nthe ninecells are '+ str(ninecells))
            z=neighborcells[x] #original coord

            for cell in ninecells: #counts total number of live cells for each of the neighboring cooridnates
                    xn=cell[0] #row
                    yn=cell[1] #column
                    if cell==z: #convert original coordinate points into nested array checker
                        checkifOne=self.board[xn][yn]

                    if (xn in list(range(size)) and yn in list(range(size))):

                        if self.board[xn][yn]==1: 
                            totalalive+=1
            print('the origboard is ' +str(self.board))
            print('total number of live neighboring cells for original coordinate ' + str(z)+ ' is ' +str(totalalive))

            #DONT RUN UNTIL YOU ADD UP THE TOTAL NUMBER OF CELLS

            if (checkifOne!=1):
                print('the original coordinate is '+str(z))
                #print('running live or die function')
                print('the cell '+ str(z) + ' has ' + str(totalalive) + ' live neighbors')
                self.liveordie(totalalive,neighborcells[x]) #pass the array of alive cells

            else: #SUBTRACT ONE LIVE CELL IF 
                totalalive -= 1
                print('Subtracting 1 live cell from the original: the cell '+ str(z) + ' has ' + str(totalalive) + ' live neighbors')
                self.liveordie(totalalive,neighborcells[x])

                    #else:
                        #print('the coordinate ' + str(xn) + 'by ' + str(yn) + 'is not within the board boundaries')
        print('upboard is ' +str(self.upboard))
        print('origboard is '+str(self.board))
        return self.upboard

    def liveordie (self,alive,originalcoord):
        xo=originalcoord[0]
        yo=originalcoord[1]
        if alive > 3:
            self.upboard[xo][yo]=0 #UPDATES original board
            print('Im too crowded im dead :(')            
        elif alive < 2:
            self.upboard[xo][yo]=0
            print('Im too lonely im dead :(')
        elif (self.upboard[xo][yo]==1) and (alive ==2 or alive ==3):
            self.upboard[xo][yo]=1
            print('theres just enough im still alive :)')
        elif (self.upboard[xo][yo]==0) and (alive==3):
            self.upboard[xo][yo]=1
            print('perfect number of neighbors im alive :)')





### GETTING STARTED###

size=int(input('What is the size of your gameboard?'))
cells=int(input('How many living cells do you want to start with?'))

newgame=setup(size,cells)

print('OK here is your initial setup')

print('first  gameboard')
firstgame=newgame.updateBoard()#first  gameboard
#Print Board

print('this is out what the board looks like in the beginning')
startGame=printBoard(firstgame, size) #this is out what the board looks like in the beginning


print('print the board () refers to the instance we just created ')
startGame.pBoard() #print the board () refers to the instance we just created 


repeat=int(input('enter the number of times you would like this to repeat'))

x=checkState(firstgame, firstgameoriginal, size) 
print('checkstate is the original value of x: ' + str(x))

###MAIN LOOP###

for i in range(repeat):
    print('i is ' + str(i))
    newboard=x.rules() #returns a NEW board
    x=checkState(newboard,newboard, size) #saves the state of NEW board for the next iteration

    ###PRINTS THE BOARD###
    printupdate=printBoard(newboard, size)
    printupdate.pBoard()

    if i==repeat:
        break;

0 个答案:

没有答案