康威生命游戏 Conway's game of life

来源:互联网 发布:ios足球游戏源码 编辑:程序博客网 时间:2024/05/16 16:18

一、需求描述

你的任务是写一个程序来计算给定起始位置下的下一代康威生命游戏。

游戏从一个两维的网格开始,每一个网格有两种状态:存活、死亡。

网格是有限的,没有生命可以存活在边界之外。当计算下一代网格时,需要遵循下述四个规则:


1. 任何四周邻居存活数少于两个的存活网格将死亡,因为人口稀少。

2. 任何四周邻居存活数多于三个的存活网格将死亡,因为过度拥挤。

3. 任何四周邻居存活数等于两个或三个的存活网格将在下一代中继续存活。

4. 任何已经死亡的网格,如果周围邻居存活数为3个,将重新复活。


二、程序输入输出

输入:当代网格信息,包括网格大小(即几行几列),以及每个网格的状态(存活或死亡)

输出:按照上面的规则,计算下一代网格,并输出

具体的格式参见下面的示例


三、示例

示例: * 存活网格, . 死亡网格


示例输入:(4 x 8 网格)

4 8

........

....*...

...**...

........


示例输出:

4 8

........

...**...

...**...

........


四、题目英文原文描述

Your task is to write a program to calculate the nextgeneration of Conway's game of life, given any starting

position. You start with a two dimensional grid of cells,where each cell is either alive or dead. The grid is finite,

and no life can exist off the edges. When calculating thenext generation of the grid, follow these four rules:


1. Any live cell with fewer than two live neighbours dies,   as if caused by underpopulation.

2. Any live cell with more than three live neighbours dies,   as if by overcrowding.

3. Any live cell with two or three live neighbours lives   on to the next generation.

4. Any dead cell with exactly three live neighbours becomes   a live cell.


Examples: * indicates live cell, . indicates dead cell


Example input: (4 x 8 grid)

4 8

........

....*...

...**...

........


Example output:

4 8

........

...**...

...**...

........

C语言代码

#include <string.h>#include <stdio.h>#include <assert.h>#define gRow 4#define gline 8char gGroupCell[gRow][gline + 1] = {    "........",    "....*.*.",    "...**...",    "...**..." };void get_33grid(int row, int line, char CellGrid33[3][3]){    int loopnum = 0;    int row33 = 0;    int line33 = 0;    for (row33 = 0; row33 < 3; row33++)    {        for (line33 = 0; line33 < 3; line33++)        {            if (((0 == row) && (0 == row33)) ||                (((gRow - 1) == row) && (2 == row33)) ||                ((0 == line) && (0 == line33)) ||                (((gline - 1) == line) && (2 == line33)))            {                CellGrid33[row33][line33] = '.';                continue;            }            CellGrid33[row33][line33] = gGroupCell[row - 1 + row33][line - 1 + line33];        }    }}char middle_cell_handle(char InputCell[3][3]){    int row = 0;    int line = 0;    int neighbor = 0;    //char CharCell[3][3];    //int  IntCell[3][3];    char MiddleCell = '.';    //memset(CharCell, 0, sizeof(CharCell));   // memset(IntCell, 0, sizeof(IntCell));    if (NULL == InputCell)        return '.';    //memcpy(IntCell, InputCell, sizeof(InputCell));    for (row = 0; row < 3; row++)    {        for (line = 0; line < 3; line++)        {            if ('*' == InputCell[row][line])                neighbor += 1;        }    }    if (3 == neighbor)    {        MiddleCell = '*';    }    else if (('*' == InputCell[1][1]) && (2 == neighbor))    {        MiddleCell = '*';    }    else    {        MiddleCell = '.';    }    return MiddleCell;}char *cell_life(char InputCell[4][9]){    int row = 0;    int line = 0;    char ChildCell[gRow][gline + 1] = { 0 };    char CellGrid33[3][3] = { 0 };    for (row = 0; row < gRow; row++)    {        for (line = 0; line < gline; line++)        {            get_33grid(row, line, CellGrid33);            ChildCell[row][line] = middle_cell_handle(CellGrid33);        }        ChildCell[row][gline] = '\0';    }    memcpy(gGroupCell, ChildCell, sizeof(gGroupCell));    return gGroupCell;}void print_cell_group(char InputCell[gRow][gline + 1]){    int row = 0;    int line = 0;    for (row = 0; row < gRow; row++)    {        for (line = 0; line < gline; line++)        {            printf("%c", InputCell[row][line]);        }        printf("\n");    }    printf("\n============\n");}char test_cell_life(int row, int line){    char CellGrid33[3][3] = { 0 };    get_33grid(row, line, CellGrid33);    return middle_cell_handle(CellGrid33);}test_case(){    assert('*'==test_cell_life(1, 3));    assert('*' == test_cell_life(1, 4));    assert('*' == test_cell_life(1, 5));    assert('.' == test_cell_life(1, 6));    assert('.' == test_cell_life(2, 4));}void main(){#if 0    print_cell_group(gGroupCell);    cell_life(gGroupCell);    print_cell_group(gGroupCell);    system("pause");#else    test_case();    system("pause");#endif}


1 0
原创粉丝点击