According to my idea, the characters change their position by 1 square in one random direction out of eight. The problem is that during the cycle they can change their position by more than 1 cell, and when adding the zeroing variables moveX and moveY, the program began to crumble.

#include <iostream> #include <fstream> #include <cstdlib> #include <time.h> #include <conio.h> #include <dos.h> using namespace std; static const int width = 1000; //Π΄Π»ΠΈΠ½Π° ΠΈ ΡˆΠΈΡ€ΠΈΠ½Π° ΠΊΠ°Ρ€Ρ‚Ρ‹ static const int length = 1000; int playField[width][length]; //массив ΠΊΠ°Ρ€Ρ‚Ρ‹ int moveX, moveY; //ΠΏΠ΅Ρ€Π΅ΠΌΠ΅Π½Π½Ρ‹Π΅ Π½ΠΎΠ²Ρ‹Ρ… ΠΊΠΎΠΎΡ€Π΄ΠΈΠ½Π°Ρ‚ class Characters{ private: int id; int hp; //Ρ…Π°ΠΏΡΡˆΠΊΠΈ public: int area; int posX, posY; //позиция пСрсонаТа void moveTo(int newX, int newY, int ID){ area = playField[moveX][moveY]; //сохр. значСния ΠΊΠ»Π΅Ρ‚ΠΊΠΈ posX = newX; posY = newY; playField[newX][newY]=ID; } void setId(int newID){ id = newID; } void showId(){ cout<<"ID: "<<id<<endl; } void showPosition(){ cout<<"X: "<<posX<<endl<<"Y: "<<posY<<endl; } }; /////////////////////////////////////////////////////////// class PlayField{ private: int area; public: void fillMap(){ //тСст функция for (int i=0; i<=width-1; i++) //Π²Π±ΠΈΠ²Π°Π΅ΠΌ Π½ΡƒΠ»ΠΈ Π² массив for (int j=0; j<=length-1; j++) playField[i][j]=-1; } void moveToFile(){ ofstream file; file.open("C:\\Users\\NoName\\Desktop\\Game\\PlayField.txt",ios::out); cout<<"file status: "<<file.is_open()<<endl; //ΠΏΡ€ΠΎΠ²Π΅Ρ€ΠΊΠ° Ρ„Π°ΠΉΠ»Π° for (int i=0; i<=width-1; i++){ //пСрСнос массива Π² Ρ„Π°ΠΉΠ» file<<endl; for (int j=0; j<=length-1; j++) file<<playField[i][j]; } file.close(); } void moveCh(int x, int y, int id){ //playField[x][y]=id; } }; ///////////////////////////////////////////////////////////// int main(){ cout<<"Welcome, nine tailed fox. Please input number of characters: "; bool cycleRunning = 1; //Π²Ρ‹ΠΊΠ»ΡŽΡ‡Π°Ρ‚Π΅Π»ΡŒ для основного Ρ†ΠΈΠΊΠ»Π° bool busy = 0; //ΠΏΡ€ΠΎΠ²Π΅Ρ€ΠΊΠ° свободности ΠΊΠ»Π΅Ρ‚ΠΊΠΈ int chX, chY; //ΠΊΠΎΠΎΡ€Π΄ΠΈΠ½Π°Ρ‚Ρ‹ спавна int way; //Π½Π°ΠΏΡ€Π°Π²Π»Π΅Π½ΠΈΠ΅ сл. шага int seconds = 0; //для Ρ‚Π°ΠΉΠΌΠ΅Ρ€Π° srand(time(NULL)); //объявлСниС Ρ€Π°Π½Π΄ΠΎΠΌΠ°ΠΉΠ·Π΅Ρ€Π° int numOfChar; cin>>numOfChar; //ΠΊΠΎΠ»-Π²ΠΎ Ρ‡Π°Ρ€ΠΎΠ² int *charId = new int[numOfChar]; //массив ΠΈΠ΄ int lastID = 0; //посл ΠΈΠ΄ PlayField field; field.fillMap(); Characters *ch = new Characters[numOfChar]; //созданиС пСрсонаТСй for(int i=0; i<numOfChar; i++){ busy = 0; ch[i].setId(i); do{ chX = rand()%1000, chY = rand()%1000; //Π½Π°Ρ‡Π°Π»ΡŒΠ½Ρ‹Π΅ ΠΊΠΎΠΎΡ€Π΄ΠΈΠ½Π°Ρ‚Ρ‹ if (i<2) if (i==1) if(ch[0].posX==chX) if(ch[0].posY==chY) busy = 1; else for(int j=i-2; j>=0; j--){ if(ch[j].posX==chX) //ΠΏΡ€ΠΎΠ²Π΅Ρ€ΠΊΠ° наличия ΠΏΡ€Π΅Π΄Ρ‹Π΄ΡƒΡ‰ΠΈΡ… Ρ‡Π°Ρ€ΠΎΠ² Π½Π° ΠΊΠ»Π΅Ρ‚ΠΊΠ΅ if(ch[j].posY==chY) busy = 1; } }while(busy == 1); ch[i].moveTo(chX, chY, i); ch[i].showPosition(); ch[i].showId(); cout<<endl; } while(cycleRunning != 0){ if(kbhit()==1) cycleRunning=0; if(seconds != time(NULL)){ //Ρ‚Π°ΠΉΠΌΠ΅Ρ€ seconds = time(NULL); for(int i=0; i<numOfChar; i++){ do{ busy = 0; if (i<2) switch (i){ case 0: { way = rand() % 8; switch (way) //Π²Ρ‹Π±ΠΎΡ€ направлСния ΠΈ ΠΏΠ΅Ρ€Π΅ΠΌΠ΅Ρ‰Π΅Π½ΠΈΠ΅ { case 0: {moveX = ch[i].posX - 1; moveY = ch[i].posY + 1;} break; case 1: {moveY = ch[i].posY + 1;} break; case 2: {moveX = ch[i].posX + 1; moveY = ch[i].posY + 1;} break; case 3: {moveX = ch[i].posX + 1;} break; case 4: {moveX = ch[i].posX + 1; moveY = ch[i].posY - 1;} break; case 5: {moveY = ch[i].posY - 1;} break; case 6: {moveX = ch[i].posX - 1; moveY = ch[i].posY - 1;} break; case 7: {moveX = ch[i].posX - 1;} break; } if(moveX<0) if(moveY<0) busy = 1; } break; case 1: { way = rand() % 8; switch (way) { case 0: {moveX = ch[i].posX - 1; moveY = ch[i].posY + 1;} break; case 1: {moveY = ch[i].posY + 1;} break; case 2: {moveX = ch[i].posX + 1; moveY = ch[i].posY + 1;} break; case 3: {moveX = ch[i].posX + 1;} break; case 4: {moveX = ch[i].posX + 1; moveY = ch[i].posY - 1;} break; case 5: {moveY = ch[i].posY - 1;} break; case 6: {moveX = ch[i].posX - 1; moveY = ch[i].posY - 1;} break; case 7: {moveX = ch[i].posX - 1;} break; } if(moveX<0) if(moveY<0) if(ch[0].posX==moveX) if(ch[0].posY==moveY) busy = 1;} break; } else{ way = (rand() % 8); cout<<way<<endl; switch (way) { case 0: {moveX = ch[i].posX - 1; moveY = ch[i].posY + 1;} break; case 1: {moveY = ch[i].posY + 1;} break; case 2: {moveX = ch[i].posX + 1; moveY = ch[i].posY + 1;} break; case 3: {moveX = ch[i].posX + 1;} break; case 4: {moveX = ch[i].posX + 1; moveY = ch[i].posY - 1;} break; case 5: {moveY = ch[i].posY - 1;} break; case 6: {moveX = ch[i].posX - 1; moveY = ch[i].posY - 1;} break; case 7: {moveX = ch[i].posX - 1;} break; } for(int j=i-2; j>=0; j--){ if(moveX<0) if(moveY<0) if(ch[j].posX==moveX) //ΠΏΡ€ΠΎΠ²Π΅Ρ€ΠΊΠ° наличия ΠΏΡ€Π΅Π΄Ρ‹Π΄ΡƒΡ‰ΠΈΡ… Ρ‡Π°Ρ€ΠΎΠ² Π½Π° ΠΊΠ»Π΅Ρ‚ΠΊΠ΅ if(ch[j].posY==moveY) busy = 1; } } }while(busy == 1); playField[ch[i].posX] [ch[i].posY] = ch[i].area; //восстановлСниС значСния Ρ‚Π΅Ρ€Ρ€ΠΈΡ‚ΠΎΡ€ΠΈΠΈ ch[i].moveTo(moveX, moveY, i); //field.moveCh(moveX, moveY, i); ch[i].showPosition(); ch[i].showId(); cout<<endl<<endl; } } } //field.moveToFile(); delete[] charId; return 0; } 
  • Why do you need variables in the scope of the whole file? Why do you have variable fields in a class that are not private? - Andrej Levkovitch
  • Did you decide to use switch yourself or did someone show it to you? This is terribly wrong. I mean where you use it for the first time. - Andrej Levkovitch
  • @AndrejLevkovitch The scope of variables for use in class objects, fields with variables not in private to change and use them in the main function, and I myself decided to use switch. - No Name
  • @AndrejLevkovitch understand what you mean. Initially I wanted to add a case with 0, but then I understood it is not necessary, but because everything worked, then forgot about it. - No Name
  • Variables in the general scope should never be allocated, as well as non-encapsulated members of the class is also a bad idea, not for this classes were created. To change class members, use methods, and NEVER make global variables. - Andrej Levkovitch

1 answer 1

Random direction does not respond to the edge of the map, where the directions are limited. Therefore, the subscriber goes beyond the negative coordinates. Or to infinity.

  • Added a crutch limiting negative values, but the problem of anomalous jumps has not gone away. - No Name
  • I also limited the coordinates and everything plows. if(moveX<0 or moveY<0 or moveX >= width or moveY >= length) busy = 1 ; - AlexGlebe
  • really thank you very much. - No Name