日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > c/c++ >内容正文

c/c++

c语言万花筒,C/C++——元胞自动机万花筒

發(fā)布時間:2023/12/16 c/c++ 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 c语言万花筒,C/C++——元胞自动机万花筒 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

程序功能

實(shí)現(xiàn)一個簡單的元胞自動機(jī),可以自定義初始狀態(tài)、運(yùn)行規(guī)則。以此基礎(chǔ)實(shí)現(xiàn)一個能自動繪制大量萬花筒圖像的程序。

注:為了防止二維數(shù)組輸出時畫面閃爍,采取雙屏幕緩沖區(qū)輪流顯示。將緩沖區(qū)寫好后一次性輸出可以防止數(shù)組循環(huán)輸出方式會導(dǎo)致的畫面閃爍。

效果

生命游戲中的“滑翔機(jī)(gliders)”:

萬花筒:

代碼

#include

#include

#include

#include

#include

#include

#include

#define BLACK 0

#define WHITE 15

#define GREEN 10

#define YELLOW 14

#define RED 12

#define BLUE 9

#define CYAN 11

#define PURPLE 13

#define INCI(x) (((x)+1)%(COL))

#define DECI(x) (((x)+COL-1)%(COL))

#define INCJ(x) (((x)+1)%(RAW))

#define DECJ(x) (((x)+RAW-1)%(RAW))

#define MODE "mode 82,42"

#define COL 41

#define RAW 41

using namespace std;

int ct=0;

HANDLE hOutput, hOutBuf;//控制臺屏幕緩沖區(qū)句柄

COORD coord={0,0};

WORD att;

DWORD bytes=0;

char data[COL][RAW];

int clr[8]={BLACK,WHITE,GREEN,YELLOW,RED,BLUE,CYAN,PURPLE};

int a[COL][RAW]={0},b[COL][RAW]={0};

unsigned int counts=0;

char str[11];

void f0()

{

int i,j,k;

memset(b,0,sizeof(b));

for(i=0;i

for(j=0;j

{

k=a[i][j];

b[(i+rand()%3-1)%COL][(j+rand()%3-1)%RAW]+=k;

b[(i+rand()%3-1)%COL][(j+rand()%3-1)%RAW]+=k;

}

for(i=0;i

for(j=0;j

{

a[i][j]=b[i][j]%8;

}

}

void f1()

{

int i,j,k;

memset(b,0,sizeof(b));

for(i=0;i

for(j=0;j

{

k=a[i][j];

//b[i][j]+=k;

b[DECI(i)][j]+=k;

b[INCI(i)][j]+=k;

b[i][DECJ(j)]+=k;

b[i][INCJ(j)]+=k;

}

for(i=0;i

for(j=0;j

{

a[i][j]=b[i][j]%8;

}

}

void f2p()

{

a[1][5]=1;

a[2][5]=1;

a[1][6]=1;

a[2][6]=1;

a[11][5]=1;

a[11][6]=1;

a[11][7]=1;

a[12][4]=1;

a[12][8]=1;

a[13][3]=1;

a[13][9]=1;

a[14][3]=1;

a[14][9]=1;

a[15][6]=1;

a[16][4]=1;

a[16][8]=1;

a[17][5]=1;

a[17][6]=1;

a[17][7]=1;

a[18][6]=1;

a[21][3]=1;

a[21][4]=1;

a[21][5]=1;

a[22][3]=1;

a[22][4]=1;

a[22][5]=1;

a[23][2]=1;

a[23][6]=1;

a[25][1]=1;

a[25][2]=1;

a[25][6]=1;

a[25][7]=1;

a[35][3]=1;

a[35][4]=1;

a[36][3]=1;

a[36][4]=1;

}

void f2()

{

int i,j,k;

memset(b,0,sizeof(b));

for(i=0;i

for(j=0;j

{

k=0;

if(a[DECI(i)][j])k++;

if(a[INCI(i)][j])k++;

if(a[i][DECJ(j)])k++;

if(a[i][INCJ(j)])k++;

if(a[DECI(i)][DECJ(j)])k++;

if(a[DECI(i)][INCJ(j)])k++;

if(a[INCI(i)][DECJ(j)])k++;

if(a[INCI(i)][INCJ(j)])k++;

if(k==3)b[i][j]=1;

else if(k==2)b[i][j]=a[i][j];

else b[i][j]=0;

}

for(i=0;i

for(j=0;j

{

a[i][j]=b[i][j];

}

}

void show()

{

int i,j;

for(i=0;i

{

for(j=0;j

{

coord.X=2*i;

coord.Y=j;

att=clr[a[i][j]];

if(a[i][j])

{

WriteConsoleOutputCharacterA(hOutBuf,"■",2,coord,&bytes);

}

else WriteConsoleOutputCharacterA(hOutBuf," ",2,coord,&bytes);

WriteConsoleOutputAttribute(hOutBuf,&att,1,coord,&bytes);

coord.X++;

WriteConsoleOutputAttribute(hOutBuf,&att,1,coord,&bytes);

}

}

itoa(counts,str,10);

coord.X=0;coord.Y=41;

WriteConsoleOutputCharacterA(hOutBuf,str,sizeof(str),coord,&bytes);

att=WHITE;

WriteConsoleOutputAttribute(hOutBuf,&att,1,coord,&bytes);

SetConsoleActiveScreenBuffer(hOutBuf);

f1();

//Sleep(50);

getch();counts++;

for(i=0;i

{

for(j=0;j

{

coord.X=2*i;

coord.Y=j;

att=clr[a[i][j]];

if(a[i][j])

{

WriteConsoleOutputCharacterA(hOutput,"■",2,coord,&bytes);

}

else WriteConsoleOutputCharacterA(hOutput," ",2,coord,&bytes);

WriteConsoleOutputAttribute(hOutput,&att,1,coord,&bytes);

coord.X++;

WriteConsoleOutputAttribute(hOutput,&att,1,coord,&bytes);

}

}

itoa(counts,str,10);

coord.X=0;coord.Y=41;

WriteConsoleOutputCharacterA(hOutput,str,sizeof(str),coord,&bytes);

att=WHITE;

WriteConsoleOutputAttribute(hOutput,&att,1,coord,&bytes);

SetConsoleActiveScreenBuffer(hOutput);

f1();

//Sleep(50);

getch();counts++;

}

int main()

{

srand(time(NULL));

system(MODE);

hOutBuf = CreateConsoleScreenBuffer(

GENERIC_WRITE,

FILE_SHARE_WRITE,

NULL,

CONSOLE_TEXTMODE_BUFFER,

NULL

);

hOutput = CreateConsoleScreenBuffer(

GENERIC_WRITE,

FILE_SHARE_WRITE,

NULL,

CONSOLE_TEXTMODE_BUFFER,

NULL

);

CONSOLE_CURSOR_INFO cci;

cci.bVisible = 0;

cci.dwSize = 1;

SetConsoleCursorInfo(hOutput, &cci);

SetConsoleCursorInfo(hOutBuf, &cci);

a[COL/2][RAW/2]=1;

//a[rand()%COL][rand()%RAW]=1;

//f2p();

while(1)show();

}

圖片

一些比較好看的:

標(biāo)簽:int,C++,COL,RAW,元胞,include,萬花筒,define

來源: https://blog.csdn.net/Eyizoha/article/details/89401253

總結(jié)

以上是生活随笔為你收集整理的c语言万花筒,C/C++——元胞自动机万花筒的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。