5613-3-康威生命游戏

这篇博客介绍了如何用C语言编写康威生命游戏。游戏规则包括:细胞周围有3个活细胞则存活,2个则保持状态,其他情况死亡。代码在Ubuntu上可以直接运行,具有边界自定义和随机生成初始细胞的能力。博客展示了运行示例,并说明了边界条件的处理方法。

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

康威生命游戏,详细见百度https://baike.baidu.com/item/%E7%94%9F%E5%91%BD%E6%B8%B8%E6%88%8F/2926434

简而言之就是在一个类似于棋盘的区域内,每个点上可以“生存”或者“死去”一个“棋子”,或者说,“细胞”。其规则如下:

1. 如果一个细胞周围有3个细胞为生(一个细胞周围共有8个细胞),则该细胞为生(即该细胞若原先为死,则转为生,若原先为生,则保持不变) 。

2. 如果一个细胞周围有2个细胞为生,则该细胞的生死状态保持不变;

3. 在其它情况下,该细胞为死(即该细胞若原先为生,则转为死,若原先为死,则保持不变)

现在,如何用c语言来写一个简单的生命游戏呢?

So show you my code:

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <time.h>

#define MAXROW 40
#define MAXCOL 40
#define DEAD 0
#define ALIVE 1

int map[MAXROW][MAXCOL], newmap[MAXROW][MAXCOL];

void init();
int neighbors(int, int);
void outputMap();
void copyMap();

// main
int main() 
{
    srand(time(0));
    int i,j;
    int row, col;
    char ans;

    // random start
    for(row = 0; row < MAXROW; row++)
        for(col = 0; col < MAXCOL; col++)
            map[row][col] = DEAD;
    for(j=0;j<=100;j++) 		// j = # of alive
    {
	int row = rand() % 40 + 1;
	int col = rand() % 40 + 1;	
        if(0 <= row && row < MAXROW &&
           0 <= col && col < MAXCOL)
            map[row][col] = ALIVE;
    }

    printf("\nThe game begin:");
    outputMap();
    for(i=1;i<=100;i++) 			// 100 iterations
    {
        for(row = 0; row < MAXROW; row++) 
        {
            for(col = 0; col < MAXCOL; col++) 
            {
                switch (neighbors(row, col)) 
                {
                    case 0:
                    case 1:
                    case 4:
                    case 5:
                    case 6:
                    case 7:
                    case 8:
                        newmap[row][col] = DEAD;
                        break;
                    case 2:
                        newmap[row][col] = map[row][col];
                        break;
                    case 3:
                        newmap[row][col] = ALIVE;
                        break;
                }
            }
        }
        copyMap();
    }
    printf("\n\nThe end of the game:");
    outputMap();
    printf("\n");
}

// neighbors
int neighbors(int row, int col) 
{
    int count = 0, c, r, c1, r1;
    for(r = row-1; r <= row+1; r++)
        for(c = col-1; c <= col+1; c++) 
        {
            if(r < 0 || r >= MAXROW || c < 0 || c >= MAXCOL)
                continue;
// boundary conditions
	    if(r == MAXROW -1 && (map[0][c] == ALIVE
### 康威生命游戏的实现 康威生命游戏是一种基于细胞自动机理论的经典模拟程序。下面展示了一个使用 Dev-C++ 编译器编写的生命游戏 C++ 源代码示例[^1]。 ```cpp #include <iostream> #include <vector> #include <cstdlib> // For system("pause") using namespace std; const int WIDTH = 20; const int HEIGHT = 20; void printGrid(const vector<vector<int>>& grid) { for (int row = 0; row < HEIGHT; ++row) { for (int col = 0; col < WIDTH; ++col) { cout << ((grid[row][col]) ? "O" : "."); } cout << endl; } } // 计算邻居数量 int countNeighbors(int row, int col, const vector<vector<int>>& grid) { int neighbors = 0; for (int i = -1; i <= 1; ++i) { for (int j = -1; j <= 1; ++j) { if (!(i == 0 && j == 0)) { // 排除自己 int newRow = row + i; int newCol = col + j; if (newRow >= 0 && newRow < HEIGHT && newCol >= 0 && newCol < WIDTH) { neighbors += grid[newRow][newCol]; } } } } return neighbors; } void updateGrid(vector<vector<int>>& oldGen, vector<vector<int>>& newGen) { for (int row = 0; row < HEIGHT; ++row) { for (int col = 0; col < WIDTH; ++col) { int liveNeighbors = countNeighbors(row, col, oldGen); if (oldGen[row][col] == 1 && (liveNeighbors < 2 || liveNeighbors > 3)) newGen[row][col] = 0; // 细胞死亡 else if (oldGen[row][col] == 0 && liveNeighbors == 3) newGen[row][col] = 1; // 新生细胞 else newGen[row][col] = oldGen[row][col]; // 不变 } } } int main() { srand(time(0)); vector<vector<int>> currentGeneration(WIDTH, vector<int>(HEIGHT, 0)); vector<vector<int>> nextGeneration(WIDTH, vector<int>(HEIGHT, 0)); // 初始化随机状态 for (int row = 0; row < HEIGHT; ++row) { for (int col = 0; col < WIDTH; ++col) { currentGeneration[col][row] = rand() % 2; } } while(true){ system("cls"); // Clear screen on Windows or use &#39;clear&#39; on Unix-based systems. printGrid(currentGeneration); updateGrid(currentGeneration, nextGeneration); swap(currentGeneration, nextGeneration); this_thread::sleep_for(std::chrono::milliseconds(50)); // Sleep to slow down the simulation system("pause"); } return 0; } ``` 此代码实现了基本版本的生命游戏逻辑,包括网格初始化、更新规则应用以及图形化显示等功能。为了简化起见,这里采用控制台输出来表示活细胞(&#39;O&#39;)和死细胞(&#39;.&#39;)的状态变化过程。此外,在实际环境中可能还需要考虑边界条件处理等问题。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值