1 2 3 4 5 { for (int j=0; j { fscanf(F,"%i ",&(*(M+i*WidthM+j))); } fscanf(F,"%i\n",&(*(M+i*WidthM+WidthM-1))); } } void TForm1::LogikaOffender() { int GlobalMaxX = 0; int GlobalMaxY = 0; int GlobalPosX = 0; int GlobalPosY = 0; int Global = 100;//Значение матрицы в точке (GlobalMaxX, GlobalMaxY) int GlobalPositionMatrix; int PositionMatrix; int PosX,PosY;//Координаты преступника в матрице из файла for (int i = 0; i < NumOfPolice; i++) { if (Proverka(Police[i]))//если преступник попал в сетку полицейского { //в PositionMatrix заносится напривление матрицы PositionMatrix = PositionM(Police[i]->GetLastX(), Police[i]->GetLastY(), Police[i]->GetX(), Police[i]->GetY()); switch (PositionMatrix) { case MUp: //DONE: понять за что отвечает Police_X, Police_Y PosY=Police_Y+Offender->GetY()-Police[i]->GetY(); PosX=Police_X+Offender->GetX()-Police[i]->GetX(); break; case MRight: PosY=Police_Y-Offender->GetX()+Police[i]->GetX(); PosX=Police_X+Offender->GetY()-Police[i]->GetY(); break; case MDown: PosY=Police_Y-Offender->GetY()+Police[i]->GetY(); PosX=Police_X-Offender->GetX()+Police[i]->GetX(); break; case MLeft: PosY=Police_Y+Offender->GetX()-Police[i]->GetX(); PosX=Police_X-Offender->GetY()+Police[i]->GetY(); break; } int MaxX; int MaxY; if (*(Matrix+(PosY+1)*MatrixWidth+PosX)>=(*(Matrix+(PosY-1)*MatrixWidth+PosX))) { MaxX=PosX; MaxY=PosY+1; } //определяем в какую сторону ходить преступнику else { MaxX=PosX; MaxY=PosY-1; } if (*(Matrix+MaxY*MatrixWidth+MaxX)<=(*(Matrix+PosY*MatrixWidth+PosX+1))) { MaxX=PosX+1; MaxY=PosY; } if (*(Matrix+MaxY*MatrixWidth+MaxX)<=(*(Matrix+PosY*MatrixWidth+PosX-1))) { MaxX=PosX-1; MaxY=PosY; } if (Global > *(Matrix+MaxY*MatrixWidth+MaxX)) { Global = *(Matrix+MaxY*MatrixWidth+MaxX); GlobalMaxX = MaxX; GlobalMaxY = MaxY; GlobalPositionMatrix = PositionMatrix; GlobalPosX = PosX; GlobalPosY = PosY; } } }// for (int i = 0; i < NumOfPolice; i++) //проверка относительно других полицейских switch (GlobalPositionMatrix) { case MUp: Offender->Move(GlobalMaxX-GlobalPosX,GlobalMaxY-GlobalPosY,Image1); break; case MRight: Offender->Move(GlobalPosY-GlobalMaxY,GlobalMaxX-GlobalPosX,Image1); break; case MDown: Offender->Move(GlobalPosX-GlobalMaxX,GlobalPosY-GlobalMaxY,Image1); break; case MLeft: Offender->Move(GlobalMaxY-GlobalPosY,GlobalPosX-GlobalMaxX,Image1); break; } } void TForm1::LogikaPatrol(int OffenderX, int OffenderY, TCub *PoliceTmp) { int PositionMatrix = PositionM(PoliceTmp->GetLastX(), PoliceTmp->GetLastY(), PoliceTmp->GetX(), PoliceTmp->GetY()); if (Proverka(PoliceTmp)) { switch (PositionMatrix) { case MUp: if ((OffenderY-PoliceTmp->GetY()<=-PoliceSpeed)|| ((OffenderX-PoliceTmp->GetX()>=2*PoliceSpeed)&&(OffenderY-PoliceTmp->GetY()==-OffenderSpeed))) { PoliceTmp->Move(0,-PoliceSpeed,Image1); } else { PoliceTmp->Move(PoliceSpeed,0,Image1); } break; case MRight: if ((PoliceTmp->GetX()-OffenderX<=-PoliceSpeed)|| ((OffenderY-PoliceTmp->GetY()>=2*PoliceSpeed)&&(PoliceTmp->GetX()-OffenderX==-OffenderSpeed))) { PoliceTmp->Move(PoliceSpeed,0,Image1); } else { PoliceTmp->Move(0,PoliceSpeed,Image1); } break; case MDown: if ((PoliceTmp->GetY()-OffenderY<=-PoliceSpeed)|| ((PoliceTmp->GetX()-OffenderX>=2*PoliceSpeed)&&(PoliceTmp->GetY()-OffenderY==-OffenderSpeed))) { PoliceTmp->Move(0,PoliceSpeed,Image1); } else { PoliceTmp->Move(-PoliceSpeed,0,Image1); } break; case MLeft: if ((OffenderX-PoliceTmp->GetX()<=-PoliceSpeed)|| ((PoliceTmp->GetY()-OffenderY>=2*PoliceSpeed)&&(OffenderX-PoliceTmp->GetX()==-OffenderSpeed))) { PoliceTmp->Move(-PoliceSpeed,0,Image1); } else { PoliceTmp->Move(0,-PoliceSpeed,Image1); } break; } } else { switch (PositionMatrix) { case MUp: if (PoliceTmp->GetY()>1) { PoliceTmp->Move(0,-PoliceSpeed,Image1); } else { PoliceTmp->Move(PoliceSpeed,0,Image1); } break; case MRight: if (PoliceTmp->GetX()<19) { PoliceTmp->Move(PoliceSpeed,0,Image1); } else { PoliceTmp->Move(0,PoliceSpeed,Image1); } case MDown: if (PoliceTmp->GetY()<29) { PoliceTmp->Move(0,PoliceSpeed,Image1); } else { PoliceTmp->Move(-PoliceSpeed,0,Image1); } case MLeft: if (PoliceTmp->GetX()>1) { PoliceTmp->Move(-PoliceSpeed,0,Image1); } else { PoliceTmp->Move(0,-PoliceSpeed,Image1); } break; } } } void TForm1::LogikaPolice(int OffenderX, int OffenderY, TCub *PoliceTmp) { int PositionMatrix = PositionM(PoliceTmp->GetLastX(), PoliceTmp->GetLastY(), PoliceTmp->GetX(), PoliceTmp->GetY()); switch (PositionMatrix) { case MUp: if ((OffenderY-PoliceTmp->GetY()<=-PoliceSpeed)|| ((OffenderX-PoliceTmp->GetX()>=2*PoliceSpeed)&&(OffenderY-PoliceTmp->GetY()==-OffenderSpeed))) { PoliceTmp->Move(0,-PoliceSpeed,Image1); } else { PoliceTmp->Move(PoliceSpeed,0,Image1); } break; case MRight: if ((PoliceTmp->GetX()-OffenderX<=-PoliceSpeed)|| ((OffenderY-PoliceTmp->GetY()>=2*PoliceSpeed)&&(PoliceTmp->GetX()-OffenderX==-OffenderSpeed))) { PoliceTmp->Move(PoliceSpeed,0,Image1); } else { PoliceTmp->Move(0,PoliceSpeed,Image1); } break; case MDown: if ((PoliceTmp->GetY()-OffenderY<=-PoliceSpeed)|| ((PoliceTmp->GetX()-OffenderX>=2*PoliceSpeed)&&(PoliceTmp->GetY()-OffenderY==-OffenderSpeed))) { PoliceTmp->Move(0,PoliceSpeed,Image1); } else { PoliceTmp->Move(-PoliceSpeed,0,Image1); } break; case MLeft: if ((OffenderX-PoliceTmp->GetX()<=-PoliceSpeed)|| ((PoliceTmp->GetY()-OffenderY>=2*PoliceSpeed)&&(OffenderX-PoliceTmp->GetX()==-OffenderSpeed))) { PoliceTmp->Move(-PoliceSpeed,0,Image1); } else { PoliceTmp->Move(0,-PoliceSpeed,Image1); } break; } } int TForm1::PositionM(int lastX, int lastY, int newX, int newY) { int temp;//возвращаемое значение if (lastX==newX) { if (newY>lastY) { temp=MDown; } else { temp=MUp; } } else { if (newX>lastX) { temp=MRight; } else { temp=MLeft; } } return temp; } void TForm1::GamesProcess() { bool tmp=1; if (!EndOfGame()) { if (tmp) { Timer2->Enabled = true; Sleep(250); Timer1->Enabled = true; if (tmp==1) {tmp=0;} else {tmp=1;} } } } void __fastcall TForm1::Timer1Timer(TObject *Sender) { Form1->LogikaOffender(); } //--------------------------------------------------------------------------- void __fastcall TForm1::Timer2Timer(TObject *Sender) { Form1->LogikaPolice(Offender->GetX(),Offender->GetY(), Police[0]); for (int i = 1; i < NumOfPolice; i++) { if (Proverka(Police[i])) { //if (*(Matrix+(Police_Y+Offender->GetY()-Police[1]->GetY())*MatrixWidth+Police_X+Offender->GetX()-Police[1]->GetX())!=-1) Form1->LogikaPolice(Offender->GetX(),Offender->GetY(), Police[i]); } } //if (Proverka(Police[6])) //{ //if (*(Matrix+(Police_Y+Offender->GetY()-Police[1]->GetY())*MatrixWidth+Police_X+Offender->GetX()-Police[1]->GetX())!=-1) // Form1->LogikaPolice(Offender->GetX(),Offender->GetY(), Police[6]); //} //TODO: Понять почему не работает второй и третий полицейский //Form1->LogikaPatrol(Offender->GetX(),Offender->GetY(), Police[2]); //Form1->LogikaPatrol(Offender->GetX(),Offender->GetY(), Police[3]); //Form1->LogikaPatrol(Offender->GetX(),Offender->GetY(), Police[4]); //Form1->LogikaPatrol(Offender->GetX(),Offender->GetY(), Police[5]); if (EndOfGame()) { Timer1->Enabled = false; Timer2->Enabled = false; } } //--------------------------------------------------------------------------- void __fastcall TForm1::FormKeyDown(TObject *Sender, WORD &Key, TShiftState Shift) { if (control&&PushStart) { //TODO: переделать PositionMatr, чтобы считалась тут switch (Key) { case VK_UP: if ((PositionMatr==MUp)||(PositionMatr==MLeft)) { Police[0]->Move(0,-PoliceSpeed,Image1); PositionMatr = PositionM(Police[0]->GetLastX(),Police[0]->GetLastY(),Police[0]->GetX(),Police[0]->GetY()); PoliceShag=1; CenaControl++; //Form1->LogikaOffender(Police->GetX(),Police->GetY(),PositionMatr); } break; case VK_RIGHT: if ((PositionMatr==MUp)||(PositionMatr==MRight)) { Police[0]->Move(PoliceSpeed,0,Image1); PositionMatr = PositionM(Police[0]->GetLastX(),Police[0]->GetLastY(),Police[0]->GetX(),Police[0]->GetY()); PoliceShag=1; CenaControl++; //Form1->LogikaOffender(Police->GetX(),Police->GetY(),PositionMatr); } break; case VK_DOWN: if ((PositionMatr==MDown)||(PositionMatr==MRight)) { Police[0]->Move(0,PoliceSpeed,Image1); PositionMatr = PositionM(Police[0]->GetLastX(),Police[0]->GetLastY(),Police[0]->GetX(),Police[0]->GetY()); PoliceShag=1; CenaControl++; //Form1->LogikaOffender(Police->GetX(),Police->GetY(),PositionMatr); } break; case VK_LEFT: if ((PositionMatr==MDown)||(PositionMatr==MLeft)) { Police[0]->Move(-PoliceSpeed,0,Image1); PositionMatr = PositionM(Police[0]->GetLastX(),Police[0]->GetLastY(),Police[0]->GetX(),Police[0]->GetY()); PoliceShag=1; CenaControl++; //Form1->LogikaOffender(Police->GetX(),Police->GetY(),PositionMatr); } break; case VK_SPACE: helpPolice = true; break; } } } //--------------------------------------------------------------------------- void __fastcall TForm1::N8Click(TObject *Sender) { Application->CreateForm(__classid(TAboutBox), &AboutBox); AboutBox->ShowModal(); AboutBox->Free(); } //--------------------------------------------------------------------------- void __fastcall TForm1::BitBtn1Click(TObject *Sender) { Close(); } //--------------------------------------------------------------------------- void __fastcall TForm1::Button2Click(TObject *Sender) { if (!flagMap) { Application->CreateForm(__classid(TForm5), &Form5); Form5->Show(); flagMap = true; } } //--------------------------------------------------------------------------- //---------------------------------------------------------------------------Unit2.cpp #pragma hdrstop #include "Unit2.h" #include "Unit1.h" #include "stdio.h" //--------------------------------------------------------------------------- #pragma package(smart_init) TCub::TCub(int newX, int newY, TColor tColor, TImage *Img) { x=newX; y=newY; lastX=x; lastY=y; color=tColor; Point(Img, 1); } void TCub::ChangeCoord(int dx, int dy) { lastX=x; lastY=y; x=x+dx; y=y+dy; } void TCub::Point(TImage *Img, bool VisibleFlag) { int sizeH=Form1->sizeH; switch (VisibleFlag) { case true: Img->Canvas->Pen->Color = color; break; case false: Img->Canvas->Pen->Color = clWhite; break; } Img->Canvas->Pen->Width = sizeH/2; Img->Canvas->Ellipse(sizeH*(x-0.25),sizeH*(y-0.25),sizeH*(x+0.25),sizeH*(y+0.25)); Img->Canvas->Pen->Width = 1; // TODO: Доделать это условие. Понять что там происходит и почему не рисует. if (lastX != x || lastY != y) { if (!VisibleFlag) { Img->Canvas->Pen->Color = clBlack; Img->Canvas->MoveTo(x*sizeH,sizeH*(y-0.5)); Img->Canvas->LineTo(sizeH*x,sizeH*(y+0.5)); Img->Canvas->MoveTo(sizeH*(x-0.5),sizeH*y); Img->Canvas->LineTo(sizeH*(x+0.5),sizeH*y); } else { Img->Canvas->Pen->Width = sizeH/4; Img->Canvas->Pen->Color = color; Img->Canvas->MoveTo(lastX*sizeH,lastY*sizeH); Img->Canvas->LineTo(x*sizeH,y*sizeH); Img->Canvas->Pen->Width = 1; } } } void TCub::Move(int dx, int dy, TImage *Img) { Point(Img, 0); ChangeCoord(dx,dy); Point(Img, 1); } int TCub::GetX() { return x; } int TCub::GetY() { return y; } int TCub::GetLastX() { return lastX; } int TCub::GetLastY() { return lastY; } //---------------------------------------------------------------------------Unit3.cpp #include #pragma hdrstop #include "Unit3.h" #include "Unit1.h" #include //--------------------------------------------------------------------------- #pragma package(smart_init) #pragma resource "*.dfm" TForm3 *Form3; //--------------------------------------------------------------------------- __fastcall TForm3::TForm3(TComponent* Owner) : TForm(Owner) { } //--------------------------------------------------------------------------- void __fastcall TForm3::Button1Click(TObject *Sender) { if (Form3->OpenDialog1->Execute()) { Edit2->Text = OpenDialog1->FileName; } } //--------------------------------------------------------------------------- void __fastcall TForm3::Button3Click(TObject *Sender) { Form3->Close(); } //--------------------------------------------------------------------------- void __fastcall TForm3::FormClose(TObject *Sender, TCloseAction &Action) { Form1->Enabled = true; } //--------------------------------------------------------------------------- void __fastcall TForm3::Button2Click(TObject *Sender) { TIniFile *ini; ini = new TIniFile(ChangeFileExt(Application->ExeName,".INI")); ini->WriteString("матрица","Размер_ячейки",Edit1->Text); ini->WriteString("матрица","Файл_с_матрицей",Edit2->Text); delete ini; if (Form1->NewGame == 1) { Form1->N2Click(NULL); } MessageBox(NULL,"Изменения сохранены","Сообщение",MB_OK | MB_ICONINFORMATION); Form3->Close(); } //--------------------------------------------------------------------------- 1 2 3 4 5 |