// lanchiyang.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//
#include"Wolf.h"
#include<stdlib.h>
#include<time.h>
#include"Sheep.h"
#include"Animal.h"
#include"string.h"
#include <iostream>
#include<ctime>
using namespace std;
int Sheep::m_sumSheep = 0;//静态函数成员初始化
int Wolf::m_sumWolf = 0;
int main()
{
int i, u;
int w[20] = { 0 }, Y[20] = { 0 };
int health[20] = { 0 };
string nsex = "公", lsex = "母";
int a1 = 0, a2 = 0, a3, a4, a5 = 0;
int ageW[20] = { 0 }, ageY[20] = { 0 };
int ax[20] = { 0 }, ay[20] = { 0 };
//先建四个青青草原必有成员
srand((int)time(NULL));
for (i = 0; i < 20; i++)
{
ax[i] = rand() % 31;
ay[i] = rand() % 31;
health[i] = 1 + rand() % 5;
}
for (i = 0; i < 2; i++)
{
ageW[i] = 3 + rand() % 4;
ageY[i] = 3 + rand() % 4;
w[i] = 2;
Y[i] = 2;
}
ageY[2] = 3 + rand() % 4;
Y[2] = 2;
for (i = 2; i < 20; i++)
{
ageW[i] = 1;
}
for (i = 3; i < 20; i++)
{
ageY[i] = 1;
}
cout << "-------------------------------------------------------------" << endl;
cout << "当前青青草原的状态:" << endl;
Animal* animalY[10];
Animal* animalW[10];
animalY[0] = new Sheep(ax[2], ay[2], "喜羊羊", ageY[0], health[2], nsex, 2);
animalY[1] = new Sheep(ax[3], ay[3], "暖羊羊", ageY[1], health[3], lsex, 1);
animalY[2] = new Sheep(ax[4], ay[4], "懒羊羊", ageY[2], health[4], nsex, 2);
animalW[0] = new Wolf(ax[0], ay[0], "灰太狼", ageW[0], health[0], nsex, 4);
animalW[1] = new Wolf(ax[1], ay[1], "红太狼", ageW[1], health[1], lsex, 3);
for (i = 0; i < 2; i++)
{
animalW[i]->show();
animalY[i]->show();
}
animalY[2]->show();
//通过类名直接调用公有静态成员变量,获取对象个数
cout << "草原上狼总数为:" << Wolf::m_sumWolf << " " << "羊的总数为:" << Sheep::m_sumSheep << endl;
cout << "-------------------------------------------------------------" << endl;
cout << "是否开始草原生态活动?如果是请按:1,否则请按:0" << endl;
cin >> u;
int m1 = 2, m2 = 5, m3 = 3;
while (u == 1)
{
if (Wolf::m_sumWolf < 2&&Sheep::m_sumSheep<2)
{
cout << "狼、羊两族灭亡" << endl;
break;
}
else if (Wolf::m_sumWolf < 2)
{
cout << "狼族灭亡" << endl;
break;
}
else if (Sheep::m_sumSheep < 2)
{
cout << "羊族灭亡" << endl;
break;
}
else
{
int ur = 0;
for (int j = 0; j < m1; j++)
{
if (w[j] == 2 && animalW[j]->getsex() == "公")
{
a1 = 0, a3 = 0;
int e = 0;
while (a1 == 0 && a3 == 0)
{
if (Y[e] == 2)
{
a1 = animalW[j]->arrest(animalY[e]);
if (a1 == 1)
{
Y[e] = 1;
delete animalY[e];
}
}
e++;
if (e == m3)
{
break;
}
}
for (int k = 1; k < m1; k++)
{
if (w[k] == 2 && animalW[k]->getsex() == "母")
{
a2 = animalW[j]->eat(a1);
a3 = animalW[j]->breed(animalW[k], a2);
if (a3 != 0)
{
if (m1 % 2 == 0)
{
animalW[m1] = new Wolf(ax[m2], ay[m2], "小公狼", ageW[m1], health[m2], nsex, 4);
}
else
{
animalW[m1] = new Wolf(ax[m2], ay[m2], "小母狼", ageW[m1], health[m2], lsex, 3);
}
w[m1] = 2;
cout << animalW[k]->getname()<<"生下" << animalW[m1]->getname() << endl;
animalW[m1]->show();
m1++;
m2++;
ur = 1;
}
}
if (ur == 1)
{
break;
}
}
if (ur == 1)
{
break;
}
}
}
for (i = 0; i < m1; i++)
{
ageW[i] += 2;
}
}
if(Sheep::m_sumSheep>=1)
{
int ur = 0;
for (int j = 0; j < m3; j++)
{
a5 = 0;
if (Y[j] == 2 && animalY[j]->getsex() == "公")
{
for (int k = 1; k < m3; k++)
{
if (Y[k] == 2)
{
if (animalY[k]->getsex() == "母")
{
a4 = animalY[j]->arrest(animalY[k]);
a5 = animalY[j]->breed(animalY[k], a4);
}
}
if (a5 != 0)
{
int o = 0;
if (o < 2)
{
if ((m3 + 1) % 2 == 0)
{
animalY[m3] = new Sheep(ax[m2], ay[m2], "小公羊", ageY[m3], health[m2], nsex, 2);
o = 1;
}
else
{
animalY[m3] = new Sheep(ax[m2], ay[m2], "小母羊", ageY[m3], health[m2], lsex, 1);
o = 2;
}
}
Y[m3] = 2;
cout << animalY[k]->getname()<<"生下" << animalY[m3]->getname() << endl;
animalY[m3]->show();
m3++;
m2++;
ur = 1;
}
if (ur == 1)
{
break;
}
}
}
if (ur == 1)
{
break;
}
}
}
for (i = 0; i < m3; i++)
{
ageY[i] += 2;
}
int s1, s2;
for (i = 0; i < m1; i++)
{
if (w[i] == 2)
{
s1 = animalW[i]->death(ageW[i]);
if (s1 == 0)
{
w[i] = 1;
delete animalW[i];
}
}
}
for (i = 0; i < m3; i++)
{
if (Y[i] == 2)
{
s2 = animalY[i]->death(ageY[i]);
i
- 1
- 2
- 3
- 4
- 5
- 6
前往页