C语言实现生命游戏
生命游戏 C++源码

#include "windows.h"#include "stdio.h"#include "stdlib.h"#include "math.h"#define IDB_PUSHBUTTON1 10#define IDB_PUSHBUTTON2 11#define IDB_PUSHBUTTON3 12int dot[200][100],dot2[200][100];HBITMAP hBm;BITMAP bm;HDC hdcmem;HDC hdcc;HINSTANCE hInst;char lpszClassName1[]="BUTTON";intnCheckBoxSt;HWNDhwndPush,hwndDefPush,hwndRadio,hwndAutoRadio,hwndCheckBox,hwndAutoCheckBox,hwnd GroupBox;LRESULT CALLBACK WndProc(HWND,UINT,WPARAM,LPARAM);int WINAPI WinMain(HINSTANCE hInstance,HINSTANCEhPrevInst,LPSTRlpszCmdLine,intnCmdShow){HWND hwnd;MSG Msg;WNDCLASS wndclass;char lpszClassName[]="ÉúÃü";char lpszTitle[]="ÉúÃüÓÎÏ·";wndclass.style =0;wndclass.lpfnWndProc =WndProc;wndclass.cbClsExtra =0;wndclass.cbWndExtra =0;wndclass.hInstance =hInstance;wndclass.hIcon =LoadIcon(NULL,IDI_APPLICATION);wndclass.hCursor =LoadCursor(NULL,IDC_ARROW);wndclass.hbrBackground =(HBRUSH)GetStockObject(WHITE_BRUSH);wndclass.lpszMenuName =NULL;wndclass.lpszClassName =lpszClassName;if(!RegisterClass(&wndclass)){MessageBeep(0);return FALSE;}hwnd=CreateWindow(lpszClassName,lpszTitle,WS_OVERLAPPEDWINDOW,CW_USEDEFAULT, CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,NULL,NULL,hInstance,NULL);hInst=hInstance;ShowWindow(hwnd,nCmdShow);UpdateWindow(hwnd);while(GetMessage(&Msg,NULL,0,0)){TranslateMessage(&Msg);DispatchMessage(&Msg);}return Msg.wParam ;}LRESULT CALLBACK WndProc(HWND hwnd,UINTmessage,WPARAMwParam,LPARAMlParam) {int n=0,i,j;int X=10,Y=10;HDC hdc;PAINTSTRUCT ps;PAINTSTRUCT ps1;HBRUSH hb;HPEN hp;switch(message){case WM_CREATE:SetTimer(hwnd,1,100,NULL);hwndPush=CreateWindow(lpszClassName1,"״̬һ",BS_PUSHBUTTON|WS_CHILD|WS_VISI BLE,X,Y,100,25,hwnd,(HMENU)IDB_PUSHBUTTON1,hInst,NULL);hwndDefPush=CreateWindow(lpszClassName1,"״̬¶þ",BS_PUSHBUTTON|WS_CHILD|WS_ VISIBLE,X+130,Y,100,25,hwnd,(HMENU)IDB_PUSHBUTTON2,hInst,NULL);hwndDefPush=CreateWindow(lpszClassName1,"Ëæ»ú",BS_PUSHBUTTON|WS_CHILD|WS_VISIBL E,X+260,Y,100,25,hwnd,(HMENU)IDB_PUSHBUTTON3,hInst,NULL);break;case WM_COMMAND:switch (LOWORD(wParam)){case IDB_PUSHBUTTON1:for(i=0;i<200;i++){for(j=0;j<110;j++){dot2[i][j]=0;dot[i][j]=0;}}dot[1][4]=1;dot[1][5]=1;dot[2][4]=1;dot[2][5]=1;dot[11][4]=1;dot[11][5]=1;dot[11][6]=1;dot[12][3]=1;dot[12][7]=1;dot[13][2]=1;dot[13][8]=1;dot[14][2]=1;dot[14][8]=1;dot[15][5]=1;dot[16][3]=1;dot[16][7]=1;dot[17][4]=1;dot[17][5]=1;dot[17][6]=1;dot[18][5]=1;dot[21][2]=1;dot[21][3]=1;dot[21][4]=1;dot[22][2]=1;dot[22][3]=1;dot[22][4]=1;dot[23][1]=1;dot[23][5]=1;dot[25][0]=1;dot[25][1]=1;dot[25][5]=1;dot[25][6]=1;dot[35][2]=1;dot[35][3]=1;dot[36][2]=1;dot[36][3]=1;dot2[1][4]=1;dot2[1][5]=1;dot2[2][4]=1;dot2[2][5]=1;dot2[11][4]=1;dot2[11][6]=1;dot2[12][3]=1;dot2[12][7]=1;dot2[13][2]=1;dot2[13][8]=1;dot2[14][2]=1;dot2[14][8]=1;dot2[15][5]=1;dot2[16][3]=1;dot2[16][7]=1;dot2[17][4]=1;dot2[17][5]=1;dot2[17][6]=1;dot2[18][5]=1;dot2[21][2]=1;dot2[21][3]=1;dot2[21][4]=1;dot2[22][2]=1;dot2[22][3]=1;dot2[22][4]=1;dot2[23][1]=1;dot2[23][5]=1;dot2[25][0]=1;dot2[25][1]=1;dot2[25][5]=1;dot2[25][6]=1;dot2[35][2]=1;dot2[35][3]=1;dot2[36][2]=1;dot2[36][3]=1;break;case IDB_PUSHBUTTON2: for(i=0;i<200;i++){for(j=0;j<110;j++){dot2[i][j]=0;dot[i][j]=0;}}dot[1][4]=1;dot[1][5]=1;dot[2][4]=1;dot[11][4]=1;dot[11][5]=1;dot[11][6]=1;dot[12][3]=1;dot[12][7]=1;dot[13][2]=1;dot[13][8]=1;dot[14][2]=1;dot[14][8]=1;dot[15][5]=1;dot[16][3]=1;dot[16][7]=1;dot[17][4]=1;dot[17][5]=1;dot[17][6]=1;dot[18][5]=1;dot[21][2]=1;dot[21][3]=1;dot[21][4]=1;dot[22][2]=1;dot[22][3]=1;dot[22][4]=1;dot[23][1]=1;dot[23][5]=1;dot[25][0]=1;dot[25][1]=1;dot[25][5]=1;dot[25][6]=1;dot[35][2]=1;dot[35][3]=1;dot[36][2]=1;dot[36][3]=1;dot2[1][4]=1; dot2[1][5]=1;dot2[2][4]=1;dot2[2][5]=1;dot2[11][4]=1;dot2[11][5]=1;dot2[11][6]=1;dot2[12][3]=1;dot2[12][7]=1;dot2[13][2]=1;dot2[13][8]=1;dot2[14][8]=1; dot2[15][5]=1; dot2[16][3]=1; dot2[16][7]=1; dot2[17][4]=1; dot2[17][5]=1; dot2[17][6]=1; dot2[18][5]=1; dot2[21][2]=1; dot2[21][3]=1; dot2[21][4]=1; dot2[22][2]=1; dot2[22][3]=1; dot2[22][4]=1; dot2[23][1]=1; dot2[23][5]=1; dot2[25][0]=1; dot2[25][1]=1; dot2[25][5]=1; dot2[25][6]=1; dot2[35][2]=1; dot2[35][3]=1; dot2[36][2]=1; dot2[36][3]=1; dot[65][55]=1; dot[65][56]=1; dot[66][55]=1; dot[66][56]=1; dot[65][54]=1; dot[66][60]=1; dot[65][60]=1; dot[66][54]=1; dot[67][54]=1; dot[67][55]=1; dot[68][54]=1; dot[68][55]=1; dot[68][56]=1; dot2[65][55]=1; dot2[65][56]=1; dot2[66][55]=1; dot2[66][56]=1; dot2[65][54]=1; dot2[66][60]=1;dot2[66][54]=1;dot2[67][54]=1;dot2[67][55]=1;dot2[68][54]=1;dot2[68][55]=1;dot2[68][56]=1;break;case IDB_PUSHBUTTON3:int x;for(i=0;i<200;i++){for(j=0;j<110;j++){dot2[i][j]=0;dot[i][j]=0;}}for(i=0;i<200;i++){for(j=0;j<110;j++){x=rand()%100;if(x>85){dot2[i][j]=1;dot[i][j]=1;}else{dot2[i][j]=0;dot[i][j]=0;}}}break;}break;case WM_PAINT:hdc=BeginPaint(hwnd,&ps);hdcc=GetDC(hwnd);hBm=CreateCompatibleBitmap(hdcc,1000,1000);hdcmem=CreateCompatibleDC(hdcc); ReleaseDC(hwnd,hdcc);SelectObject(hdcmem,hBm);SetMapMode(hdcmem,MM_TEXT);hb=CreateSolidBrush(BLACK_BRUSH);hp=CreatePen(PS_SOLID,1,RGB(0,0,0));Rectangle(hdcmem,0,0,1000,1000);SelectObject(hdcmem,hb);SelectObject(hdcmem,hp);for(i=0;i<1000;){MoveToEx(hdcmem,i,50,NULL);LineTo(hdcmem,i,550);i=i+5;}for(i=50;i<550;){MoveToEx(hdcmem,0,i,NULL);LineTo(hdcmem,1000,i);i=i+5;}for(i=0;i<200;i++){for(j=0;j<99;j++){if(dot[i][j]==1)Rectangle(hdcmem,i*5,50+j*5,i*5+5,50+j*5+5);if(dot[i-1][j-1]==1){n=n+1;}if(dot[i-1][j]==1){n=n+1;}if(dot[i-1][j+1]==1){n=n+1;}if(dot[i][j-1]==1){n=n+1;}if(dot[i][j+1]==1){n=n+1;}if(dot[i+1][j-1]==1){n=n+1;}if(dot[i+1][j]==1){n=n+1;}if(dot[i+1][j+1]==1){n=n+1;}if(n>=4)dot2[i][j]=0;if(n<=1)dot2[i][j]=0;if(n==3)dot2[i][j]=1;n=0;}}BitBlt(hdc,0,0,1000,1000,hdcmem,0,0,SRCCOPY); DeleteObject(hdcmem);DeleteObject(hBm);DeleteObject(hb);DeleteObject(hp);EndPaint(hwnd,&ps);break;case WM_TIMER:InvalidateRect(hwnd,NULL,0);for(i=0;i<200;i++)for(j=0;j<110;j++)dot[i][j]=dot2[i][j];break;case WM_DESTROY:PostQuitMessage(0);KillTimer(hwnd,1);break;default:return DefWindowProc(hwnd,message,wParam,lParam);}return 0;}。
生命游戏c语言源代码

