如何让Hunter 和RABBIT不能重合在一起?
main()函数:#include "Hunter.h"
#define MAP_X 39 //地图X坐标
#define MAP_Y 16 //地图Y坐标
#define RABBIT_NUM 3
//游戏地图
char g_Map[MAP_Y][MAP_X] = {
"* * * * * * * * * * * * * * * * * * *",
"* *",
"* *",
"* *",
"* *",
"* *",
"* *",
"* *",
"* *",
"* *",
"* *",
"* *",
"* *",
"* *",
"* *",
"* * * * * * * * * * * * * * * * * * *"
};
typedef struct _RABBIT
{
int rabX;
int rabY;
} RABBIT, * pRABBIT;
//刷新
void Refurbish()
{
system("cls");
}
void main()
{
srand((unsigned int)time(NULL));
CHunter hunter;
RABBIT rabbit[5];
for(int i = 0; i < RABBIT_NUM; i++)
{
rabbit[i].rabX = (rand() % (MAP_X - 4) + 1);
rabbit[i].rabY = (rand() % (MAP_Y - 2) + 1);
}
char inPut[255] = "";
bool active = false;
while(!active)
{
Refurbish();
//绘制地图
g_Map[hunter.PutY()][hunter.PutX()] = 2;
for(int i = 0; i < RABBIT_NUM; i++)
{
g_Map[rabbit[i].rabY][rabbit[i].rabX] = 4;
}
for(int i = 0; i < MAP_Y; i++)
{
for(int j = 0; j < MAP_X; j++)
{
cout<<g_Map[i][j];
}
cout<<endl;
}
cout<<"请按提示进行操作!"<<endl;
cout<<"W.向上走一步。 S.向下走一步。 A.向左走一步。 D.向右走一步。"<<endl;
cin>>inPut;
g_Map[hunter.PutY()][hunter.PutX()] = ' '; //清除脚印
if((inPut[0] == 'W' || inPut[0] == 'w') && hunter.PutY() != 1)
{
hunter.SetY(-1);
}
else if((inPut[0] == 'S'|| inPut[0] == 's') && hunter.PutY() != (MAP_Y - 2))
{
hunter.SetY(1);
}
else if((inPut[0] == 'A' || inPut[0] == 'a') && hunter.PutX() != 2)
{
hunter.SetX(-2);
}
else if((inPut[0] == 'D' || inPut[0] == 'd') && hunter.PutX() != (MAP_X - 4))
{
hunter.SetX(2);
}
}
}
Hunter头文件:
#pragma once
#include <iostream>
#include <time.h>
using namespace std;
class CHunter
{
private:
int m_bullet;
int m_quarryCount;
int m_posX;
int m_posY;
public:
CHunter();
~CHunter();
void ShowHunter();
void Hunt();
void SetX(int );
void SetY(int );
int PutX();
int PutY();
};
Hunter的CPP文件:
#include "Hunter.h"
CHunter::CHunter()
{
m_bullet = 10;
m_quarryCount = 0;
m_posX = 2;
m_posY = 1;
}
CHunter::~CHunter()
{
}
void CHunter::ShowHunter()
{
}
void CHunter::Hunt()
{
if(m_bullet > 0)
{
if((rand() % 10) < 7)
{
cout<<"击中兔子!"<<endl;
m_quarryCount++;
}
else
{
cout<<"没有击中!"<<endl;
}
m_bullet--;
}
else
{
cout<<"没有子弹,无法射击"<<endl;
}
}
void CHunter::SetX(int i)
{
m_posX += i;
}
void CHunter::SetY(int i)
{
m_posY += i;
}
int CHunter::PutX()
{
return m_posX;
}
int CHunter::PutY()
{
return m_posY;
}