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

本文介绍了一个用C语言实现的康威生命游戏程序。该程序能够根据给定的初始状态计算出下一代的生命网格布局,并遵循游戏的经典规则进行迭代。文章提供了完整的源代码,包括网格获取、中间单元格处理及生命状态更新等功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、需求描述

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

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

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


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 8

char 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

}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值