Python 3, 2D List - Adjust Values of Neighboring Cells (Homework)


Python 3, 2D List - Adjust Values of Neighboring Cells (Homework)
NOTE: I can not import anything like pygame or numpy.
Working on a version of Conway's Game of Life, and I was able to get a "U" shape moving around. At this point I've got no clue on how to adjust values of the neighboring cells, or if it's even possible with my code.
In the function "setInitialPatternList" I've added if statements to print out "PASS" to see if I'm on the right track. From what it's printing out, I can tell it's not looking at all of the "X"s I have individually. I'm unsure of where to go at this point, or if my "if" functions in "setInitialPatternList" need to be moved elsewhere.
I need help trying to figure out how I can adjust the cells neighboring the "X"s on the board.
from random import randint
from os import system
MAX_ROW = 30
MAX_COL = 60
currentGen =
tempGen =
def displayMenu():
print("[P]lay – Press 'P' to play.")
print("[Q]uit – Press 'Q' to exit.n")
def setZeroList(listName):
for row in range(MAX_ROW):
for col in range(MAX_COL):
listName[row][col] = "."
def setInitialPatternList(listToFormat):
rowChoice = randint(0,MAX_ROW-7)
columnChoice = randint(0,MAX_COL-7)
for foundation in range(1,7):
listToFormat[rowChoice + foundation][columnChoice] = "X"
listToFormat[rowChoice + foundation][columnChoice + 6] = "X"
listToFormat[rowChoice + 6][columnChoice + foundation] = "X"
counter = 0
if listToFormat[rowChoice + 1][columnChoice] == "X":
print("Check1")
if listToFormat[rowChoice - 1][columnChoice] == "X":
print("Check2")
if listToFormat[rowChoice][columnChoice + 1] == "X":
print("Check3")
if listToFormat[rowChoice][columnChoice - 1] == "X":
print("Check4")
if listToFormat[rowChoice + 1][columnChoice + 1] == "X":
print("Check5")
if listToFormat[rowChoice + 1][columnChoice - 1] == "X":
print("Check6")
if listToFormat[rowChoice - 1][columnChoice - 1] == "X":
print("Check7")
if listToFormat[rowChoice - 1][columnChoice + 1] == "X":
print("Check8")
if counter == 0:
print("Counter Check")
def copyList(tempList, currentList):
for row in range(MAX_ROW):
for col in range(MAX_COL):
currentList[row][col] = tempList[row][col]
def displayList(currentList):
MAX_ROW = 30
MAX_COL = 60
for row in range(MAX_ROW):
for col in range(MAX_COL):
if (col == (MAX_COL - 1)):
print(currentList[row][col], end = "n")
else:
print(currentList[row][col], end = "")
def displaySubMenu():
print("[S]top - Press 'S' to stop.")
for row in range(MAX_ROW):
currentGen.append([0] * MAX_COL)
for row in range(MAX_ROW):
tempGen.append([0] * MAX_COL)
displayMenu()
setZeroList(tempGen)
setInitialPatternList(tempGen)
copyList(tempGen, currentGen)
displayList(currentGen)
while(True):
userResponses = ["q", "p"]
while(True):
playOrQuit = input(">>").lower()
if(playOrQuit in userResponses):
break
else:
continue
if(playOrQuit == "p"):
system("clear") #Change to cls
displayMenu()
setZeroList(tempGen)
setInitialPatternList(tempGen)
copyList(tempGen, currentGen)
displayList(currentGen)
else:
system("clear") #Change to cls
break
Sorry, didn't realize there wasn't a question in there. How would I go about seeing and/or changing the neighboring cells?
– howthegodschill
2 days ago
First, your
if
statements are close, but they have a serious problem: what happens when rowChoice
is 0
? rowChoice-1
is -1
, which you don't want to check (unless you wanted a torus rather than a plane, which wraps around at the edges). You need to guard against that and not check upward when you're on row 0 (and likewise at each edge, of course, not just the top.)– abarnert
2 days ago
if
rowChoice
0
rowChoice-1
-1
Next, to evolve the game one step, you need to write a function that does that. The function will need to loop over all of the cells, and it will need those
if
statements to check the neighbors, and it will need to actually count them (like x_neighbors += 1
), not just print something out, and then do something with that count (that's where the Life rules come into play).– abarnert
2 days ago
if
x_neighbors += 1
One last thing: make sure you don't modify the array while you're still walking it. For example, when you're evaluating position
(1, 1)
, you need to use the previous step's (0, 0)
and (0, 1)
and (0, 2)
and (1, 0)
, not the new values you already computed for the next step. So, your evolve
function is going to have to build up a new array as it goes, not change the current one.– abarnert
2 days ago
(1, 1)
(0, 0)
(0, 1)
(0, 2)
(1, 0)
evolve
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
So...what's your question?
– Rushabh Mehta
2 days ago