生命游戏c语言源代码#include#include#define hs 10 //这能改行数#define ls 10 //这能改列数#define tiaojian1 2 //这里可改周围几个细胞时存活#define tiaojian2 3 //这里可改周围几个细胞时繁殖int world[hs][ls];void shuru(int[hs][ls]); //为数组赋初始值void shuchu(int[hs][ls]); //提供输出int huanjin(int,int); //计算周围细胞数void jisuanweilai(int[hs][ls]); //计算未来void chashou(int[hs][ls]); //运行后插手杀死或复活某些细胞int main(void){printf("生命游戏\n");printf("默认世界大小10*10,世界大小及规则代码定义中可改\n");printf("输入需要添加细胞的点的坐标,添加完成后输入一组负数坐标即可终止输入\n");int i,j;for(i=0;i<hs;i++)for(j=0;j<ls;j++)world[i][j]=0; //1为生0为死归零shuchu(world);shuru(world);shuchu(world);system("pause");while(1){char i;jisuanweilai(world);shuchu(world);printf("继续按回车,插手按a,终止按b\n"); i=getchar();if(i=='\n') ;else if(i=='a'){chashou(world);shuchu(world);system("pause");}else break;}system("pause");return 0;}void shuru(int world[hs][ls]){int i,j;while(1){scanf("%d%d",&i,&j);if(i<0||j<0) break; //任意为负终止输入else if(i>=hs||j>=ls){printf("输入数据超范围\n"); }world[i][j]=1; //1为生0为死}}void shuchu(int world[hs][ls]) {int i,j,k=0;for(i=0;i{printf(" %d",i);}printf("\n");printf(" ┌"); //第二行for(i=0;i<ls-1;i++){printf("─┬");}printf("─┐\n");for(i=0;i{printf("%d│",i);for(j=0;j<ls;j++){if(world[i][j]==1){printf("●│");k++;}else printf("○│");}printf("\n");if(i==hs-1) break;printf(" ├");for(j=0;j<ls-1;j++){printf("─┼");}printf("─┤\n");}printf(" └");for(i=0;i<ls-1;i++)printf("─┴");printf("─┘\n");printf(" 现有%d个存活细胞\n",k); }int huanjin(int h,int l){int sum=0,i,j;for(i=h-1;i<=h+1;i++){for(j=l-1;j<=l+1;j++){if(i<0||i>=hs)continue;if(j<0||j>=ls)continue;if(i==h&&j==l)continue;if(world[i][j]==1)sum++;}</ls-1;i++)</ls-1;j++)</ls;j++)</ls-1;i++)</ls;j++)</hs;i++)}return sum;}void jisuanweilai(int world[hs][ls]) {int i,j,k;for(i=0;i<hs;i++){for(j=0;j<ls;j++){k=huanjin(i,j);switch(k){case tiaojian1: break;case tiaojian2: world[i][j]=1;break; default : world[i][j]=0;break;}}}void chashou(int world[hs][ls]){int i,j;printf("加入要复活的细胞坐标,添加完成后输入一组负数坐标即可终止输入\n");while(1){scanf("%d%d",&i,&j);if(i<0||j<0) break; //任意为负终止输入else if(i>=hs||j>=ls){printf("输入数据超范围\n");}world[i][j]=1; //1为生0为死}printf("加入要杀死的细胞坐标,添加完成后输入一组负数坐标即可终止输入\n");while(1){scanf("%d%d",&i,&j);if(i<0||j<0) break; //任意为负终止输入else if(i>=hs||j>=ls){printf("输入数据超范围\n");}world[i][j]=0; //1为生0为死}</ls;j++) </hs;i++)。
生命游戏c++程序

