Claude 3.7 Sonnetのコーディング能力を試す 2


以前の投稿でClaude 3.7 Sonnetのコーディング能力を試した.今回はLangton’s Antを試してみた.

プロンプトと結果は下記.(最後のアニメーションのコードがコメントアウトされていたので,その部分のみ手動で修正した)

動作はしているが描画が上手くいっていない.

Langton's AntのシミュレーションをPythonで実装.

エラーが出ないように自分で点検して.
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation

class LangtonAnt:
    # 方向: 0=上, 1=右, 2=下, 3=左
    DIRECTIONS = [(-1, 0), (0, 1), (1, 0), (0, -1)]
    
    def __init__(self, grid_size=100):
        # 格子の初期化(0=白, 1=黒)
        self.grid = np.zeros((grid_size, grid_size), dtype=int)
        # アリの初期位置(格子の中央)
        self.position = (grid_size // 2, grid_size // 2)
        # アリの初期方向(上)
        self.direction = 0
        # 移動回数
        self.steps = 0
        # 格子のサイズ
        self.grid_size = grid_size
        
    def step(self):
        """アリを1ステップ移動させる"""
        x, y = self.position
        
        # 現在のセルの色に応じて方向を変える
        if self.grid[x, y] == 0:  # 白セルの場合
            self.direction = (self.direction + 1) % 4  # 右に90度回転
            self.grid[x, y] = 1  # セルを黒に変える
        else:  # 黒セルの場合
            self.direction = (self.direction - 1) % 4  # 左に90度回転
            self.grid[x, y] = 0  # セルを白に変える
        
        # アリの移動
        dx, dy = self.DIRECTIONS[self.direction]
        new_x, new_y = x + dx, y + dy
        
        # 格子の境界を循環させる
        new_x %= self.grid_size
        new_y %= self.grid_size
        
        self.position = (new_x, new_y)
        self.steps += 1
        
    def run(self, steps):
        """指定された回数のステップを実行する"""
        for _ in range(steps):
            self.step()
            
    def display(self):
        """現在の格子状態を表示する"""
        plt.figure(figsize=(8, 8))
        plt.imshow(self.grid, cmap='binary')
        plt.title(f'Langton\'s Ant after {self.steps} steps')
        ant_y, ant_x = self.position  # 表示用に座標を反転
        plt.plot(ant_x, ant_y, 'ro')  # アリの位置を赤い点で表示
        plt.grid(True)
        plt.show()
        
    def create_animation(self, steps, interval=50):
        """アニメーションを作成する"""
        fig, ax = plt.subplots(figsize=(8, 8))
        ax.set_title('Langton\'s Ant Animation')
        ax.set_xticks([])
        ax.set_yticks([])
        
        # 初期状態
        img = ax.imshow(self.grid, cmap='binary', animated=True)
        ant_marker, = ax.plot([], [], 'ro', markersize=5)
        
        # 初期化関数
        def init():
            return [img, ant_marker]
        
        # アニメーション更新関数
        def update(frame):
            self.step()
            img.set_array(self.grid)
            ant_y, ant_x = self.position  # 表示用に座標を反転
            ant_marker.set_data([ant_x], [ant_y])
            ax.set_title(f'Langton\'s Ant: Step {self.steps}')
            return [img, ant_marker]
        
        # アニメーションの作成
        ani = animation.FuncAnimation(fig, update, frames=steps,
                                     init_func=init, blit=True, interval=interval)
        plt.close()  # インラインでの表示を防ぐ
        return ani

# 使用例
if __name__ == "__main__":
    # Langton's Antのインスタンスを作成
    ant = LangtonAnt(grid_size=101)  # 奇数サイズの格子を使用
    
    # 11,000ステップ実行
    ant.run(11000)
    
    # 結果を表示
    ant.display()
    
    # アニメーションを作成する場合(新しいインスタンスを使用)
    # animation_ant = LangtonAnt(grid_size=101)
    # ani = animation_ant.create_animation(steps=500)
    # ani.save('langton_ant.gif', writer='pillow', fps=20)

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です