i attempting list based implementation of conway's game of life without using add-ons or lists of lists in python 3.5.2. issue running number of "neighbors" each point has incorrect. have left print statement in code below. in example, occupied cell denoted "o" while unoccupied cell denoted "."
#determine how many neighbors surrounding provided point def getneighbors(board, index, columns): neighbors = 0 try: if(board[index + 1] == "o"): neighbors += 1 except: pass try: if(board[index - 1] == "o"): neighbors += 1 except: pass try: if(board[index + columns] == "o"): neighbors += 1 except: pass try: if(board[index - columns] == "o"): neighbors += 1 except: pass try: if(board[index - columns + 1] == "o"): neighbors += 1 except: pass try: if(board[index - columns - 1] == "o"): neighbors += 1 except: pass try: if(board[index + columns + 1] == "o"): neighbors += 1 except: pass try: if(board[index + columns - 1] == "o"): neighbors += 1 except: pass return neighbors #creates game board in list of lists def mkboard(rows,columns): board = ["."] * rows * columns return board #used edit point on game board def editpoint(x,y,board,columns): = 0 = x + ((y - 1) * columns) - 1 if( board[i] == "o"): board[i] = "." elif( board[i] == "."): board[i] = "o" return board #simulates next step in game def nextturn(board, columns): prevboard = board = 0 index in prevboard: neighbors = 0 if( index == 'o'): neighbors = getneighbors(prevboard, i, columns) print(neighbors) if(neighbors == 0 or neighbors == 1): board[i] = "." elif(neighbors >= 4): board[i] = "." elif(index == "."): neighbors = getneighbors(prevboard, i, columns) print(neighbors) if(neighbors == 3): board[i] = "o" += 1 return board #prints board screen show user happening def printboard(board, columns): counter = 0 cell in board: print(cell,end=" ") counter += 1 if( counter == columns): print('\n') counter = 0 print("======conway's game of life======") #take user input number of rows , columns board , converts them integers rows = input('enter number of rows:') rows = int(rows) columns = input('enter number of columns:') columns = int(columns) #create board , show user board = mkboard(rows,columns) printboard(board,columns) choice = 0 #if user wants add points board can, otherwise can begin game while( 1 != 3): choice = input('make choice:\n1) change point\n2) continue game\n3) quit\n') if(choice =='1'): x = input('enter x coordinate of point negate: ') x = int(x) y = input('enter y coordinate of point negate: ') y = int(y) board = editpoint(x,y,board, rows) printboard(board,columns) elif(choice =='2'): board = nextturn(board, columns) printboard(board,columns) elif(choice == '3'): break
i found 2 mistakes:
- at
board = editpoint(x,y,board, rows)
should passcolumns
instead ofrows
. in
nextturn
,prevboard = board
doesn't think does. assignment not create copy of list. both variables reference same list object. see example:>>> a= [0,1]
>>> b= a
>>> a[0]= 9
>>> b # output: [9, 1]
to create copy of list, use
prevboard= board[:]
.
Comments
Post a Comment