生命游戏c++程序// 生命游戏.cpp : Defines the class behaviors for the application.//#include "stdafx.h"#include "生命游戏.h"#include "MainFrm.h"#include "生命游戏Doc.h"#include "生命游戏View.h"#ifdef _DEBUG#define new DEBUG_NEW#undef THIS_FILEstatic char THIS_FILE[] = __FILE__;#endif/////////////////////////////////////////////////////////////////// //////////// CMyAppBEGIN_MESSAGE_MAP(CMyApp, CWinApp)//{{AFX_MSG_MAP(CMyApp)ON_COMMAND(ID_APP_ABOUT, OnAppAbout)// NOTE - the ClassWizard will add and remove mapping macros here.// DO NOT EDIT what you see in these blocks of generated code!//}}AFX_MSG_MAP// Standard file based document commandsON_COMMAND(ID_FILE_NEW, CWinApp::OnFileNew)ON_COMMAND(ID_FILE_OPEN, CWinApp::OnFileOpen)END_MESSAGE_MAP()/////////////////////////////////////////////////////////////////// //////////// CMyApp constructionCMyApp::CMyApp(){// TODO: add construction code here,// Place all significant initialization in InitInstance}/////////////////////////////////////////////////////////////////// //////////// The one and only CMyApp objectCMyApp theApp;/////////////////////////////////////////////////////////////////// //////////// CMyApp initializationBOOL CMyApp::InitInstance(){AfxEnableControlContainer();// Standard initialization// If you are not using these features and wish to reduce the size// of your final executable, you should remove from the following// the specific initialization routines you do not need.#ifdef _AFXDLLEnable3dControls(); // Call this when using MFC in a shared DLL #elseEnable3dControlsStatic(); // Call this when linking to MFC statically#endif// Change the registry key under which our settings are stored.// TODO: You should modify this string to be something appropriate// such as the name of your company or organization.SetRegistryKey(_T("Local AppWizard-Generated Applications"));LoadStdProfileSettings(); // Load standard INI file options (including MRU)// Register the application's document templates. Document templates// serve as the connection between documents, frame windows and views.CSingleDocTemplate* pDocTemplate;pDocTemplate = new CSingleDocTemplate(IDR_MAINFRAME,RUNTIME_CLASS(CMyDoc),RUNTIME_CLASS(CMainFrame), // main SDI frame window RUNTIME_CLASS(CMyView));AddDocTemplate(pDocTemplate);// Parse command line for standard shell commands, DDE, file openCCommandLineInfo cmdInfo;ParseCommandLine(cmdInfo);// Dispatch commands specified on the command lineif (!ProcessShellCommand(cmdInfo))return FALSE;m_pMainWnd->MoveWindow(0,0,630,730,true);// The one and only window has been initialized, so show and update it.m_pMainWnd->ShowWindow(SW_SHOW);m_pMainWnd->UpdateWindow();return TRUE;}/////////////////////////////////////////////////////////////////// //////////// CAboutDlg dialog used for App Aboutclass CAboutDlg : public CDialog{public:CAboutDlg();// Dialog Data//{{AFX_DATA(CAboutDlg)enum { IDD = IDD_ABOUTBOX };//}}AFX_DATA// ClassWizard generated virtual function overrides//{{AFX_VIRTUAL(CAboutDlg)protected:virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support //}}AFX_VIRTUAL// Implementationprotected://{{AFX_MSG(CAboutDlg)// No message handlers//}}AFX_MSGDECLARE_MESSAGE_MAP()};CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD){//{{AFX_DATA_INIT(CAboutDlg)//}}AFX_DATA_INIT}void CAboutDlg::DoDataExchange(CDataExchange* pDX){CDialog::DoDataExchange(pDX);//{{AFX_DATA_MAP(CAboutDlg)//}}AFX_DATA_MAP}BEGIN_MESSAGE_MAP(CAboutDlg, CDialog) //{{AFX_MSG_MAP(CAboutDlg)// No message handlers//}}AFX_MSG_MAPEND_MESSAGE_MAP()// App command to run the dialogvoid CMyApp::OnAppAbout(){CAboutDlg aboutDlg;aboutDlg.DoModal();}/////////////////////////////////////////////////////////////////// ////////// // CMyApp message handlers。
生命游戏源代码

