康威生命游戏Game of life

游戏介绍

生命游戏是一个零玩家游戏。它包括一个二维矩形世界,这个世界中的每个方格居住着一个活着的或死了的细胞。一个细胞在下一个时刻生死取决于相邻八个方格中活着的或死了的细胞的数量。如果相邻方格活着的细胞数量过多,这个细胞会因为资源匮乏而在下一个时刻死去;相反,如果周围活细胞过少,这个细胞会因太孤单而死去。实际中,玩家可以设定周围活细胞的数目怎样时才适宜该细胞的生存。如果这个数目设定过高,世界中的大部分细胞会因为找不到太多的活的邻居而死去,直到整个世界都没有生命;如果这个数目设定过低,世界中又会被生命充满而没有什么变化。

规则:

生命游戏中,对于任意细胞,规则如下:

  • 每个细胞有两种状态 - 存活死亡,每个细胞与以自身为中心的周围八格细胞产生互动(如图,黑色为存活,白色为死亡)
  • 当前细胞为存活状态时,当周围的存活细胞低于2个时(不包含2个),该细胞变成死亡状态。(模拟生命数量稀少)
  • 当前细胞为存活状态时,当周围有2个或3个存活细胞时,该细胞保持原样。
  • 当前细胞为存活状态时,当周围有超过3个存活细胞时,该细胞变成死亡状态。(模拟生命数量过多)
  • 当前细胞为死亡状态时,当周围有3个存活细胞时,该细胞变成存活状态。(模拟繁殖)

可以把最初的细胞结构定义为种子,当所有在种子中的细胞同时被以上规则处理后,可以得到第一代细胞图。按规则继续处理当前的细胞图,可以得到下一代的细胞图,周而复始。

#include <iostream>
#include <vector>
#include <thread>
#include <chrono>
#include <atomic>
using namespace std;

class GameOfLife {
public:
    GameOfLife(int rows, int cols) : rows(rows), cols(cols), grid(rows, vector<int>(cols)) {}

    void init() {
        // Initialize the grid with some live cells
        grid[1][2] = 1;
        grid[2][3] = 1;
        grid[3][1] = 1;
        grid[3][2] = 1;
        grid[3][3] = 1;
    }

    bool run() {
        vector<vector<int>> newGrid(rows, vector<int>(cols));
        for (int i = 0; i < rows; i++) {
            for (int j = 0; j < cols; j++) {
                int liveNeighbors = countLiveNeighbors(i, j);
                if (grid[i][j] == 1) {
                    if (liveNeighbors < 2 || liveNeighbors > 3) {
                        newGrid[i][j] = 0;
                    } else {
                        newGrid[i][j] = 1;
                    }
                } else {
                    if (liveNeighbors == 3) {
                        newGrid[i][j] = 1;
                    }
                }
            }
        }
        if (grid == newGrid) {
            return false;
        } else {
            grid = newGrid;
            return true;
        }
    }

    void display() {
    // Clear console and move cursor to the start of the grid
    cout << "\033[2J\033[1;1H";

    for (int i = 0; i < rows; i++) {
        for (int j = 0; j < cols; j++) {
            if (grid[i][j] == 0) {
                cout << " ";
            } else {
                cout << "*";
            }
            cout << " ";
        }
        cout << endl;
    }
    cout << endl;
}


private:
    int rows;
    int cols;
    vector<vector<int>> grid;

    int countLiveNeighbors(int row, int col) {
        int count = 0;
        for (int i = row - 1; i <= row + 1; i++) {
            for (int j = col - 1; j <= col + 1; j++) {
                if (i == row && j == col) continue;
                if (i >= 0 && i < rows && j >= 0 && j < cols && grid[i][j] == 1) {
                    count++;
                }
            }
        }
        return count;
    }
};

atomic<bool> stop(false);

void checkInput() {
    char c;
    while (true) {
        cin >> c;
        if (c == 'q') {
            stop = true;
            break;
        }
    }
}

int main() {
    GameOfLife game(10, 10);
    game.init();
    game.display();

    thread inputThread(checkInput);

    while (!stop) {
        std::this_thread::sleep_for(std::chrono::milliseconds(500));
        bool changed = game.run();
        if (!changed) break;
        game.display();
    }

    inputThread.join();
}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值