I'm trying to recreate Conway's game of Life, and realized I had to create two versions of the gameboard (represented a a nested array), which contains the values of living cells (1) and dead cells (0). I am using IDLE to complete this project. The github link is here
I am struggling with creating two distinct gameboards. My purpose in doing so is to keep the updated cells from influencing the other cells. Even though I update one board with values and not the other one, they end up having the same values. Below is a snippet of my code. I tried to create a global variable called board
separate from a local variable with the updated board. I'm a bit mystified as to how board somehow gets updated, even though I do not directly update it in my function - am I assigning global variables wrong ? Thanks for your patience, I am still a beginner.
Function Where I Create Local Variable upboard
Class checkState:
"""this class will update the neighboring cells according to Jon Conway's rules of the game of life"""
def __init__(self, upboard, size):#updateBoard will provide the initial living cells, but this will selfupdate
self.upboard=upboard
self.size=size
def rules(self): #cycle through array coordinates of the board and apply rules to each coordinate
b=self.upboard
global board
neighborcells=[[i,j] for j in range(len(b[i])) for i in range(len(b))]
#print('neighborcells are '+str(neighborcells)) #a list of board coordinates
The code that Starts the Program where I defined board, my global variable
firstgame=newgame.updateBoard()#first gameboard
print('this is firstgame '+str(firstgame))
board=firstgame #initially set board to firstgame value
#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, 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, size) #saves the state of NEW board for the next iteration
###PRINTS THE BOARD###
printupdate=printBoard(newboard, size)
printupdate.pBoard()
board=newboard #for global variable of original board
if i==repeat:
break;