// game of life.cpp//function head#include <stdio.h>(standard input &output输入输出)#include <stdlib.h>(定义杂项函数及内存分配函数)#include <windows.h>(对话框程序或者窗口程序)#include <time.h>(从系统时钟获取时间方式)#include <ctype.h>(字符函数)//define the size of the two-dimensional array(定义大小的二维数组)#define SIZE 40void rule(int array[SIZE][SIZE]);//function prototype(函数原型)void SetColor(unsigned short ForeColor=5,unsigned short BackGroundColor=2) //change color(改变颜色){HANDLE hCon = GetStdHandle(STD_OUTPUT_HANDLE); SetConsoleTextAttribute(hCon,ForeColor|BackGroundColor); }int main(){int data[SIZE][SIZE]={0};//0->virus(病毒)1->cell(细胞)2->boundary(边界)int i,j,instruct;srand(time(NULL));//use the current time to seed the random number generator(使用当前时间种子的随机数发生器)for(i=0;i<=SIZE-1;i++){for(j=0;j<=SIZE-1;j++){if(i*j==0||i==SIZE-1||j==SIZE-1){data[i][j]=22;}//end if(如果结束)elsedata[i][j]=11*(rand()%2);}//end for(结束)}//end forsystem("mode con cols=100 & color 0f");//to set the size and the color of the window (设置大小和颜色的窗口)//input a random number except 0 to continue the game (输入一个随机数除0继续游戏)while(scanf("%d",&instruct)!=0){system("cls");//clear the screenfor(i=0;i<=SIZE-1;i++){for(j=0;j<=SIZE-1;j++){if(data[i][j]%10==1){SetColor(10);//function call(函数的调用)printf("%c ",6);//display the character(显示字符)}else if(data[i][j]%10==0){SetColor(6);//function call(函数的调用)printf("%c ",4);//display the character(显示字符)}else{SetColor(3);//function call(函数的调用)printf("%c ",3);//display the character (显示字符)}if(i*j==0||i==SIZE-1||j==SIZE-1){;}elsedata[i][j]=(data[i][j]%10)*10;}printf("\n");}rule(data);//function call(函数的调用)printf("请输入1继续0结束\n");}//end while(结束时)return 0;}void rule(int array[SIZE][SIZE]){int amount,i,j,k,l;for(i=1;i<=SIZE-2;i++){for(j=1;j<=SIZE-2;j++){amount=0;for(k=i-1;k<=i+1;k++){for(l=j-1;l<=j+1;l++){if(array[k][l]/10==1&&(k!=i||l!=j)){amount++;}//end if(如果结束)}//end for(结束)}//end for//** the rule of the game of life(游戏的规则生活)if(amount==3){array[i][j]++;}else if(amount==2){array[i][j]+=array[i][j]/10;}else;}//end for}//end for}。
C#生命游戏控制台程序

