BI報表的設計,提升數(shù)據(jù)分析與決策效率">BI報表的設計,提升數(shù)據(jù)分析與決策效率
686
2025-04-01
目錄
一、生命游戲
1、生命游戲概述
2、生命演化規(guī)則:B3/S23
二、生命游戲C++實現(xiàn)
1、編寫頭文件life.h
2、編寫C++程序life.cpp
3、編寫頭文件utility.h
4、編寫程序文件utility.cpp
5、編寫主程序文件main.cpp
6、運行程序,查看結(jié)果
三、嘗試生命游戲其它初始布局
一、生命游戲
1、生命游戲概述
在研究元胞自動機理論過程中,Conway發(fā)明生命游戲(Game of Life、GoL),在上個世紀七十年代風靡一時。
這是0人游戲,即按照初始的設置,游戲自動演化。在類似圍棋的棋盤中,每一個格子可以是空格或者存在一個生命/細胞/Cell;每一個格子有8個相鄰的格子(正上方、正下方、右側(cè)、左側(cè)、左上方、右上方、左下方以及右下方),相鄰的格子中存活的生命數(shù)量稱為其鄰居(neighbor)數(shù)。在世代交替時,所有的格子根據(jù)其鄰居數(shù),誕生新生命、Cell保持存活或者Cell死亡。
2、生命演化規(guī)則:B3/S23
一個生命如果恰好有2個或3個鄰居,它會存活到下一個世代;否則,會因為孤獨或擁擠而死亡。
一個空格,如果恰好有3個鄰居,則誕生一個新生命。
二、生命游戲C++實現(xiàn)
在Dev C++里創(chuàng)建項目TheGameOfLife,創(chuàng)建頭文件與C++源文件,如下圖所示:
1、編寫頭文件life.h
const int maxrow = 20, maxcol = 60; // grid dimensions
class Life {
public:
void initialize();
void print();
void update();
private:
int grid[maxrow + 2][maxcol + 2]; // allows for two extra rows and columns
int neighbor_count(int row, int col);
};
#define DONE
#include "life.cpp"
2、編寫C++程序life.cpp
#ifdef DONE
void Life::initialize()
/*Pre: None
Post: The Life object contains a configuration specified by the user.*/
{
int row, col;
for (row = 0; row <= maxrow + 1; row++)
for (col = 0; col <= maxcol + 1; col++)
grid[row][col] = 0;
cout << "List the coordinates for living cells." << endl;
cout << "Terminate the list with the special pair -1 -1" << endl;
cin >> row >> col;
while (row != -1 || col != -1) {
if (row >=1 && row <= maxrow)
if (col >=1 && col <= maxcol)
grid[row][col] = 1;
else
cout << "Column " << col << " is out of range." << endl;
else
cout << "Row " << row << " is out of range." << endl;
cin >> row >> col;
}
}
void Life::print()
/*Pre: The Life object contains a configuration.
Post: The configuration is written for the user.*/
{
int row, col;
cout << "\nThe current Life configuration is:" << endl;
for (row = 1; row <= maxrow; row++) {
for (col = 1; col <= maxcol; col++)
if (grid[row][col] == 1) cout << '*';
else cout << ' ';
cout << endl;
}
cout << endl;
}
void Life::update()
/*Pre: The Life object contains a configuration
Post: The Life object contains the next generation of configuration.*/
{
int row, col;
int new_grid[maxrow + 2][maxcol + 2];
for (row = 1; row <= maxrow; row++)
for (col = 1; col <= maxcol; col++)
switch(neighbor_count(row, col)) {
case 2:
new_grid[row][col] = grid[row][col]; // Status stays the same.
break;
case 3:
new_grid[row][col] = 1; // Cell is alive.
break;
default:
new_grid[row][col] = 0; // Cell is dead.
}
for (row = 1; row <= maxrow; row++)
for (col = 1; col <= maxcol; col++)
grid[row][col] = new_grid[row][col];
}
int Life::neighbor_count(int row, int col)
/*Pre: The Life object contains a configuration, and the coordinates row and col
define a cell inside its hedge.
Post: The number of living neighbors of the specified cell is returned.*/
{
int i, j;
int count = 0;
for (i = row - 1; i <= row + 1; i++)
for (j = col - 1; j <= col + 1; j++)
count += grid[i][j]; // Increase the count if neighbor is alive.
count -= grid[row][col]; // Reduce count, since cell is not its own neighbor.
return count;
}
#endif
3、編寫頭文件utility.h
#include
#include
using namespace std;
#include "life.h"
void instructions();
bool user_says_yes();
#define DONE
#include "utility.cpp"
4、編寫程序文件utility.cpp
#ifdef DONE
void instructions()
/*Pre: None.
Post: Instructions for using the Life program have been printed.*/
{
cout << "Welcome to Conway's game of Life." << endl;
cout << "This game uses a grid of size " << maxrow << " by " << maxcol << " in which " << endl;
cout << "each cell can either be occupied by an organism or not." << endl;
cout << "The occupied cells change from generation to generation" << endl;
cout << "according to the number of neighboring cells which are alive." << endl;
}
bool user_says_yes()
{
int c;
bool initial_response = true;
do { // Loop until an appropriate input is received.
if (initial_response)
cout << " (y,n)? " << flush;
else
cout << "Respond with either y or n: " << flush;
do { // Ignore white space.
c = cin.get();
} while (c == '\n' || c == ' ' || c == '\t');
initial_response = false;
} while (c != 'y' && c != 'Y' && c != 'n' && c != 'N');
return(c == 'y' || c == 'Y');
}
#endif
5、編寫主程序文件main.cpp
#include "utility.h"
int main() // Program to play Conway's game of Life
/*Pre: The user supplies an initial configuration of living cells.
Post: The program prints a sequence of pictures showing the changes in the
configuration of living cells according to the rules for the game of Life.
Uses: The class Life and its methods initialize(), print(), and update().
The functions instructions(); user_says_yes().*/
{
Life configuration;
instructions();
configuration.initialize();
cout << "count = " << configuration.neighbor_count(2, 3) << endl;
configuration.print();
cout << "Continue viewing new generations? " << endl;
while (user_says_yes()) {
configuration.update();
configuration.print();
cout << "Continue viewing new generations? " << endl;
}
return 0;
}
6、運行程序,查看結(jié)果
三、嘗試生命游戲其它初始布局
5G游戲 C++
版權(quán)聲明:本文內(nèi)容由網(wǎng)絡用戶投稿,版權(quán)歸原作者所有,本站不擁有其著作權(quán),亦不承擔相應法律責任。如果您發(fā)現(xiàn)本站中有涉嫌抄襲或描述失實的內(nèi)容,請聯(lián)系我們jiasou666@gmail.com 處理,核實后本網(wǎng)站將在24小時內(nèi)刪除侵權(quán)內(nèi)容。
版權(quán)聲明:本文內(nèi)容由網(wǎng)絡用戶投稿,版權(quán)歸原作者所有,本站不擁有其著作權(quán),亦不承擔相應法律責任。如果您發(fā)現(xiàn)本站中有涉嫌抄襲或描述失實的內(nèi)容,請聯(lián)系我們jiasou666@gmail.com 處理,核實后本網(wǎng)站將在24小時內(nèi)刪除侵權(quán)內(nèi)容。