C++案例:C++版生命游戏

举报
howard2005 发表于 2021/12/29 23:56:31 2021/12/29
【摘要】 目录 一、生命游戏 1、生命游戏概述 2、生命演化规则:B3/S23 二、生命游戏C++实现 1、编写头文件life.h 2、编写C++程序life.cpp 3、编写头文件utility.h 4、编写程序文件utility.cpp 5、编写主程序文件main.cpp 6、运行程序,查看结果 三、尝试生命游戏其它初始...

目录

一、生命游戏

1、生命游戏概述

2、生命演化规则:B3/S23

二、生命游戏C++实现

1、编写头文件life.h

2、编写C++程序life.cpp

3、编写头文件utility.h

4、编写程序文件utility.cpp

5、编写主程序文件main.cpp

6、运行程序,查看结果

三、尝试生命游戏其它初始布局


一、生命游戏

1、生命游戏概述

在研究元胞自动机理论过程中,Conway发明生命游戏(Game of Life、GoL),在上个世纪七十年代风靡一时。

这是0人游戏,即按照初始的设置,游戏自动演化。在类似围棋的棋盘中,每一个格子可以是空格或者存在一个生命/细胞/Cell;每一个格子有8个相邻的格子(正上方、正下方、右侧、左侧、左上方、右上方、左下方以及右下方),相邻的格子中存活的生命数量称为其邻居(neighbor)数。在世代交替时,所有的格子根据其邻居数,诞生新生命、Cell保持存活或者Cell死亡。

2、生命演化规则:B3/S23

  • 一个生命如果恰好有2个或3个邻居,它会存活到下一个世代;否则,会因为孤独或拥挤而死亡。
  • 一个空格,如果恰好有3个邻居,则诞生一个新生命。

二、生命游戏C++实现

在Dev C++里创建项目TheGameOfLife,创建头文件与C++源文件,如下图所示:

1、编写头文件life.h


  
  1. const int maxrow = 20, maxcol = 60; // grid dimensions
  2. class Life {
  3. public:
  4. void initialize();
  5. void print();
  6. void update();
  7. private:
  8. int grid[maxrow + 2][maxcol + 2]; // allows for two extra rows and columns
  9. int neighbor_count(int row, int col);
  10. };
  11. #define DONE
  12. #include "life.cpp"

2、编写C++程序life.cpp


  
  1. #ifdef DONE
  2. void Life::initialize()
  3. /*Pre: None
  4. Post: The Life object contains a configuration specified by the user.*/
  5. {
  6. int row, col;
  7. for (row = 0; row <= maxrow + 1; row++)
  8. for (col = 0; col <= maxcol + 1; col++)
  9. grid[row][col] = 0;
  10. cout << "List the coordinates for living cells." << endl;
  11. cout << "Terminate the list with the special pair -1 -1" << endl;
  12. cin >> row >> col;
  13. while (row != -1 || col != -1) {
  14. if (row >=1 && row <= maxrow)
  15. if (col >=1 && col <= maxcol)
  16. grid[row][col] = 1;
  17. else
  18. cout << "Column " << col << " is out of range." << endl;
  19. else
  20. cout << "Row " << row << " is out of range." << endl;
  21. cin >> row >> col;
  22. }
  23. }
  24. void Life::print()
  25. /*Pre: The Life object contains a configuration.
  26. Post: The configuration is written for the user.*/
  27. {
  28. int row, col;
  29. cout << "\nThe current Life configuration is:" << endl;
  30. for (row = 1; row <= maxrow; row++) {
  31. for (col = 1; col <= maxcol; col++)
  32. if (grid[row][col] == 1) cout << '*';
  33. else cout << ' ';
  34. cout << endl;
  35. }
  36. cout << endl;
  37. }
  38. void Life::update()
  39. /*Pre: The Life object contains a configuration
  40. Post: The Life object contains the next generation of configuration.*/
  41. {
  42. int row, col;
  43. int new_grid[maxrow + 2][maxcol + 2];
  44. for (row = 1; row <= maxrow; row++)
  45. for (col = 1; col <= maxcol; col++)
  46. switch(neighbor_count(row, col)) {
  47. case 2:
  48. new_grid[row][col] = grid[row][col]; // Status stays the same.
  49. break;
  50. case 3:
  51. new_grid[row][col] = 1; // Cell is alive.
  52. break;
  53. default:
  54. new_grid[row][col] = 0; // Cell is dead.
  55. }
  56. for (row = 1; row <= maxrow; row++)
  57. for (col = 1; col <= maxcol; col++)
  58. grid[row][col] = new_grid[row][col];
  59. }
  60. int Life::neighbor_count(int row, int col)
  61. /*Pre: The Life object contains a configuration, and the coordinates row and col
  62. define a cell inside its hedge.
  63. Post: The number of living neighbors of the specified cell is returned.*/
  64. {
  65. int i, j;
  66. int count = 0;
  67. for (i = row - 1; i <= row + 1; i++)
  68. for (j = col - 1; j <= col + 1; j++)
  69. count += grid[i][j]; // Increase the count if neighbor is alive.
  70. count -= grid[row][col]; // Reduce count, since cell is not its own neighbor.
  71. return count;
  72. }
  73. #endif

