@dtwice369

Are you sure you want to delete the question?

Leaving a resolved question undeleted may help others!

Python ライフゲーム

Q&A

Closed

解決したいこと

Pythonでライフゲームを作成しているが、第2世代からの表示が上手くいかない。

発生している問題・エラー

ValueError                                Traceback (most recent call last)
Cell In[1], line 60
     58     img = ax.imshow(board, cmap='gray', interpolation='nearest')
     59     imgs.append([img])
---> 60     board = update_board(board)
     62 plt.close()
     63 ani = animation.ArtistAnimation(fig, imgs, interval=200, repeat_delay=1000)

Cell In[1], line 36, in update_board(board)
     33 #print(neighbors)
     34 #print(board)
     35 for k in range(8):
---> 36     if board[neighbors[k]] == 1:
     37         live_neighbors += 1
     39 if board[i, j] == 1:

ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

該当するソースコード

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

def create_board(size):
    return np.random.choice([0, 1], [N, N], p = [1 - PRATE, PRATE])

def p_func(k, n):
    if k == -1:
        return n - 1
    elif k == n:
        return 0
    else:
        return k

def my_field(i, j, n):
    return [[p_func(i - 1, n), p_func(j - 1, n)],
            [p_func(i, n), p_func(j - 1, n)],
            [p_func(i + 1, n), p_func(j - 1, n)],
            [p_func(i - 1, n), p_func(j, n)],
            [p_func(i + 1, n), p_func(j, n)],
            [p_func(i - 1, n), p_func(j + 1, n)],
            [p_func(i, n), p_func(j + 1, n)],
            [p_func(i + 1, n), p_func(j + 1, n)]]

def update_board(board):
    new_board = board.copy()
    live_neighbors = 0
    for i in range(board.shape[0]):
        for j in range(board.shape[1]):
            neighbors = my_field(i, j, 50)
            #print(neighbors)
            #print(board)
            for k in range(8):
                if board[neighbors[k]] == 1:
                    live_neighbors += 1
            
            if board[i, j] == 1:
                if live_neighbors < 2 or live_neighbors > 3:
                    new_board[i, j] = 0
            else:
                if live_neighbors == 3:
                    new_board[i, j] = 1

    return new_board

# ボードのサイズと初期状態の設定
N = 50
PRATE = 0.3
board = create_board(N)

# アニメーションの初期化
fig, ax = plt.subplots()
imgs = []

for _ in range(100):
    img = ax.imshow(board, cmap='gray', interpolation='nearest')
    imgs.append([img])
    board = update_board(board)

plt.close()
ani = animation.ArtistAnimation(fig, imgs, interval=200, repeat_delay=1000)
rc('animation',html='jshtml')
ani

自分で試したこと

周囲の8セルの生存数の計算方法が分からない。
解決方法を教えていただきたいです。

0 likes

1Answer

こちらでいかがでしょうか。

def my_field(i, j, n):
    return [(p_func(i - 1, n), p_func(j - 1, n)),
            (p_func(i, n), p_func(j - 1, n)),
            (p_func(i + 1, n), p_func(j - 1, n)),
            (p_func(i - 1, n), p_func(j, n)),
            (p_func(i + 1, n), p_func(j, n)),
            (p_func(i - 1, n), p_func(j + 1, n)),
            (p_func(i, n), p_func(j + 1, n)),
            (p_func(i + 1, n), p_func(j + 1, n))]
0Like

Comments

  1. @dtwice369

    Questioner

    試しましたがエラーはなくなりましたが第二世代目から表示されません。image.png
    image.png

  2. live_neighbors = 0の位置が間違っています。
    あとは自力でデバッグしてください。

  3. @dtwice369

    Questioner

    無事完成しました。ありがとうございます。

Your answer might help someone💌