生命游戏又称细胞自动机。
反映了生命演化的规则。
生命游戏原理:生命生活在二维环境中,每个生命生活在一个方格中,每个细胞在下一个时刻的状态取决于周围8个细胞的活着或死了的状态。
1) 如果周围活着的细胞数量少于设定的值,细胞将孤独而死。
2) 如果周围活着的细胞数量多于设定的值,细胞将拥挤而死。
using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace Demo03{class Program{static void Main(string[] args){CLifeGame g1 = new CLifeGame();Console.WriteLine("游戏初始化:");Console.WriteLine();g1.Initial();g1.Run();Console.WriteLine();Console.Write("是否继续(Y or N):");string i = Console.ReadLine();while (i == "y" || i == "Y"){Console.WriteLine();g1.Run();Console.WriteLine();Console.Write("是否继续(Y or N):");i = Console.ReadLine();}Console.WriteLine();Console.Write("游戏中止!");}}}using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace Demo03{class CLifeGame{int[,] struArr = new int[10, 10];int[,] struArr2 = new int[10, 10];Random rad = new Random();public void Initial(){for (int i = 0; i < 10; i++)for (int j = 0; j < 10; j++){int a = rad.Next(0, 2);struArr[i, j] = a;struArr2[i, j] = struArr[i, j];}}public void Run(){for (int i = 0; i < 10; i++)for (int j = 0; j < 10; j++){int count = 0;if (i == 0){if (j == 0){if (struArr[i, j + 1] == 1) { count++; }if (struArr[i + 1, j] == 1) { count++; }if (struArr[i + 1, j + 1] == 1) { count++; }}else if (j == 9){if (struArr[i, j - 1] == 1) { count++; }if (struArr[i + 1, j] == 1) { count++; }if (struArr[i + 1, j - 1] == 1) { count++; }}else{if (struArr[i, j - 1] == 1) { count++; }if (struArr[i, j + 1] == 1) { count++; }if (struArr[i + 1, j] == 1) { count++; }if (struArr[i + 1, j + 1] == 1) { count++; }if (struArr[i + 1, j - 1] == 1) { count++; }}}else if (i == 9){if (j == 0){if (struArr[i, j + 1] == 1) { count++; }if (struArr[i - 1, j] == 1) { count++; }if (struArr[i - 1, j + 1] == 1) { count++; }}else if (j == 9){if (struArr[i, j - 1] == 1) { count++; }if (struArr[i - 1, j] == 1) { count++; }if (struArr[i - 1, j - 1] == 1) { count++; } }else{if (struArr[i, j - 1] == 1) { count++; }if (struArr[i, j + 1] == 1) { count++; }if (struArr[i - 1, j] == 1) { count++; }if (struArr[i - 1, j + 1] == 1) { count++; }if (struArr[i - 1, j - 1] == 1) { count++; } }}else if (j == 0){if (struArr[i - 1, j] == 1) { count++; }if (struArr[i - 1, j + 1] == 1) { count++; }if (struArr[i, j + 1] == 1) { count++; }if (struArr[i + 1, j + 1] == 1) { count++; }if (struArr[i + 1, j] == 1) { count++; }}else if (j == 9){if (struArr[i - 1, j] == 1) { count++; }if (struArr[i - 1, j - 1] == 1) { count++; }if (struArr[i, j - 1] == 1) { count++; }if (struArr[i + 1, j - 1] == 1) { count++; }if (struArr[i + 1, j] == 1) { count++; }}else{if (struArr[i - 1, j - 1] == 1) { count++; }if (struArr[i - 1, j] == 1) { count++; }if (struArr[i - 1, j + 1] == 1) { count++; }if (struArr[i, j - 1] == 1) { count++; }if (struArr[i, j + 1] == 1) { count++; }if (struArr[i + 1, j - 1] == 1) { count++; }if (struArr[i + 1, j] == 1) { count++; }if (struArr[i + 1, j + 1] == 1) { count++; }}switch (count){case 3:struArr2[i, j] = 1;break;case 2:if (struArr[i, j] == 1){ struArr2[i, j] = 1; }break;default:struArr2[i, j] = 0;break;}}for (int i = 0; i < 10; i++){for (int j = 0; j < 10; j++){if (struArr[i, j] == 1) Console.Write("1");if (struArr[i, j] == 0) Console.Write("0");}Console.WriteLine();}for (int i = 0; i < 10; i++)for (int j = 0; j < 10; j++)struArr[i, j] = struArr2[i, j];}}}。
生命游戏-JAVA语言代码-细胞自动机-毕业论文

