2017-01-21 16 views
0

私はちょうどPythonの学習を始めました。この宿題は、ConwayのGame of Lifeのmatplotlibを使って実行するプログラムを作成する必要があります。私の教授はコードの一部を行っており、TODOコメントの下にコードを記入する必要があります。だから私はそれを書きました、なぜそれがアニメーションを実行しなかったのか分かりませんでした。何を私は間違っていたのですか?あまりにも多くの人にありがとう!プログラム内のコメントは、指示のようなものなのでかなり長いです。Python matplotlibを使ったConwayのゲームGame

# life.py - Once complete, this program will play the game of 
#   life. 

import numpy as np 
import matplotlib.pyplot as plt 
import matplotlib.animation as animation 

# The update function is needed for the animation 
# to work properly. It has four parameters: 
# frameNum - this is handled by the animation, don't change this. 
# img - the plot that is passed and changed, don't change this. 
# world - the two-D array that represents the world for the 
#   game of life. This will be updated to the next gen. 
# N - the size of the world (which is square). 

def update(frameNum, img, world, N) : 

    newWorld = world.copy() 

    ## TODO - Write code here to update the cells in newWorld 
    ##  for the next generation of life. Remember to 
    ##  use the toroidal model. Rather than making 
    ##  special cases for row/column 0 and N-1, just 
    ##  use the % operator. 

    for i in range (N): 
     for j in range (N): 
      total= (world[(i-1)%N][(j-1)%N]+world[(i-1)%N][j]+world[(i-1)%N][(j+1)%N]+ 
        world[i][(j-1)%N]+world[i][(j+1)%N]+ world[(i+1)%N][(j-1)%N]+ 
        world[(i+1)%N][j]+ world[(i+1)%N][(j+1)%N])/255 

      if world[i][j]== 255: 

       if total>3 or total<2: 
        newWorld[i][j]== 0 
       elif total==3: 
        newWorld[i][j]== 255 

    img.set_data(newWorld) 
    world[:] = newWorld[:] 
    return img 

N = 50 
SPEED = 100 
PROB_LIFE = 40 


## TODO - Write code here to create the initial population 
## of the world. I recommend creating an N x N array that 
## is initially filled with random numbers from 0 to 99. 
## Then use the PROB_LIFE to change the entries to be 
## either alive (255) or dead (0). 
world= np.random.choice([0,255], N*N, p=[1-((PROB_LIFE)/100),(PROB_LIFE)/100]).reshape(N,N) 

fig, ax = plt.subplots() 
img = ax.imshow(world, interpolation='nearest') 
ani = animation.FuncAnimation(fig, update, fargs = (img, world, N), 
           frames = 10, interval = SPEED, 
           save_count = 50) 
plt.show() 

# This is the end of the program. You should not need 
# anything after this point. 

答えて

3

あなたは

newWorld[i][j] = 0 # need `=` instead of `== 

newWorld[i][j] = 255 # need `=` instead of `== 

に新しい値を割り当てるために、今あなたがアニメーションが表示されます=の代わり==を使用する必要があります。

しかし、あなたはまた、elifと勘違いしている - 正しく

 if world[i][j] == 255: 

      if total > 3 or total < 2: 
       newWorld[i][j] = 0 

     elif total == 3: 
       newWorld[i][j] = 255 

または==

 if world[i][j] == 255: 

      if total > 3 or total < 2: 
       newWorld[i][j] = 0 

     else: 

      if total == 3: 
       newWorld[i][j] = 255 
+0

良い答えが、オープニングの文が反転し、より読みやすく、= - 間違ったインデントを。あなたは '=='の代わりに '='を使わなければなりません。 (編集しようとしましたが、編集は6文字でなければなりません...) –

+0

@ScottCarpenterあなたは正しく修正されています。 – furas

関連する問題