#include #include #include #include using namespace std; short Map[10][10]={ 1,1,1,1,1,1,1,1,1,1, 0,0,0,0,0,0,0,1,0,1, 1,0,1,1,1,1,0,1,0,1, 1,0,1,0,1,1,0,1,0,1, 1,0,0,0,0,0,0,1,0,1, 1,0,1,1,1,1,0,1,0,1, 1,0,0,0,0,1,0,0,0,1, 1,0,1,1,1,0,1,1,0,1, 1,0,0,0,0,0,1,0,0,0, 1,1,1,1,1,1,1,1,1,1 }; void gotoxy(int x,int y) { COORD CursorPosition; CursorPosition.X=x; CursorPosition.Y=y; SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),CursorPosition); } class Sniper { protected: short GX,GY; HANDLE hMutex; public : void Display(); Sniper(); void GetGun(); short Exception(short,short); void Createmutex(); void Releasemutex(); }; class Major:Sniper{ short MX, MY; short Mdirect; bool KeyLock; short SightSize; unsigned MID; unsigned long MThread; public: void InputKey(); void Motion(); Major(); short GetMX(); short GetMY(); void Sight(); static unsigned __stdcall MoveM(void*); unsigned long GetMThread(); void StartThread(); void WaitThread(); }; class Enemy:Sniper{ short EX,EY; short PushX[101],PushY[101]; short Pindex; short LookAt[4]; short LookCnt; short PathCount; short Counting; short Edirect; short SightSize; unsigned EID; unsigned long EThread; public : Enemy(); void MoveEnemy(); void Push(); void Pop(); void InitLook(); void ClearMap(); short GetEX(); short GetEY(); void Sight(); static unsigned __stdcall MoveE(void*); unsigned long GetEThread(); void StartThread(); void WaitThread(); }; Enemy::Enemy() { SightSize=1; PathCount=0; Counting=1; Edirect=0; for(int i=0;i<10;i++) for(int j=0;j<10;j++) if(Map[i][j]==0) PathCount++; EX=8;EY=9; Pindex=0; Push(); InitLook(); } Major::Major() { SightSize=1; MX=1; MY=0; KeyLock=false; Mdirect=0; } Sniper S; Enemy E; Major M; Sniper::Sniper() { GetGun(); } unsigned long Major::GetMThread() { return MThread; } unsigned long Enemy::GetEThread() { return EThread; } void Sniper::Createmutex() { hMutex=CreateMutex(NULL,FALSE,NULL); } void Sniper::Releasemutex() { ReleaseMutex(hMutex); } void Enemy::StartThread() { EThread=_beginthreadex(NULL,0,MoveE, (void*)this ,0,&EID); } void Major::StartThread() { MThread=_beginthreadex(NULL,0,MoveM, (void*)this ,0,&MID); } void Enemy::WaitThread() { WaitForSingleObject((HANDLE)EThread,INFINITE); } void Major::WaitThread() { WaitForSingleObject((HANDLE)MThread,INFINITE); } void Sniper::GetGun() { srand(time(NULL)); do { GX=rand()%10; GY=rand()%10; if(Map[GX][GY]==0 && E.GetEX()!=GX && M.GetMX()!=GX && E.GetEY()!=GY && M.GetMY()!=GY) break; }while(1); } short Enemy:: GetEX() { return EX; } short Enemy::GetEY() { return EY; } short Major::GetMX() { return MX; } short Major::GetMY() { return MY; } void Sniper::Display() { gotoxy(0,0); for(int i=0;i<10;i++) { for(int j=0;j<10;j++) { if(Map[i][j]==0) cout<<"¡à"; else if(Map[i][j]==-1) cout<<"¡Ù"; else cout<<"¡á"; } printf("\n"); } gotoxy(GY*2,GX); printf("¢Ò"); gotoxy(M.GetMY()*2,M.GetMX()); printf("¢ß"); gotoxy(E.GetEY()*2,E.GetEX()); printf("¢ç"); //gotoxy(20,9); //printf("ss %d %d %d %d %d %d \n",MX,MY, EX,EY, GX,GY); } void Enemy::Sight() { switch (Edirect) { case 0: for(int i=1;i<=SightSize;i++) if(Exception(EX-i,EY)!=-1) { if(EX-i==GX && EY==GY)//½Ã¾ß¿¡ ÃÑÀÌ ÀÖ´Ù¸é { } } break; case 1: for(int i=1;i<=SightSize;i++) if(Exception(EX,EY-i)!=-1) if(EX==GX &&EY-i==GY) { } break; case 2: for(int i=1;i<=SightSize;i++) if(Exception(EX,EY+i)!=-1) if(EX==GX &&EY+i==GY) { EY++; Display(); } break; case 3: for(int i=1;i<=SightSize;i++) if(Exception(EX-i,EY)!=-1) if(EX+i==GX &&EY==GY) { EX++; Display(); } break; default: break; } if(EX==GX && EY==GY) { gotoxy(20,10); cout<<"ÀûÀÌ ÃÑÀ» ã¾Ò½À´Ï´Ù."; ClearMap(); } } void Major::Sight() { switch (Mdirect) { case 0: break; case 1: break; case 2: break; case 3: break; default: break; } } void Major::InputKey() { if(GetAsyncKeyState(VK_UP) && Exception(MX-1,MY)>=0)//À§ÂÊ Mdirect=0,MX--,KeyLock=TRUE; else if(GetAsyncKeyState(VK_LEFT) && Exception(MX,MY-1)>=0)//¿ÞÂÊ Mdirect=1,MY--,KeyLock=TRUE; else if(GetAsyncKeyState(VK_RIGHT) && Exception(MX,MY+1)>=0)//¿À¸¥ÂÊ Mdirect=2,MY++,KeyLock=TRUE; else if(GetAsyncKeyState(VK_DOWN) && Exception(MX+1,MY)>=0)//¾Æ·¡ Mdirect=3,MX++,KeyLock=TRUE; else if(GetAsyncKeyState(VK_SPACE))//½ºÆäÀ̽º¸¦ ´­·¶À» °æ¿ì ÀûÀÌ º¸ÀδÙ. { gotoxy(E.GetEY()*2,E.GetEX()); printf("¢ç"); } } void Major::Motion() { do { WaitForSingleObject(hMutex,INFINITE); KeyLock=false; InputKey(); Sleep(300); if(KeyLock==TRUE) { Display(); system("cls"); } ReleaseMutex(hMutex); }while(1); } void Enemy::ClearMap() { for(int i=0;i<10;i++) for(int j=0;j<10;j++) if(Map[i][j]<0) Map[i][j]=0; } short Sniper::Exception(short a,short b) { if(a>=10 || a<0 || b>=10 || b<0) return -1; else if(Map[a][b]==1) return -1; else if(Map[a][b]==-1) return 0; else return 1; } void Enemy::InitLook() { LookCnt=0; for(int i=0;i<4;i++) LookAt[i]=0; } void Enemy::MoveEnemy() { int R; srand(time(NULL)); do { WaitForSingleObject(hMutex,INFINITE); R=rand()%4; switch(R) { case 0: //À§ÂÊ Edirect=0; if(Exception(EX-1,EY)==1) { EX-=1; Push(); InitLook(); Counting++; Sleep(300); } else if(LookAt[0]==0) LookAt[0]=1,LookCnt++; break; case 1://¿ÞÂÊ Edirect=1; if(Exception(EX,EY-1)==1) { EY-=1; Push(); InitLook(); Counting++; Sleep(300); } else if(LookAt[1]==0) LookAt[1]=1,LookCnt++; break; case 2://¿À¸¥Á· Edirect=2; if(Exception(EX,EY+1)==1) { EY+=1; Push(); InitLook(); Counting++; Sleep(300); } else if(LookAt[2]==0) LookAt[2]=1,LookCnt++; break; case 3://¾Æ·¡ Edirect=3; if(Exception(EX+1,EY)==1) { EX+=1; Push(); InitLook(); Counting++; Sleep(300); } else if(LookAt[3]==0) LookAt[3]=1,LookCnt++; break; } if(LookCnt==4) { Pop(); InitLook(); } gotoxy(20,10); printf("%d %d\n",Counting,PathCount); if(Counting==PathCount) { ClearMap(); Counting=0; } Display(); ReleaseMutex(hMutex); }while(1); } void Enemy::Push() { PushX[Pindex]=EX; PushY[Pindex++]=EY; Map[EX][EY]=-1; } void Enemy::Pop() { do { Pindex--; EX=PushX[Pindex]; EY=PushY[Pindex]; Display(); Sleep(300); if(Exception(PushX[Pindex]-1,PushY[Pindex])==1) { EX=PushX[Pindex]-1; EY=PushY[Pindex++]; Counting++; Push(); Display(); Sleep(300); break; } if(Exception(PushX[Pindex],PushY[Pindex]-1)==1) { EX=PushX[Pindex]; EY=PushY[Pindex++]-1; Counting++; Push(); Display(); Sleep(300); break; } if(Exception(PushX[Pindex],PushY[Pindex]+1)==1) { EX=PushX[Pindex]; EY=PushY[Pindex++]+1; Counting++; Push(); Display(); Sleep(300); break; } if(Exception(PushX[Pindex]+1,PushY[Pindex])==1) { EX=PushX[Pindex]+1; EY=PushY[Pindex++]; Counting++; Push(); Display(); Sleep(300); break; } }while(1); } int main() { S.Createmutex(); M.StartThread(); E.StartThread(); M.WaitThread(); E.WaitThread(); return 0; } unsigned __stdcall Enemy::MoveE(LPVOID arg) { Enemy *Pe1=(Enemy*)arg; Pe1->MoveEnemy(); return 0; } unsigned __stdcall Major::MoveM(LPVOID arg) { Major *Pm1=(Major*)arg; Pm1->Motion(); return 0; }