이것은 처음으로이 사이트에 올린 글이므로 저와 잘 어울리지 않습니다. 나는 다른 사람들의 게시물을 통해 해결책을 찾는 데 성공하지 못했다는 문제에 봉착했다. 우리는 Conway의 Game of Life에 대한 표준 규칙을 따르고 있습니다.Conway의 인생 게임에 문제가 있습니다.
지금까지 2 차원 어레이를 생성 할 수 있었고 배열에 '-'와 'O'가 무작위로있는 문자를 채울 수있었습니다 (숫자 0이 아닌 문자 O). 나는 모든 이웃들을 정확하게 조사하고 점검 할 것이라고 믿는다. * try/catch를 사용한 이유는 범위를 벗어난 오류 메시지가 표시되는 것을 피할 수있는 유일한 방법이기 때문입니다.
내 주요 문제는 값을 확인하거나 변경할 때 배열을 올바르게 통과하는지 잘 모르겠다는 것입니다. 또한 전체 배열에 라이브/다이 규칙을 적용한 후 "업데이트 된"2 차원 배열을 인쇄하는 방법을 알지 못합니다.
현재 출력은 무작위 배열을 한 번 인쇄 한 다음 똑같은 배열을 다시 인쇄 한 다음 계속할 것인지 묻습니다 (while 루프를 다시 반복하십시오). 내가 'y'를 선택하면, 나는 파이썬으로 약 3 주 오전으로 내가 잃어버린 이상 해요
TypeError: list indices must be integers, not list
가, 어떤 아이디어가 도움이 될 것입니다 오류 메시지가 얻을. 충돌이 경우 라인에서 발생 : 문제가 여기에
import random
numrows = 25
numcols = 25
def rnd():
rn = random.randint(0,1)
return rn
def initial():
grid = []
count = 0
for x in range(numrows):
grid.append([])
for y in range(numcols):
rand=random.randrange(1,3)
if(rand == 1):
grid[x].append('O')
else:
grid[x].append('-')
for x in grid:
print(*x, sep=' ',end="\n") #prints initial grid with random values
print("")# space for clarity sake
print("")# " "
answer = 'y'
count = 0
while(answer == 'y'):
#This is where I am checking each neighbor. Adding 1 to count for each
#live neighbor.
for r in range(0,numrows):
for c in range(0,numcols):
try:
if(grid[r-1][c-1] == 'O'):
count += 1
except:
pass
try:
if(grid[r-1][c] == 'O'):
count += 1
except:
pass
try:
if(grid[r-1][c+1] == 'O'):
count += 1
except:
pass
try:
if(grid[r][c-1] == 'O'):
count += 1
except:
pass
try:
if(grid[r][c+1] == 'O'):
count += 1
except:
pass
try:
if(grid[r+1][c-1] == 'O'):
count += 1
except:
pass
try:
if(grid[r+1][c] == 'O'):
count += 1
except:
pass
try:
if(grid[r+1][c+1] == 'O'):
count += 1
except:
pass
#this is where I am applying the rules of living or dying
#based on how many neighbors there are.
if(grid[r][c] == '-'):
if(count == 3):
grid[r][c].append('O')
if(grid[r][c] == 'O'):
if(count < 2):
grid[r][c].append('-')
if(grid[r][c] == 'O'):
if(count == 2 or count == 3):
grid[r][c].append('O')
if(grid[r][c] == 'O'):
if(count > 3):
grid[r][c].append('-')
for r in grid: #This is a problem area as i dont
print(*r, sep=' ',end="\n")#know how to print the grid
answer = input("Continue? y or n(lower case only): ")
if(answer != 'y'):
print(" Hope you had a great life! Goodbye!")
전체 오류 메시지 (Traceback)를 표시합니다. 문제를 만드는 코드 줄과 같은 유용한 정보가 있습니다. – furas
'r-1'을 사용하기 전에 'r-1> -1'을 확인해야하므로'try/except'가 필요하지 않습니다. 'c-1> -1'과'r + 1
furas
'grid [r] [c]'는 char가 아니고리스트이기 때문에'grid [r] [c] .append ('O')'- 당신은'grid [r] [c] = 'O'''를 필요로합니다 – furas