21xrx.com
2025-03-24 04:31:24 Monday
文章检索 我的文章 写文章
C++语言编写的生命游戏代码
2023-06-22 04:41:23 深夜i     22     0
C++ 生命游戏 编写 代码

生命游戏是一款体现了生命与演化规律的经典游戏,它在计算机科学中也有很高的应用价值。C++语言是一门高效的编程语言,因其性能强大而广受程序员的喜爱。下面,我们将分享一份基于C++语言编写的生命游戏代码。

首先,我们需要了解一些生命游戏的规则和概念。在生命游戏中存在一个平面,我们称之为细胞宇宙,其中的每个格子都可以看作一个细胞。每一个细胞都有两种状态:生和死。当一个格子周围有3个生的格子时,它将自动复活;当一个格子周围有2或3个生的格子时,它将保持原有状态;当一个格子周围的生格子少于2个或大于3个时,它将死亡。

基于上述规则,我们可以编写以下的生命游戏代码:

#include <iostream>
#include <cstdlib>
#include <ctime>
namespace life_game {
  const int WIDTH = 100// 宇宙宽度
  const int HEIGHT = 50// 宇宙高度
  const int GENERATIONS = 100// 迭代次数
  // 随机生成初始宇宙
  void randomizeUniverse(bool universe[HEIGHT][WIDTH]) {
    srand((unsigned)time(NULL));
    for (int row = 0; row < HEIGHT; row++) {
      for (int col = 0; col < WIDTH; col++) {
        universe[row][col] = rand() % 2 == 1;
      }
    }
  }
  // 统计一个细胞周围的生细胞数
  int countLivingNeighbors(bool universe[HEIGHT][WIDTH], int row, int col) {
    int count = 0;
    for (int i = -1; i <= 1; i++) {
      for (int j = -1; j <= 1; j++) {
        if (i == 0 && j == 0) continue;
        int n_row = row + i, n_col = col + j;
        // 处理越界情况
        if (n_row < 0) n_row = HEIGHT - 1;
        else if (n_row >= HEIGHT) n_row = 0;
        if (n_col < 0) n_col = WIDTH - 1;
        else if (n_col >= WIDTH) n_col = 0;
        if (universe[n_row][n_col]) count++;
      }
    }
    return count;
  }
  // 更新宇宙状态
  void updateUniverse(bool universe[HEIGHT][WIDTH]) {
    bool new_universe[HEIGHT][WIDTH];
    for (int row = 0; row < HEIGHT; row++) {
      for (int col = 0; col < WIDTH; col++) {
        int living_neighbors = countLivingNeighbors(universe, row, col);
        if (universe[row][col]) { // 当前细胞为生细胞
          if (living_neighbors == 2 || living_neighbors == 3) {
            new_universe[row][col] = true// 继续存活
          } else {
            new_universe[row][col] = false// 死亡
          }
        } else// 当前细胞为死细胞
          if (living_neighbors == 3) {
            new_universe[row][col] = true// 复活
          } else {
            new_universe[row][col] = false// 继续死亡
          }
        }
      }
    }
    // 更新宇宙状态
    for (int row = 0; row < HEIGHT; row++) {
      for (int col = 0; col < WIDTH; col++) {
        universe[row][col] = new_universe[row][col];
      }
    }
  }
  // 打印宇宙状态
  void printUniverse(bool universe[HEIGHT][WIDTH]) {
    for (int row = 0; row < HEIGHT; row++) {
      for (int col = 0; col < WIDTH; col++) {
        std::cout << (universe[row][col] ? '#' : ' ');
      }
      std::cout << std::endl;
    }
  }
  // 主函数
  void run() {
    bool universe[HEIGHT][WIDTH];
    randomizeUniverse(universe);
    for (int i = 0; i < GENERATIONS; i++) {
      printUniverse(universe);
      updateUniverse(universe);
      // 暂停1秒
      #ifdef _WIN32
      system("pause > null");
      #else
      sleep(1);
      #endif
    }
  }
}
int main() {
  life_game::run();
  return 0;
}

以上是基于C++语言编写的生命游戏代码,其中定义了一个 `life_game` 命名空间,包含了随机生成初始宇宙、统计细胞周围生细胞数、更新宇宙状态、打印宇宙状态等函数,以及主函数 `run`。在主函数中,我们随机生成了初始宇宙并迭代更新宇宙状态,同时通过暂停一段时间来模拟生命游戏的动态过程。

C++语言的高效和生命游戏算法的完善,使得生命游戏程序的执行速度得到了保证,并且程序更加稳定可靠。生命游戏的实现不仅仅是一个程序,更是一段演化时空中的美妙旋律。

  
  

评论区