ライフゲーム #15
今までのコードを少し変更、整理してみる。
ノードのname属性を"nx_y"形式から"x,y"形式に変更する。
また、vis_alive.gにneatoでオプション指定していたものを組み入れる。
次世代を生成するnext_gen.gは触る必要は多分ないはず。
まず、メッシュ生成プログラムgen_mesh.gを変更する。
gen_mesh.g
BEGIN { if (ARGC != 1) { printf(2, 'usage: gvpr -f gen_mesh.g -a width,height\n'); exit(0); } int w = xOf(ARGV[0]), h = yOf(ARGV[0]); if (w < 1 || h < 1) { printf(2, 'error: width and height must be positive integer: %d,%d\n', w, h); exit(0); } node_t gen_node(graph_t g, int x, int y) { return node(g, sprintf('%d,%d', x, y)); } void gen_edge(graph_t g, node_t n, int x, int y) { edge_sg(g, n, gen_node(g, x, y), ''); } graph_t g = graph('', 'U'); int x, y; for (y = 0; y < h; y++) { for (x = 0; x < w; x++) { node_t n = gen_node(g, x, y); if (x < w - 1) { if (y > 0) gen_edge(g, n, x + 1, y - 1); gen_edge(g, n, x + 1, y); if (y < h - 1) gen_edge(g, n, x + 1, y + 1); } if (y > 0) gen_edge(g, n, x, y - 1); } } write(g); }
引数の形式を変更し、二つの引数を与えるために引用符で囲む必要を無くした。
そして大きな変更はノードのname属性を"x,y"形式にしたことである。
エッジ生成のロジックは全く変えていない。
gvpr -f gen_mesh.g -a 3,4 | gvpr -c "N{pos=name;}" | neato -T png -o 3x4.png