生命游戏-JAVA语言代码-细胞自动机-毕业论文浙江理工大学毕业论文(设计)诚信声明我谨在此保证:本人所写的毕业论文(设计),凡引用他人的研究成果均已在参考文献或注释中列出。
论文(设计)主体均由本人独立完成,没有抄袭、剽窃他人已经发表或未发表的研究成果行为。
如出现以上违反知识产权的情况,本人愿意承担相应的责任。
声明人(签名):年月日摘要本文利用Java 作为编程语言,Java swing编写图形界面实现了生命游戏的可视化编程,并且在生命游戏的基础上进行了一定的扩展,增加了系统复杂性,给定了简单的初始状态以此进一步研究细胞自动机在复杂系统中所表现的能力,为细胞自动机及生命游戏的后续研究奠定基础。
结论:计算机实现的细胞自动机在计算机模拟的复杂系统中确实拥有复杂系统所表现出来的自适应性,不确定性等特性关键词:生命游戏;细胞自动机;复杂系统ABSTRACTUsing Java as a programming language, Java Swing as graphical interface to achieve a visual programming of the Game of Life. Expand the basis of the game of life, increased system complexity, given the simple initial state in order to further studythe demonstrated ability of cellular automata in a complex system, lay the foundation for the follow-up study of cellular automata and the Game of Life. Conclusion:The computer-implemented cellular automaton computer simulation of complex systems do have complex systems demonstrated by the self-adaptive, uncertainties and other characteristicsKeywords: Game of Life; Cellular Automata;Complex System目录摘要Abstract第一章绪论 ..................................................................... .........................................................1 1.1引言 ..................................................................... ..................................................................1 1.2生命游戏 ..................................................................... ..........................................................2 1.2.1什么是生命游戏 ..................................................................... ..........................................2 1.2.2生命游戏、细胞自动机的研究意义 ..................................................................... ............2 第二章细胞自动机、生命游戏与复杂系统 ..................................................................... .3 2.1复杂系统 ...............................................................................................................................3 2.2细胞自动机...................................................................... ......................................................4 第三章我的生命游戏的实现 ..................................................................... ...........................4 3.1 我的生命游戏与传统生命游戏的区别 ..................................................................... ............4 3.1.1传统生命游戏 ..................................................................... ..............................................4 3.1.2我的生命游戏 ..................................................................... ..............................................4 3.2 基本思想 ..................................................................... .........................................................5 3.3 基本概念 ..................................................................... ...........................................................6 3.3.1系统基本概念 ..................................................................... ..............................................6 3.3.2个体基本属性 ..................................................................... ..............................................7 3.3.3环境基本定义 ..................................................................... ..............................................7 3.4 程序主流程...........................................................................................................................8 3.5 核心算法介绍 ..................................................................... ..................................................9 3.6 生物个体方法 ..................................................................... ................................................ 10 3.6.1生长 ..................................................................... ........................................................... 10 3.6.2获取竞争力,竞争力基于个体所处环境, ....................................................................10 3.6.3繁殖,变异,进化, ................................................................... ................................... 10 3.6.4获取所需食物量 ..................................................................... ........................................ 11 3.7 环境规则 ..................................................................... . (11)3.8 关键参数设置 ..................................................................... ................................................ 11 3.9 预测结果及理论分析 ..................................................................... .................................... 12 3.10 运行结果及结论分析...................................................................... . (13).................................................................... ....................................... 17 3.11 本文尚未完成部分致谢 ..................................................................... ................................................................ 19 参考文献 ..................................................................... .. (20)浙江理工大学本科毕业设计(论文)第一章绪论1.1引言系统是由相互关联,相互制约,相互作用的部分所组成的具有某种功能的有机整体。
约瑟夫生死游戏(C )数据结构实现

