1 #include <stdio.h> 2 #include <stdlib.h> 3 #include <time.h> 4 #include <conio.h> 5 6 7 //行寬度 8 #define ROW_LEN 38 9 10 11 //比特位域結構 12 typedef struct bits bits; 13 struct bits{ 14 15 unsigned int c0 : 1; 16 unsigned int c1 : 1; 17 unsigned int c2 : 1; 18 unsigned int c3 : 1; 19 unsigned int c4 : 1; 20 unsigned int c5 : 1; 21 unsigned int c6 : 1; 22 unsigned int c7 : 1; 23 }; 24 25 26 //行類型 27 typedef bits row[( ROW_LEN + 7 ) / 8]; 28 29 30 //讀取行中元胞 31 unsigned int get_cell( row rw, int x ){ 32 33 unsigned int re = 0; 34 35 36 if( x < -1 || x > ROW_LEN ){ 37 38 puts( "get_cell: 坐標錯誤." ); 39 40 return re; 41 } 42 43 if( -1 == x ){ 44 45 x = ROW_LEN - 1; 46 } 47 48 if( ROW_LEN == x ){ 49 50 x = 0; 51 } 52 53 54 switch( x % 8 ){ 55 56 case 0:{ 57 58 re = rw[x / 8].c0; 59 }break; 60 61 case 1:{ 62 63 re = rw[x / 8].c1; 64 }break; 65 66 case 2:{ 67 68 re = rw[x / 8].c2; 69 }break; 70 71 case 3:{ 72 73 re = rw[x / 8].c3; 74 }break; 75 76 case 4:{ 77 78 re = rw[x / 8].c4; 79 }break; 80 81 case 5:{ 82 83 re = rw[x / 8].c5; 84 }break; 85 86 case 6:{ 87 88 re = rw[x / 8].c6; 89 }break; 90 91 case 7:{ 92 93 re = rw[x / 8].c7; 94 }break; 95 } 96 97 98 return re; 99 } 100 101 102 //修改行中元胞 103 void set_cell( row rw, int x, unsigned int v ){ 104 105 106 if( x < -1 || x > ROW_LEN ){ 107 108 puts( "set_cell: 坐標錯誤." ); 109 110 return; 111 } 112 113 if( -1 == x ){ 114 115 x = ROW_LEN - 1; 116 } 117 118 if( ROW_LEN == x ){ 119 120 x = 0; 121 } 122 123 124 v = v % 2; 125 126 127 switch( x % 8 ){ 128 129 case 0:{ 130 131 rw[x / 8].c0 = v; 132 }break; 133 134 case 1:{ 135 136 rw[x / 8].c1 = v; 137 }break; 138 139 case 2:{ 140 141 rw[x / 8].c2 = v; 142 }break; 143 144 case 3:{ 145 146 rw[x / 8].c3 = v; 147 }break; 148 149 case 4:{ 150 151 rw[x / 8].c4 = v; 152 }break; 153 154 case 5:{ 155 156 rw[x / 8].c5 = v; 157 }break; 158 159 case 6:{ 160 161 rw[x / 8].c6 = v; 162 }break; 163 164 case 7:{ 165 166 rw[x / 8].c7 = v; 167 }break; 168 } 169 } 170 171 172 //演化行中元胞 173 unsigned int evo_cell( row rw, int x, unsigned char tab ){ 174 175 unsigned char num = 0; 176 177 178 if( x < 0 || x > ROW_LEN - 1 ){ 179 180 puts( "evo_cell: 坐標錯誤." ); 181 182 return 0; 183 } 184 185 186 num |= ( unsigned char )get_cell( rw, x - 1 ); 187 num <<= 1; 188 num |= ( unsigned char )get_cell( rw, x ); 189 num <<= 1; 190 num |= ( unsigned char )get_cell( rw, x + 1 ); 191 192 193 return ( tab >> num ) & 0x01; 194 } 195 196 197 //演化行到另外一個行 198 void evo_row( row rw1, row rw2, unsigned char tab ){ 199 200 int x; 201 202 203 for( x = 0; x < ROW_LEN; x++ ){ 204 205 set_cell( rw2, x, evo_cell( rw1, x, tab ) ); 206 } 207 } 208 209 210 //隨機初始化行 211 void rand_row( row rw ){ 212 213 int x; 214 215 216 for( x = 0; x < ROW_LEN; x++ ){ 217 218 set_cell( rw, x, rand() % 2 ); 219 } 220 } 221 222 223 //顯示行 224 void display_row( row rw ){ 225 226 int x; 227 228 229 for( x = 0; x < ROW_LEN; x++ ){ 230 231 printf( "%s", get_cell( rw, x ) ? "▉" : " " ); 232 } 233 234 printf( "\n" ); 235 } 236 237 238 239 //規則選擇,這裡選擇為規則30,隨機產生等腰三角形 240 //這種自動機是一種隨機數算法的根基 241 #define TYPE_ID 30 242 243 244 //主函數 245 int main( int argc, char * argv[] ){ 246 247 row rw1, rw2; 248 249 250 srand( ( unsigned int )time( NULL ) ); 251 252 rand_row( rw1 ); 253 254 255 while( 1 ){ 256 257 display_row( rw1 ); 258 259 _getch(); 260 261 evo_row( rw1, rw2, TYPE_ID ); 262 263 display_row( rw2 ); 264 265 _getch(); 266 267 evo_row( rw2, rw1, TYPE_ID ); 268 } 269 270 return 0; 271 }
運行效果: