Comment detail

ライフゲーム (Nested Flatten)
LifeGame書いたことなかったから、間引きなしで。
グライダーが動いたので多分合ってる?
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#include <stdio.h>
#include <conio.h>

#define WIDTH  10
#define HIGHT 10

void next_gen(int a[HIGHT][WIDTH]){
    int life;
    int x,y;
    int x_l,x_r,y_u,y_d;
    for(y=0;y<HIGHT;y++){
        y_u=(y+ 9)%HIGHT;
        y_d=(y+11)%HIGHT;
        for(x=0;x<WIDTH;x++){
            x_l=(x+ 9)%WIDTH;
            x_r=(x+11)%WIDTH;
            
            life= (a[y_u][x_l]&1)+(a[y_u][x]&1)+(a[y_u][x_r]&1)
                 +(a[ y ][x_l]&1)              +(a[ y ][x_r]&1)
                 +(a[y_d][x_l]&1)+(a[y_d][x]&1)+(a[y_d][x_r]&1);

            if((life|(a[y][x]&1))==3) a[y][x]|=2;
        }
    }
    for(y=0;y<HIGHT;y++){
        for(x=0;x<WIDTH;x++){
            a[x][y]>>=1;
        }
    }
}

void put_gen(int a[HIGHT][WIDTH]){
    int x,y;
    for(y=0;y<HIGHT;y++){
        for(x=0;x<WIDTH;x++){
            putchar('[');
            putchar(a[y][x]==1?'*':' ');
            putchar(']');
        }
            putchar('\n');
    }
}

void lifegame(int a[HIGHT][WIDTH]){
    int gen=0;
    do{
        printf("T=%d\n",gen);
        put_gen(a);
        next_gen(a);
        gen++;
    }while(getch()==0x20);
}

int main(){
    int a[HIGHT][WIDTH]={
/* 出題
        {0,1,0,0,0,0,1,1,1,0},
        {0,0,0,0,1,0,0,1,1,0},
        {0,0,0,1,0,0,1,0,1,0},
        {1,0,1,1,0,0,1,0,0,0},
        {0,1,0,0,0,0,0,0,1,0},
        {1,0,0,0,1,0,1,1,0,1},
        {0,1,0,0,0,0,1,0,0,0},
        {0,0,0,0,0,0,0,0,0,1},
        {1,0,0,0,0,0,1,0,0,1},
        {0,0,0,0,1,1,0,0,1,0}
/*/
//グライダー
        {0,0,0,0,0,0,0,0,0,0},
        {0,0,0,0,0,0,0,0,0,0},
        {0,0,0,0,0,0,0,0,0,0},
        {0,0,0,0,0,0,0,0,0,0},
        {0,0,0,1,0,0,0,0,0,0},
        {0,0,1,0,0,0,0,0,0,0},
        {0,0,1,1,1,0,0,0,0,0},
        {0,0,0,0,0,0,0,0,0,0},
        {0,0,0,0,0,0,0,0,0,0},
        {0,0,0,0,0,0,0,0,0,0},
//*/
    };
    lifegame(a);
    
    return 0;
}
題意を間違えたみたいなんで追加修正
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
void init_life(int a[HEIGHT][WIDTH]){
    int x,y;
    int count=0;
    
    for(y=0;y<HEIGHT;y++){
        for(x=0;x<WIDTH;x++){
            a[y][x]=0;
        }
    }

    srand(time(NULL));
    do{
        x=rand()/(RAND_MAX/WIDTH);
        y=rand()/(RAND_MAX/HEIGHT);
        if(calc_life(a,x,y)<=(2+(rand()/(RAND_MAX/2)))){
            a[y][x]=1;
            count++;
        }
    }while(count<((HEIGHT*WIDTH)*(2+(rand()/(RAND_MAX/2)))/10));
}

int main(){
    int a[HEIGHT][WIDTH];
    init_life(a);
    lifegame(a);
    
    return 0;
}

Index

Feed

Other

Link

Pathtraq

loading...