3、编写头文件utility.h


  
  1. #include <iostream>
  2. #include <istream>
  3. using namespace std;
  4. #include "life.h"
  5. void instructions();
  6. bool user_says_yes();
  7. #define DONE
  8. #include "utility.cpp"

4、编写程序文件utility.cpp


  
  1. #ifdef DONE
  2. void instructions()
  3. /*Pre: None.
  4. Post: Instructions for using the Life program have been printed.*/
  5. {
  6. cout << "Welcome to Conway's game of Life." << endl;
  7. cout << "This game uses a grid of size " << maxrow << " by " << maxcol << " in which " << endl;
  8. cout << "each cell can either be occupied by an organism or not." << endl;
  9. cout << "The occupied cells change from generation to generation" << endl;
  10. cout << "according to the number of neighboring cells which are alive." << endl;
  11. }
  12. bool user_says_yes()
  13. {
  14. int c;
  15. bool initial_response = true;
  16. do { // Loop until an appropriate input is received.
  17. if (initial_response)
  18. cout << " (y,n)? " << flush;
  19. else
  20. cout << "Respond with either y or n: " << flush;
  21. do { // Ignore white space.
  22. c = cin.get();
  23. } while (c == '\n' || c == ' ' || c == '\t');
  24. initial_response = false;
  25. } while (c != 'y' && c != 'Y' && c != 'n' && c != 'N');
  26. return(c == 'y' || c == 'Y');
  27. }
  28. #endif

5、编写主程序文件main.cpp


  
  1. #include "utility.h"
  2. int main() // Program to play Conway's game of Life
  3. /*Pre: The user supplies an initial configuration of living cells.
  4. Post: The program prints a sequence of pictures showing the changes in the
  5. configuration of living cells according to the rules for the game of Life.
  6. Uses: The class Life and its methods initialize(), print(), and update().
  7. The functions instructions(); user_says_yes().*/
  8. {
  9. Life configuration;
  10. instructions();
  11. configuration.initialize();
  12. cout << "count = " << configuration.neighbor_count(2, 3) << endl;
  13. configuration.print();
  14. cout << "Continue viewing new generations? " << endl;
  15. while (user_says_yes()) {
  16. configuration.update();
  17. configuration.print();
  18. cout << "Continue viewing new generations? " << endl;
  19. }
  20. return 0;
  21. }

6、运行程序,查看结果

三、尝试生命游戏其它初始布局

文章来源: howard2005.blog.csdn.net,作者:howard2005,版权归原作者所有,如需转载,请联系作者。

原文链接:howard2005.blog.csdn.net/article/details/79294248

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。