?? main.cpp
字號:
/*************************************************************
* 生態系統模擬,草原、羊、狼
* Copyright@Arthur
* 2008-12-24
**************************************************************/
#include <stdio.h>
#include <iostream>
#include <vector>
using namespace std;
#define MaxNM 500
#define MaxR 5000 //羊的最大數量
#define MaxW 100 //狼的最大數量
//羊的結構
typedef struct SRam
{
int x,y; //位置坐標
double hunger;
int childDays;
bool female;
bool death; //是否死亡
}SRam;
//狼的結構
typedef struct SWolf
{
int x,y; //位置坐標
double hunger;
int childDays;
bool female;
bool death; //是否死亡
}SWolf;
//草原的單元結構
typedef struct SGrassUnit
{
int ramIndex[10],wolfIndex[10]; //羊和狼的序號
double ramNum,wolfNum; //此處狼羊的數量,可以為小數
double belongTo;
double ate;
double grass; //草的狀態 0-1
}SGrassUnit;
//模擬系統
class CEcosystem
{
public:
CEcosystem();
~CEcosystem();
int Run();
protected:
int Init();
int Simulating();
int Release();
void Usage();
void PrintInfor(int day = 0);
protected:
int m_days; //模擬的總天數
int m_grassM,m_grassN; //草原的大小
double m_grassRate; //草的增長率
//模擬生物數據
SGrassUnit **m_grass;
SRam *m_rams;
SWolf *m_wolves;
int m_nRam; //羊的數量
int m_nWolf; //狼的數量
bool m_ready;
};
int main()
{
CEcosystem ecosystemS;
ecosystemS.Run();
return 0;
}
CEcosystem::CEcosystem()
{
m_grass = NULL;
m_rams = NULL;
m_wolves = NULL;
}
CEcosystem::~CEcosystem()
{
}
int CEcosystem::Init()
{
char y;
m_ready = false;
//打印應用
Usage();
cout<<"開始模擬?y/n?\n";
cin>>y;
if (y != 'y')
{
//退出系統
cout<<"按任意鍵退出模擬程序……";
getchar();
getchar();
return 1;
}
//讀入數據
cout<<"輸入草原的大小(10..500) n m\n";
cin>>m_grassN>>m_grassM;
if (m_grassN < 9 || m_grassN > 501 && m_grassM < 9 || m_grassM > 500)
{
cout<<"錯誤輸入!";
return -1;
}
cout<<"輸入草的增長率(0..1) rate\n";
cin>>m_grassRate;
if (m_grassRate <= 0 || m_grassRate > 1 )
{
cout<<"錯誤輸入!";
return -1;
}
cout<<"輸入羊(0..1000)狼(0..50)的初始量 n m\n";
cin>>m_nRam>>m_nWolf;
if (m_nWolf < 0 || m_nWolf > 50 || m_nRam < 0 || m_nRam > 1000)
{
cout<<"錯誤輸入!";
return -1;
}
cout<<"輸入模擬的天數(1..1000) rate\n";
cin>>m_days;
if (m_days <= 0 || m_days > 1000 )
{
cout<<"錯誤輸入!";
return -1;
}
//數據的初始化
m_grass = new SGrassUnit* [m_grassN];
for (int i = 0 ; i < m_grassN ; i++)
{
m_grass[i] = new SGrassUnit[m_grassM];
}
for (int i = 0 ; i < m_grassN ; i++)
{
for (int j = 0 ; j < m_grassM ; j++)
{
m_grass[i][j].ate = 0;
m_grass[i][j].belongTo = 1;
m_grass[i][j].grass = 0;
m_grass[i][j].ramNum = 0;
m_grass[i][j].wolfNum = 0;
for (int k = 0 ; k < 10 ; k++)
{
m_grass[i][j].ramIndex[k] = m_grass[i][j].wolfIndex[k] = -1;
}
}
}
m_rams = new SRam[MaxR];
m_wolves = new SWolf[MaxW];
for (int i = 0 ; i < MaxR ; i++)
{
m_rams[i].childDays = 101; //成年羊
m_rams[i].death = true;
m_rams[i].female = i % 2 == 1;
m_rams[i].hunger = 0;
m_rams[i].x = 0;
m_rams[i].y = 0;
}
for (int i = 0 ; i < MaxW ; i++)
{
m_wolves[i].childDays = 0; //表示不帶孩子的狼
m_wolves[i].death = true;
m_wolves[i].female = i % 2 == 1;
m_wolves[i].hunger = 0;
m_wolves[i].x = 0;
m_wolves[i].y = 0;
}
for (int i = 0 ; i < m_nRam ; i++)
{
m_rams[i].death = false;
m_rams[i].x = (int)((double)rand() / ((double)RAND_MAX + 1) * m_grassN);
m_rams[i].y = (int)((double)rand() / ((double)RAND_MAX + 1) * m_grassM);
m_grass[m_rams[i].x][m_rams[i].y].ramNum++;
for (int j = 0 ; j < 10 ; j++)
{
if (m_grass[m_rams[i].x][m_rams[i].y].ramIndex[j] < 0)
{
m_grass[m_rams[i].x][m_rams[i].y].ramIndex[j] = i;
break;
}
}
}
for (int i = 0 ; i < m_nWolf ; i++)
{
m_wolves[i].death = false;
m_wolves[i].x = (int)((double)rand() / ((double)RAND_MAX + 1) * m_grassN);
m_wolves[i].y = (int)((double)rand() / ((double)RAND_MAX + 1) * m_grassM);
m_grass[m_wolves[i].x][m_wolves[i].y].wolfNum++;
for (int j = 0 ; j < 10 ; j++)
{
if (m_grass[m_wolves[i].x][m_wolves[i].y].wolfIndex[j] < 0)
{
m_grass[m_wolves[i].x][m_wolves[i].y].wolfIndex[j] = i;
break;
}
}
}
m_ready = true;
return 0;
}
int CEcosystem::Release()
{
if (m_grass)
{
for (int i = 0 ; i < m_grassN ; i++)
delete [](m_grass[i]);
delete []m_grass;
}
if (m_rams)
delete []m_rams;
if (m_wolves)
delete []m_wolves;
return 0;
}
int CEcosystem::Run()
{
int flag ;
while (1)
{
//初始化數據
flag = Init();
if (flag == -1)
{
continue;
}else if (flag == 1)
{
break;
}
//模擬過程
if (Simulating() == -1)
{
cout<<"模擬過程中錯誤!\n";
getchar();
getchar();
continue;
}
//模擬結束打印最后結果,并釋放空間
PrintInfor();
Release();
}
return 0;
}
int CEcosystem::Simulating()
{
int currDay = 0;
int i,j,x,y,index;
//double food;
//int count;
if (!m_ready)
{
return 1;
}
while (currDay < m_days)
{
//每天增加
currDay++;
//每隔10天輸出當前狀態
if (currDay % 10 == 0)
PrintInfor(currDay);
//草生長
for (i = 0 ; i < m_grassN ; i++)
{
for (j = 0 ; j < m_grassM ; j++)
{
m_grass[i][j].grass -= m_grass[i][j].ate;
m_grass[i][j].ate = 0;
if (m_grass[i][j].grass < 1)
{
m_grass[i][j].grass += m_grassRate;
}
}
}
//羊捕食
for (index = 0 ; index < MaxR ; index++)
{
if (!m_rams[index].death )
{
//沒有死亡的羊
if (m_rams[index].childDays <= 100)
{
m_rams[index].childDays++;
}
x = m_rams[index].x;
y = m_rams[index].y;
m_rams[index].hunger += 10;
for (int i = x - 5 ; i < x + 5 ; i++)
{
for (int j = y - 5 ; j < y + 5 ; j++)
{
if (i >= 0 && j >= 0 && i < m_grassN && j < m_grassM )
{
m_rams[index].hunger -= m_grass[i][j].grass/m_grass[i][j].belongTo;
m_grass[i][j].ate = m_grass[i][j].grass/m_grass[i][j].belongTo;
if (m_rams[index].hunger < 0 )
{
break;
}
}
}
if (m_rams[index].hunger < 0 )
{
break;
}
}
//餓死死亡
if (m_rams[index].hunger > 10 * 4)
{
m_rams[index].death = true;
//對草原的信息進行更新
m_grass[m_rams[index].x][m_rams[index].y].ramNum--;
for (j = 0 ; j < 10 ; j++)
{
if (m_grass[x][x].ramIndex[j] == index)
{
m_grass[x][y].ramIndex[j] = -1;
break;
}
}
}
}
}
//狼捕食
for (index = 0 ; index < MaxW ; index++)
{
if (!m_wolves[index].death)
{
if (m_wolves[index].childDays > 0 && m_wolves[index].childDays < 100)
{
m_wolves[index].childDays++;
}
x = m_wolves[index].x;
y = m_wolves[index].y;
if (m_wolves[index].hunger < 0)
{
//五天不用捕食
m_wolves[index].hunger++;
continue;
}
m_wolves[index].hunger += 0.5;
if (m_wolves[index].childDays > 0)
{
m_wolves[index].hunger += 0.5;
}
for (int i = x - 20 ; i < x + 20 ; i++)
{
if (i < 0 || i > m_grassN )
continue;
for (int j = y - 20 ; j < y + 20 ; j++)
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -