C++ 生命游戏

依然是跟着知乎上的Dalao学习。地址在这:

https://zhuanlan.zhihu.com/p/24768071



以下代码:


#include<iostream>
#include<conio.h>
#include<stdlib.h>
#include<windows.h>
#include<time.h>
using namespace std;

#define High 25
#define Width 50

int cells[25][50];

void gotoxy(int x, int y)
{
	HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
	COORD pos;
	pos.X = x;
	pos.Y = y;
	SetConsoleCursorPosition(handle, pos);
}

void startup()
{
	int i, j;
	for ( i = 0; i < High; i++)
	{
		for ( j = 0; j < Width; j++)
		{
			cells[i][j] = 1;
		}
	}
}

void show()
{
	gotoxy(0, 0);
	int i, j;
	for ( i = 1; i <= High-1; i++)
	{
		for ( j = 1; j <= Width-1; j++)
		{
			if (cells[i][j]==1)
			{
				cout << "*";
			}
			else
			{
				cout << " ";
			}
		}
		cout << endl;
	}
	Sleep(50);
}

void UpdateWithoutInput()
{
	int newcells[25][50];
	int neibournumber;
	int i, j;
	for ( i = 1; i <= High-1; i++)
	{
		for ( j = 1; j <= Width-1; j++)
		{
			neibournumber = cells[i - 1][j - 1] + cells[i - 1][j] + cells[i - 1][j + 1] + cells[i][j - 1] + cells[i][j + 1] + cells[i + 1][j - 1] + cells[i + 1][j] + cells[i + 1][j + 1];
			if (neibournumber == 3)
			{
				newcells[i][j] = 1;
			}
			else if (neibournumber == 2)
			{
				newcells[i][j] = cells[i][j];
			}
			else
				newcells[i][j] = 0;
		}
	}
	for (i = 1; i <= High - 1; i++)
	{
		for ( j = 1; j <= Width-1 ; j++)
		{
			cells[i][j] = newcells[i][j];
		}
	}
}

void UpdateWithInput()
{

}

int main()
{
	startup();
	while (1)
	{
		show();
		UpdateWithoutInput();
		UpdateWithInput();
	}
	return 0;
}


  • 1
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个简单的 C++ 生命游戏代码: ```c++ #include <iostream> #include <cstdlib> #include <ctime> using namespace std; const int ROW = 20; const int COL = 20; void init_board(int board[][COL]) { srand(time(nullptr)); for (int i = 0; i < ROW; i++) { for (int j = 0; j < COL; j++) { board[i][j] = rand() % 2; } } } void print_board(int board[][COL]) { for (int i = 0; i < ROW; i++) { for (int j = 0; j < COL; j++) { if (board[i][j] == 1) { cout << "* "; } else { cout << ". "; } } cout << endl; } } int count_neighbors(int board[][COL], 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 r = row + i; int c = col + j; if (r < 0 || r >= ROW || c < 0 || c >= COL) { continue; } count += board[r][c]; } } return count; } void update_board(int board[][COL]) { int new_board[ROW][COL] = {0}; for (int i = 0; i < ROW; i++) { for (int j = 0; j < COL; j++) { int count = count_neighbors(board, i, j); if (board[i][j] == 1) { if (count < 2) { new_board[i][j] = 0; } else if (count == 2 || count == 3) { new_board[i][j] = 1; } else { new_board[i][j] = 0; } } else { if (count == 3) { new_board[i][j] = 1; } else { new_board[i][j] = 0; } } } } for (int i = 0; i < ROW; i++) { for (int j = 0; j < COL; j++) { board[i][j] = new_board[i][j]; } } } int main() { int board[ROW][COL] = {0}; init_board(board); for (int i = 0; i < 10; i++) { print_board(board); update_board(board); cout << endl; } return 0; } ``` 该代码使用了二维数组来表示生命游戏的棋盘,使用随机数来初始化棋盘中的细胞。`count_neighbors` 函数用于计算某个细胞周围的存活细胞数量,`update_board` 函数用于更新棋盘中的细胞状态。最后,`main` 函数使用循环调用 `print_board` 和 `update_board` 函数来模拟生命游戏的演化过程。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值