题目二:约瑟夫生者死者游戏(链表存储)一:【内容与要求】约瑟夫游戏的大意是:每30个旅客同乘一条船,因为严重超载,加上风高浪大,危险万分;因此船长告诉乘客,只有将全船一半的旅客投入还中,其余人才能幸免遇难。
无奈,大家只得同意这种办法,并议定30个人围成一圈,由第一个人数起,依次报数,数到第9人,便把他投入大海中,然后再从他的下一个人数起,数到第9人,再将他扔进大海中,如此循环地进行,直到剩下15个乘客为止。
问哪些位置是将被扔下大海的位置。
二:概要设计利用链表循环来解决。
首先,就必须先定义一个链表,按照所需要的长度进行定义,然后令其为指针指向头指针,即完成了一个循环链表的创建。
接下来先打印链表输出。
其次,就是算法实现,需要利用指针来进行,数据域标记人员编号,先用一个指针循环查找,找到第一个需要删除的人,标记为1,先输出节点数,再进行删除。
依次循环查找,直到被删除的节点数量为总人数的一半的时候则结束。
三:程序执行流程图三:详细代码结构1.链表的创建(1)创建头节点Josephring(){head=new Node;//为头结点申请空间head->no=1;//为数据域赋值head->next=head;//形成循环链表}(2) 循环插入链表void Josephring::CreateJosephus(int n) {Node*s=head;//标记头结点totalnum=n;for(int i=2;i<=n;i++){ Node*w=new Node;//新建一个节点w->no=i;w->next=head;s->next=w;s=w;//S作为尾指针}}首先申请一个节点,并且W指针指向它,然后从2开始赋值,此时先令新节点的W指针指向头结点,再令S指针指向它,依次循环插入创建。
2.打印输出链表void Josephring::show(){cout<<head->no<<"\t";//先输出头节点Node*q=head->next;while(q!=head){cout<<q->no<<"\t";q=q->next;}}先打印输出头结点,然后循环判定,将不等于头结点的全部输出。
过河死代码c语言

过河死代码c语言在C语言中,我们可以创建一个模拟程序来模拟"过河死"的场景。
这个程序将模拟一个人试图过河,但是如果他遇到危险,他就会"死"。
以下是一个简单的C语言代码示例:```cinclude <>int main() {int river_crossing = 0; // 初始状态为0,表示人不在河的任何一边int is_danger = 0; // 初始状态为0,表示没有危险printf("开始过河...\n");while (1) { // 无限循环,直到遇到危险或成功过河if (river_crossing == 0 && is_danger == 0) { // 如果人不在河的任何一边,并且没有危险river_crossing = 1; // 人开始过河printf("正在过河...\n");} else if (river_crossing == 1 && is_danger == 0) { // 如果人在河的另一边,并且没有危险printf("成功过河!\n");break; // 跳出循环,表示成功过河} else if (river_crossing == 0 && is_danger == 1) { // 如果人还在河的一边,但是有危险printf("遇到危险,游戏结束!\n");break; // 跳出循环,表示游戏结束} else if (river_crossing == 1 && is_danger == 1) { // 如果人在河的另一边,但是有危险printf("在河的另一边也有危险,游戏结束!\n");break; // 跳出循环,表示游戏结束}}return 0;}```在这个程序中,`river_crossing`变量表示人是否在河的一边或另一边,`is_danger`变量表示是否遇到危险。
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
本世纪70年代,人们曾疯魔一种被称作“生命游戏”的小游戏,这种游戏相当简单。
假设有一个像棋盘一样的方格网,每个方格中放置一个生命细胞,生命细胞只有两种状态:“生”或“死”。
游戏规则如下:
1. 如果一个细胞周围有3个细胞为生(一个细胞周围共有8个细胞),则该细胞为生,即该细胞若原先为死,则转为生,若原先为生,则保持不变;
2. 如果一个细胞周围有2个细胞为生,则该细胞的生死状态保持不变;
3. 在其它情况下,该细胞为死,即该细胞若原先为生,则转为死,若原先为死,则保持不变。
依此规则进行迭代变化,使细胞生生死死,会得到一些有趣的结果。
该游戏之所以被称为“生命游戏”,是因为其简单的游戏规则,反映了自然界中的生存规律:如果一个生命,其周围的同类生命太少的话,会因为得不到帮助而死亡;如果太多,则会因为得不到足够的资源而死亡。
用计算机模拟这个“生命游戏”也相当简单,可以用一个M×N像素的图像来代表M×N个细胞,其中每一个像素,代表一个细胞,像素为黑色表示细胞为生,像素为白色代表细胞为死。
设定图像中每个像素的初始状态后依据上述的游戏规则演绎生命的变化,由于初始状态和迭代次数不同,将会得到令人叹服的优美图案。
下面给出的小程序是用TC2.0编写。
演示100×100个生命细胞初始状态全为生时的变代情况,变化时边缘细胞不参与变化。
随着迭代次数的不同,在屏幕显示的图案精彩纷呈,像万花筒般引人入胜。
#include <graphics.h>
main(){
int orgData[100][100],resData[100][100];/*分别记录每次迭代
的初始和结果状态*/
int nCount,nRows,nCols,i,j,times; /*times记录迭代次数*/
int GraphDriver=DETECT,GraphMode;
for (i=0;i<100;i++) /*初始化数据,令每一个细胞为生*/
for (j=0;j<100;j++) orgData[i][j]=1;
initgraph(&GraphDriver,&GraphMode,′′′′); /*初始化屏幕
显示*/
setcolor(WHITE);
rectangle(270,190,370,290); /*作显示边框*/
for (times=1;times<200;times++){
for (nRows=1;nRows<99;nRows++) {
for (nCols=1;nCols<99;nCols++){
/*计算每一个细胞周围的活的细胞数*/
nCount=orgData[nRows-1][nCols-1]+orgData[nRows-1][nCols]
+orgData[nRows-1][nCols+1]+orgData[nRows][nCols-1]
+orgData[nRows][nCols+1]+orgData[nRows+1][nCols-1]
+orgData[nRows+1][nCols]+orgData[nRows+1][nCols+1];
switch(nCount){
/*周围有3个活细胞,该细胞为生,在屏幕上用黑色像素表示*
case 3: putpixel(nCols+210,120+nRows,BLACK);
resData[nRows][nCols]=1;break;
/*周围有2个活细胞,该细胞不变,在屏幕显示也不变*/
case 2: resData[nRows][nCols]=orgData[nRows][nCols];
break;
/*其它情况下,细胞为死,在屏幕上用白色像素表示*/
default:resData[nRows][nCols]=0;
putpixel(nCols+210,120+nRows,WHITE);
}
}
}
for (i=1;i<99;i++)
for (j=1;j<99;j++) orgData[i][j]=resData[i][j];
getch();
}
}
在计算机上运行上述程序,得到迭代次数为45、69、74、78、97、116、119和156时的图像分别如上图所示。
在实际模拟时,可以取更多的生命细胞,也可以考虑生命细胞的初始状态是依一定概率设定的随机状态,变化时也可以让边缘细胞参与变化。
只要对上述程序略作更改,就会得到另外一系列美妙绝伦的图案。
用c调用sendmail发邮件
最近在做一个小项目,需要用到c来调用sendmail发送邮件,参考了:
/FAQ/cxsjl/rjgc/2007/11/1484369104490.html
的提示,做出来了以下的实现:
意思是打开两个文件,一个是管道的fp,一个是写好了邮件内容的文件,这样就可以发送了,厉害啊
[root@bjxdurs235 20090816]# cat -n sendmail.c
1 #include<stdio.h>
2 #define MAX_LINE_CHAR 512
3
4 int main(void)
5 {
6 char *line;
7 char buffer[MAX_LINE_CHAR+1];
8 FILE *email_txt;
9 FILE *fp;
10
11
12 email_txt = fopen( "email.txt","r" );
13 if( email_txt == NULL ){
14 perror("email.txt");
15 exit (1);
16 }
17 fp=popen("/usr/lib/sendmail -F monitor -t","w");
18
19 while ( (line = fgets( buffer, MAX_LINE_CHAR, email_txt )) != NULL ){
20 fprintf(fp,"%s",buffer);
21
22 }
23 pclose(fp);
24
25 }
最后没有经过一个编译错误,直接运行,邮件发送成功,第一次写程序这么顺利,难得啊。