#include <iostream>
#include <ctime>
#include <conio.h>
using namespace std;
class data
{
private:
int x;
int y;
public:
data(int i, int ff) :x(i), y(ff){ ; }
virtual~data(){ cout << "data................\n"; }
virtual int getInt(){ return x; }
virtual int getY(){ return y; }
virtual void setint(int i){ x = i; }
virtual void setY(int ff){ y = ff; }
virtual void print() = 0;
};
class she_s :public data
{
public:
she_s(int x, int ff) :data(x,ff){}
~she_s(){ cout << "she_s...........\n"; }
void print()
{
cout << "x==" << getInt() << "y==" << getY();
}
};
class shiru:public data
{
public:
shiru(int x, int y) :data(x,y){}
~shiru(){ cout << "shiru...............\n"; }
void print()
{
cout << "x==" << this->getInt() << "y ==" <<getY();;
}
};
class node
{
public:
node(data *pdata) :Data(pdata), isNode(0), L(0), conut(0){}
~node(){
cout << "node............\n";
delete Data;
Data = 0;
if (conut)
{
delete conut;
conut = 0;
}
}
node* getNode(){ return isNode; }
data *getData(){ return Data; }
node *getL(){ return L; }
void initConut()
{
if (!conut)
conut = new int(0);
return;
}
void SetL(node *p){ L = p; }
void Setdata(data *pdata){ Data = pdata; }
void SetNode(node *p){ isNode = p; }
int getConut(){ return *conut; }
void SetConut(int i){ *conut = i; }
void ADDConut(){ *conut+=1; }
void SunConut(){ *conut-=1; }
private:
data *Data;
node *isNode;
node *L;
int *conut;
};
class l
{
public:
l() :head(0),end(0),shi(0){}
void init_she()
{
for (int i = 0; i < 3; i++)
{
data *p = new she_s(11,i+1 );
if (!head)
{
head = new node(p);
head->SetL(0);
head->SetNode(0);
end = head;
head->initConut();
head->ADDConut();
}
else
{
node *pnew = new node(p);
end->SetNode(pnew);
pnew->SetL(end);
end = pnew;
head->ADDConut();
}
}
}
node *getShiTou(){ return head; }
node *getWei(){ return end; }
node * getshi(){ return shi; }
void Setweiba(node *p){ end = p; }
void init_shiwu()
{
if (!shi)
{
data *d = new shiru(7, 9);
shi = new node(d);
shi->initConut();
shi->ADDConut();
return;
}
}
~l()
{
cout << "L........." << endl;
if (head)
{
while (head->getNode() != NULL)
{
head = head->getNode();
delete (head->getL());
head->SetL(0);
}
delete head;
head = end = NULL;
}
if (shi)
{
delete shi;
shi = NULL;
}
}
private:
node *head;
node *end;
node *shi;
};
class re
{
public:
re() :map(0), sp(300), ch(0){ quit = false; }
void initmap()
{
map = (char **)calloc(21, sizeof(char*));
for (int i = 0; i < 21; i++)
{
map[i] = (char*)calloc(21, sizeof(char));
}
if (!map)
exit(0);
for (int i = 0; i < 20; i++)
{
map[0][i] = '1';
}
for (int i = 0; i < 20; i++)
{
map[i][0] = '1';
}
for (int i = 0; i < 20; i++)
{
map[i][19] = '1';
}
for (int i = 0; i < 20; i++)
{
map[19][i] = '1';
}
}
int getsp(){ return sp; }
void Setsp(int i){ sp = i; }
void show_map()
{
cout << "\t C++太吃蛇大战!!" << endl;
cout << "\n\n";
for (int i = 0; i < 20; i++)
{
for (int j = 0; j < 20; j++)
{
if (map[i][j] == '1')
cout << "■";
if (map[i][j] == '2')
cout << "○";
if (map[i][j] == '3')
cout << "●";
if (map[i][j] == '6')
cout << "◎";
else if (!map[i][j])
cout << " ";
}
switch (i)
{
case 3:
cout << "\t蛇等级为:" << p.getShiTou()->getConut()/5;
break;
case 5:
cout << "\t蛇的长度:" << p.getShiTou()->getConut();
break;
case 7:
cout << "\t蛇的速度:" << getsp();
break;
case 9:
cout << "\t得分:" <<( p.getShiTou()->getConut() -3)* 10;
break;
case 11:
cout << "\t食物的坐标:X" << p.getshi()->getData()->getInt() << " Y的坐标:"<<p.getshi()->getData()->getY();
break;
case 17:
cout << "\t游戏结束请按ESL";
break;
case 18:
cout << "\t长按加速";
break;
}
cout << endl;
}
}
void init_shiwu()
{
srand(time(0));
while (1)
{
int x = rand() % 18 + 1;
int y = rand() % 18 + 1;
if (map[x][y] != '1')
{
node *temp = p.getWei();
while (temp)
{
if (x == temp->getData()->getInt() && y == temp->getData()->getY())
break;
temp = temp->getL();
}
if (temp)
continue;
else
{
p.getshi()->getData()->setint(x);
p.getshi()->getData()->setY(y);
map[p.getshi()->getData()->getInt()][p.getshi()->getData()->getY()] = '3';
return;
}
}
}
}
void read_map()
{
node *ps =p.getWei();
int numer = p.getShiTou()->getConut();
int conut=0;
while (ps)
{
if (!conut)
{
++conut;
map[ps->getData()->getInt()][ps->getData()->getY()] = '6';
}
else if (conut == numer-1)
{
map[ps->getData()->getInt()][ps->getData()->getY()] = '2';
conut++;
}
else
{
++conut;
map[ps->getData()->getInt()][ps->getData()->getY()] = '3';
}
ps = ps->getL();
}
map[p.getshi()->getData()->getInt()][p.getshi()->getData()->getY()] = '3';
}
void Sethead(const int i)
{
node *ps =p.getShiTou() ;
if (!ps)
return;
map[ps->getData()->getInt()][ps->getData()->getY()] = '\0';
ps->getData()->setint(ps->getNode()->getData()->getInt());
ps->getData()->setY(ps->getNode()->getData()->getY());
map[ps->getData()->getInt()][ps->getData()->getY()] = '2';
int x = p.getWei()->getData()->getInt();
int y = p.getWei()->getData()->getY();
switch (i)
{
case 1:
case 2:
while (1)
{
ps = ps->getNode();
if (ps->getNode() == NULL)
break;
ps->getData()->setint(ps->getNode()->getData()->getInt());
ps->getData()->setY(ps->getNode()->getData()->getY());
map[ps->getData()->getInt()][ps->getData()->getY()] = '3';
}
if (i == 1)
{
ps->getData()->setint(x-1);
ps->getData()->setY(y);
map[ps->getData()->getInt()][ps->getData()->getY()] = '6';
}
else
{
ps->getData()->setint(x + 1);
ps->getData()->setY(y);
map[ps->getData()->getInt()][ps->getData()->getY()] = '6';
}
break;
case 3:
case 4:
while(1)
{
ps = ps->getNode();
if (ps->getNode() == NULL)
break;
ps->getData()->setint(ps->getNode()->getData()->getInt());
ps->getData()->setY(ps->getNode()->getData()->getY());
map[ps->getData()->getInt()][ps->getData()->getY()] = '3';
}
if (i == 3)
{
ps->getData()->setint(x);
ps->getData()->setY(y-1);
map[ps->getData()->getInt()][ps->getData()->getY()] = '6';
}
else
{
ps->getData()->setint(x );
ps->getData()->setY(y+1);
map[ps->getData()->getInt()][ps->getData()->getY()] = '6';
}
break;
}
}
void chishiwu()
{
node*ps = p.getWei();
if (ps->getData()->getInt() == p.getshi()->getData()->getInt() && ps->getData()->getY() == p.getshi()->getData()->getY())
{
p.getShiTou()->ADDConut();
init_shiwu();//布置食物
addshe_s();
if ((p.getShiTou()->getConut() - 3) % 5 == 0)
{
Setsp(sp - 50);
}
}
}
void addshe_s()
{
int x = p.getWei()->getData()->getInt();
int y = p.getWei()->getData()->getY();
data *pp = new she_s(x, y);
node*ps = new node(pp);
node *temp = p.getWei();
temp->SetNode(ps);
ps->SetL(temp);
p.Setweiba(ps);
ps = ps->getL();
while (ps!=p.getShiTou())
{
ps->getData()->setint(ps->getL()->getData()->getInt());
ps->getData()->setY(ps->getL()->getData()->getY());
ps = ps->getL();
}
}
bool simang()
{
int x = p.getWei()->getData()->getInt();
int y = p.getWei()->getData()->getY();
if (x==0 || x == 19 || y == 0 || y == 19)
return true;
node *ps = p.getWei()->getL();
node *pa= p.getShiTou();
while (ps != pa)
{
if (ps->getData()->getInt() == x&&ps->getData()->getY() == y)
return true;
if (pa->getData()->getInt() == x&&pa->getData()->getY() == y)
return true;
ps = ps->getL();
if (ps == pa)
break;
pa = pa->getNode();
if (ps = ps)
break;
}
return false;
}
void Run();
void Sleep(int num)
{
clock_t st, en;
st = clock();
while (1)
{
en = clock();
if ((double)(en - st) >= num)
{
break;
}
}
}
~re()
{
for (int i = 0; i < 21; i++)
{
delete []map[i];
}
delete []map;
cout << "re......." << endl;
}
private:
char **map; //■●○
l p;
int sp;
bool quit;
unsigned char ch;
};
void re::Run()
{
system("color 02");
initmap();//树池地图
p.init_she();//舒适化蛇
p.init_shiwu();//刷护士食物
read_map();//写入地图
show_map();//显示地图
while (1)
{
if (_kbhit())
{
ch = _getch();
if (_kbhit())
{
quit = false;
Sleep(20);
}
}
switch (ch)
{
case 'H'://上H
Sethead(1);
break;
case 'P'://下P
Sethead(2);
break;
case 'K'://左K
Sethead(3);
break;
case 'M'://右M
Sethead(4);
break;
}
chishiwu();
system("cls");
show_map();//显示
if (simang() || ch == 27 )
{
cout << "游戏结束\n";
break;
}
if (quit)
{
Sleep(getsp());
}
quit = true;
}
}
int main111()
{
while (1)
{
re *r=new re;
r->Run();
delete r;
r = NULL;
fflush(stdin);
system("pause");
}
system("pause");
return 0;
}
学C++ 写面向对象贪吃蛇(作风一贯::懒)
最新推荐文章于 2023-03-09 00:39:05 发布