Gra PacMan

0

Witam. Postanowiłem tym razem odtworzyć grę PacMan w C++ i chciałbym żeby ktoś sprawdził,czy klon działa podobnie do orginału. Kod jest nie idealny i planuje go jeszcze poprawić.
Dziękuje za uwagę.

#include<iostream>
#include<vector>
#include<array>
#include<thread>
#include<string>
#include<algorithm>
#include<conio.h>
#include<chrono>
#include<random>


enum TYPES
{	
	GHOST_RED = 1,
	GHOST_PINK = 2,
	GHOST_CYAN = 3,
	GHOST_ORANGE = 4,
	PLAYER = 5,
	DOT = 6,
	SUPER_DOT = 7,
};
enum MODE {
	SCATTER = 1,
	CHASE = 2,
	EATEN = 3,
	FRIGHTENED = 4,
	EXITING_ROOM = 5,
};

class GameObject {
private:
	int X;
	int Y;
	int TYPE;
	int MODE;
public:
	int PREVIOUS_MODE = SCATTER;
	std::chrono::steady_clock::time_point Ghost_Movment_Diffrence = std::chrono::steady_clock::now();
	std::array<std::string, 4> COLLISION = { " "," "," "," "};
	std::array<int, 2> TARGET_TILE = { 0,0 };
	std::array<int, 2> DIRECTION = { 0,0 };
	GameObject(int x, int y, int type, int mode,std::array<int,2> direction) {
		X = x;
		Y = y;
		TYPE = type;
		DIRECTION = direction;
		MODE = mode;
	}
	void SetX(int x) {
		X = x;
	}
	void SetY(int y) {
		Y = y;
	}
	void SetType(int type) {
		TYPE = type;
	}
	void SetMode(int mode) {
		MODE = mode;
	}
	int GetX() {
		return X;
	}
	int GetY() {
		return Y;
	}
	int GetType() {
		return TYPE;
	}
	int GetMode() {
		return MODE;
	}
};


struct less_than_key
{
	inline bool operator() ( GameObject& struct1,  GameObject& struct2)
	{
		return (struct1.GetType() > struct2.GetType());
	}
};





void SetMap(std::array<std::array<std::string, 23>, 25>& map,std::vector<GameObject> &objects) {
	
	
	map = { {
		{"#","#","#","#","#","#","#","#","#","#","#","#","#","#","#","#","#","#","#","#","#","#","#"},
		{"#"," "," "," "," "," "," "," "," "," "," ","#"," "," "," "," "," "," "," "," "," "," ","#"},
		{"#"," ","#","#","#"," ","#","#","#","#"," ","#"," ","#","#","#","#"," ","#","#","#"," ","#"},
		{"#"," ","#","#","#"," ","#","#","#","#"," ","#"," ","#","#","#","#"," ","#","#","#"," ","#"},
		{"#"," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," ","#"},
		{"#"," ","#","#","#"," ","#"," ","#","#","#","#","#","#","#"," ","#"," ","#","#","#"," ","#"},
		{"#"," "," "," "," "," ","#"," ","#","#","#","#","#","#","#"," ","#"," "," "," "," "," ","#"},
		{"#","#","#","#","#"," ","#"," "," "," "," ","#"," "," "," "," ","#"," ","#","#","#","#","#"},
		{"#","#","#","#","#"," ","#","#","#","#"," ","#"," ","#","#","#","#"," ","#","#","#","#","#"},
		{"#","#","#","#","#"," ","#"," "," "," "," "," "," "," "," "," ","#"," ","#","#","#","#","#"},
		{"#","#","#","#","#"," ","#"," ","#","#","#"," ","#","#","#"," ","#"," ","#","#","#","#","#"},
		{" "," "," "," "," "," "," "," ","#"," "," "," "," "," ","#"," "," "," "," "," "," "," "," "},
		{"#","#","#","#","#"," ","#"," ","#","#","#","#","#","#","#"," ","#"," ","#","#","#","#","#"},
		{"#","#","#","#","#"," ","#"," "," "," "," "," "," "," "," "," ","#"," ","#","#","#","#","#"},
		{"#","#","#","#","#"," ","#"," ","#","#","#","#","#","#","#"," ","#"," ","#","#","#","#","#"},
		{"#","#","#","#","#"," ","#"," ","#","#","#","#","#","#","#"," ","#"," ","#","#","#","#","#"},
		{"#"," "," "," "," "," "," "," "," "," "," ","#"," "," "," "," "," "," "," "," "," "," ","#"},
		{"#"," ","#","#","#"," ","#","#","#","#"," ","#"," ","#","#","#","#"," ","#","#","#"," ","#"},
		{"#"," "," "," ","#"," "," "," "," "," "," "," "," "," "," "," "," "," ","#"," "," "," ","#"},
		{"#","#","#"," ","#"," ","#"," ","#","#","#","#","#","#","#"," ","#"," ","#"," ","#","#","#"},
		{"#"," "," "," "," "," ","#"," "," "," "," ","#"," "," "," "," ","#"," "," "," "," "," ","#"},
		{"#"," ","#","#","#","#","#","#","#","#"," ","#"," ","#","#","#","#","#","#","#","#"," ","#"},
		{"#"," ","#","#","#","#","#","#","#","#"," ","#"," ","#","#","#","#","#","#","#","#"," ","#"},
		{"#"," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," ","#"},
		{"#","#","#","#","#","#","#","#","#","#","#","#","#","#","#","#","#","#","#","#","#","#","#"}
	} };
	std::sort(objects.begin(), objects.end(), less_than_key());
	for (int i = 0; i < objects.size(); i++) {
		switch (objects[i].GetType())
		{
		case DOT:
			map[objects[i].GetY()][objects[i].GetX()] = "*";
			break;
		case SUPER_DOT:
			map[objects[i].GetY()][objects[i].GetX()] = "@";
			break;
		case PLAYER:
			map[objects[i].GetY()][objects[i].GetX()] = "M";
			break;
		case GHOST_RED:
			map[objects[i].GetY()][objects[i].GetX()] = "&";
			break;
		case GHOST_ORANGE:
			map[objects[i].GetY()][objects[i].GetX()] = "%";
			break;
		case GHOST_CYAN:
			map[objects[i].GetY()][objects[i].GetX()] = "!";
			break;
		case GHOST_PINK:
			map[objects[i].GetY()][objects[i].GetX()] = "$";
			break;
		default:
			break;
		}
		
	}
}
void Draw(std::array<std::array<std::string, 23>, 25>& map,std::vector<GameObject>& objects) { //optimize this
	for (int i = 0; i < 25; i++) {
		for (int j = 0; j < 23; j++) {

			if (map[i][j] == "%" or map[i][j] == "&" or map[i][j] == "$" or map[i][j] == "!") {
				for (int k = 1; k < 5; k++) {
					if (objects[objects.size() - k].GetX() == j && objects[objects.size() - k].GetY() == i && objects[objects.size() - k].GetMode() == FRIGHTENED) {
						std::cout << "\u001b[38;5;33m";
					}	
				}
				std::cout << map[i][j];
				std::cout << "\u001b[38;5;37m";
			}
			else {
				std::cout << map[i][j];
			}
			

		}
		std::cout << "\n";
	}
}
void Input(char& key) {
	for (;;) {
		key = _getch();
	}
}
void Collision(GameObject& object1, std::array<std::array<std::string, 23>, 25>& map) { 
	object1.COLLISION = { " "," "," "," "};
	int getX = object1.GetX();
	int getY = object1.GetY();
	if (getX + 1 >= 23) {
		object1.COLLISION[0] = map[object1.GetY()][0];
		object1.COLLISION[1] = map[object1.GetY()][object1.GetX() - 1];
	}
	else if (getX - 1 < 0) {
		object1.COLLISION[0] = map[object1.GetY()][object1.GetX() + 1];
		object1.COLLISION[1] = map[object1.GetY()][22];
	}
	else {
		object1.COLLISION[0] = map[object1.GetY()][object1.GetX() + 1];
		object1.COLLISION[1] = map[object1.GetY()][object1.GetX() - 1];
	}
	object1.COLLISION[2] = map[object1.GetY() + 1][object1.GetX()];
	object1.COLLISION[3] = map[object1.GetY() - 1][object1.GetX()];

}

int main() {
	system("cls");
	int Level = 1;
	int Blinky_Pellet_Skip = 20;
	loop:
	bool switched = false;
	int pellets = 190;
	bool Dead = false;
	char Key;
	int Index = 0;
	int Player_ID = 0;
	int Score = 0;
	std::vector<GameObject> objects;
	std::array<int, 8> Duration;
	std::array<int, 8> GHOST_MODES = { SCATTER,CHASE,SCATTER,CHASE,SCATTER,CHASE,SCATTER,CHASE };
	std::array<std::array<std::string,23>, 25> map;
	if (Level == 1) {
		Duration = { 7,20,7,20,5,20,5,20 };
		Blinky_Pellet_Skip = 20;
	}
	else if (Level >= 2 && Level <= 4) {
		Duration = { 7,20,7,20,5,1033,1 };
	}
	else if (Level >= 5) {
		Duration = { 5,20,7,20,5,1033,1 };
	}
	if (Level == 2) {
		Blinky_Pellet_Skip = 30;
	}
	if (Level >= 3 && Level <= 5) {
		Blinky_Pellet_Skip = 40;
	}
	if (Level >= 6 && Level <= 8) {
		Blinky_Pellet_Skip = 50;
	}
	if (Level >= 9 && Level <= 11) {
		Blinky_Pellet_Skip = 60;
	}
	if (Level >= 12 && Level <= 14) {
		Blinky_Pellet_Skip = 80;
	}
	if (Level >= 15 && Level <= 18) {
		Blinky_Pellet_Skip = 100;
	}
	if (Level >= 19) {
		Blinky_Pellet_Skip = 120;
	}


	map = { {
		{"#","#","#","#","#","#","#","#","#","#","#","#","#","#","#","#","#","#","#","#","#","#","#"},
		{"#","*","*","*","*","*","*","*","*","*","*","#","*","*","*","*","*","*","*","*","*","*","#"},
		{"#","@","#","#","#","*","#","#","#","#","*","#","*","#","#","#","#","*","#","#","#","@","#"},
		{"#","*","#","#","#","*","#","#","#","#","*","#","*","#","#","#","#","*","#","#","#","*","#"},
		{"#","*","*","*","*","*","*","*","*","*","*","*","*","*","*","*","*","*","*","*","*","*","#"},
		{"#","*","#","#","#","*","#","*","#","#","#","#","#","#","#","*","#","*","#","#","#","*","#"},
		{"#","*","*","*","*","*","#","*","#","#","#","#","#","#","#","*","#","*","*","*","*","*","#"},
		{"#","#","#","#","#","*","#","*","*","*","*","#","*","*","*","*","#","*","#","#","#","#","#"},
		{"#","#","#","#","#","*","#","#","#","#"," ","#"," ","#","#","#","#","*","#","#","#","#","#"},
		{"#","#","#","#","#","*","#"," "," "," "," ","&"," "," "," "," ","#","*","#","#","#","#","#"},
		{"#","#","#","#","#","*","#"," ","#","#","#"," ","#","#","#"," ","#","*","#","#","#","#","#"},
		{" "," "," "," "," ","*"," "," ","#","%"," "," ","$","!","#"," "," ","*"," "," "," "," "," "},
		{"#","#","#","#","#","*","#"," ","#","#","#","#","#","#","#"," ","#","*","#","#","#","#","#"},
		{"#","#","#","#","#","*","#"," "," "," "," "," "," "," "," "," ","#","*","#","#","#","#","#"},
		{"#","#","#","#","#","*","#"," ","#","#","#","#","#","#","#"," ","#","*","#","#","#","#","#"},
		{"#","#","#","#","#","*","#"," ","#","#","#","#","#","#","#"," ","#","*","#","#","#","#","#"},
		{"#","*","*","*","*","*","*","*","*","*","*","#","*","*","*","*","*","*","*","*","*","*","#"},
		{"#","*","#","#","#","*","#","#","#","#","*","#","*","#","#","#","#","*","#","#","#","*","#"},
		{"#","@","*","*","#","*","*","*","*","*","*","M","*","*","*","*","*","*","#","*","*","@","#"},
		{"#","#","#","*","#","*","#","*","#","#","#","#","#","#","#","*","#","*","#","*","#","#","#"},
		{"#","*","*","*","*","*","#","*","*","*","*","#","*","*","*","*","#","*","*","*","*","*","#"},
		{"#","*","#","#","#","#","#","#","#","#","*","#","*","#","#","#","#","#","#","#","#","*","#"},
		{"#","*","#","#","#","#","#","#","#","#","*","#","*","#","#","#","#","#","#","#","#","*","#"},
		{"#","*","*","*","*","*","*","*","*","*","*","*","*","*","*","*","*","*","*","*","*","*","#"},
		{"#","#","#","#","#","#","#","#","#","#","#","#","#","#","#","#","#","#","#","#","#","#","#"}
	} };
	for (int i = 0; i < 25; i++) {
		for (int j = 0; j < 23; j++) {
			if (map[i][j] == "*") {
				objects.emplace_back(GameObject(j, i, DOT, SCATTER,{ 0,0 }));
			}
			else if (map[i][j] == "@") {
				objects.emplace_back(GameObject(j, i, SUPER_DOT, SCATTER,{ 0,0 }));
			}
			else if (map[i][j] == "M") {
				objects.emplace_back(GameObject(j, i, PLAYER, SCATTER,{ 1,0 }));
			}
			else if (map[i][j] == "&") {
				
				objects.emplace_back(GameObject(j, i, GHOST_RED, SCATTER,{ 0,-1 }));
				objects[objects.size() - 1].TARGET_TILE = { 26,-3 };
				objects[objects.size() - 1].PREVIOUS_MODE = SCATTER;
			}
			else if (map[i][j] == "%") {
				objects.push_back(GameObject(j, i, GHOST_ORANGE, EXITING_ROOM,{ 0,-1 }));
				objects[objects.size() - 1].TARGET_TILE = { j,i };
				objects[objects.size() - 1].PREVIOUS_MODE = SCATTER;
			}
			else if (map[i][j] == "!") {
				objects.push_back(GameObject(j, i, GHOST_CYAN, EXITING_ROOM,{ 0,-1 }));
				objects[objects.size() - 1].TARGET_TILE = { j,i };
				objects[objects.size() - 1].PREVIOUS_MODE = SCATTER;
			}
			else if (map[i][j] == "$") {
				objects.push_back(GameObject(j, i, GHOST_PINK, EXITING_ROOM,{ 0,-1 }));
				objects[objects.size() - 1].TARGET_TILE = { 11,10 };
				objects[objects.size() - 1].PREVIOUS_MODE = SCATTER;
			}
		}
	}
	
	std::thread InputThread(Input,std::ref(Key)); //optimize the thread system
	std::chrono::steady_clock::time_point Frightened_Diffrence = std::chrono::steady_clock::now();
	std::chrono::steady_clock::time_point MovmentDiffrence = std::chrono::steady_clock::now();
	std::chrono::steady_clock::time_point Ghost_Diffrence = std::chrono::steady_clock::now();
	std::chrono::steady_clock::time_point clock;
	for (;;) {
		if (Dead == true) { std::cout << "You dead! "; std::cout << "Score you managed to get:" << Score; std::this_thread::sleep_for(std::chrono::seconds(1)); system("cls"); InputThread.detach(); Score = 0; goto loop; }
		if (pellets <= 0) { std::cout << "Next Level! "; std::cout << "Current Score:" << Score; std::this_thread::sleep_for(std::chrono::seconds(1)); system("cls"); InputThread.detach(); Level += 1; pellets = 190; goto loop; }
		clock = std::chrono::steady_clock::now();
		
		SetMap(map,objects);
		Draw(map,objects);
		Player_ID = objects.size() - 5;
		Collision(objects[Player_ID], map);
		for (int i = 1; i < 5; i++) {
			Collision(objects[Player_ID + i], map);
		}
		for (int i = 1; i < 5; i++) {

			if (objects[Player_ID + i].GetMode() == FRIGHTENED && std::chrono::duration_cast<std::chrono::seconds>(clock - Frightened_Diffrence).count()  >= 6) {
				for (int j = 1; j < 5; j++) {
					if (objects[Player_ID + j].GetMode() == FRIGHTENED) {
						objects[Player_ID + j].SetMode(objects[Player_ID + j].PREVIOUS_MODE);
						objects[Player_ID + j].DIRECTION[0] = -objects[Player_ID + j].DIRECTION[0];
						objects[Player_ID + j].DIRECTION[1] = -objects[Player_ID + j].DIRECTION[1];
						switched = true;
					}
				}
			}
			if (objects[Player_ID + i].GetMode() != FRIGHTENED) {
				if (objects[Player_ID + i].GetType() == GHOST_CYAN && pellets > 160) {
					continue;
				}
				else if(objects[Player_ID + i].GetType() == GHOST_CYAN && pellets <= 160 && objects[Player_ID + i].GetMode() == EXITING_ROOM) {
					objects[Player_ID + i].TARGET_TILE = { 11,10 };
				}
				if (objects[Player_ID + i].GetType() == GHOST_ORANGE && pellets > 124) {

					continue;
				}
				else if (objects[Player_ID + i].GetType() == GHOST_ORANGE && pellets <= 124 && objects[Player_ID + i].GetMode() == EXITING_ROOM) {

					objects[Player_ID + i].TARGET_TILE = { 11,10 };
				}
				if (objects[Player_ID + i].GetMode() == EXITING_ROOM && objects[Player_ID + i].GetX() == 11 && objects[Player_ID + i].GetY() == 9) {
					objects[Player_ID + i].SetMode(GHOST_MODES[Index]);
					
				}
				if (objects[Player_ID + i].GetMode() != EXITING_ROOM && objects[Player_ID + i].GetMode() != EATEN) {
					objects[Player_ID + i].PREVIOUS_MODE = objects[Player_ID + i].GetMode();
					objects[Player_ID + i].SetMode(GHOST_MODES[Index]);
					if (objects[Player_ID + i].GetType() == GHOST_RED && pellets <= Blinky_Pellet_Skip) {
						objects[Player_ID + i].SetMode(CHASE);
					}
				}
				if (objects[Player_ID + i].GetMode() == SCATTER) {
					if (objects[Player_ID + i].GetType() == GHOST_RED) {
						objects[Player_ID + i].TARGET_TILE = { 23,-3 };
					}
					else if (objects[Player_ID + i].GetType() == GHOST_ORANGE) {
						objects[Player_ID + i].TARGET_TILE = { 0,-3 };
					}
					else if (objects[Player_ID + i].GetType() == GHOST_PINK) {
						objects[Player_ID + i].TARGET_TILE = { 26,27 };
					}
					else if (objects[Player_ID + i].GetType() == GHOST_CYAN) {
						objects[Player_ID + i].TARGET_TILE = { 3,27 };
					}
				}
				else if (objects[Player_ID + i].GetMode() == CHASE) {
					if (objects[Player_ID + i].GetType() == GHOST_RED) {
						objects[Player_ID + i].TARGET_TILE = { objects[Player_ID].GetX(),objects[Player_ID].GetY() };
					}
					else if (objects[Player_ID + i].GetType() == GHOST_PINK) {
						if (objects[Player_ID].DIRECTION[0] == 0 && objects[Player_ID].DIRECTION[1] == -1) {
							objects[Player_ID + i].TARGET_TILE = { objects[Player_ID].GetX() - 4,objects[Player_ID].DIRECTION[1] * 4 + objects[Player_ID].GetY() };
							
						}
						else {
							objects[Player_ID + i].TARGET_TILE = { objects[Player_ID].DIRECTION[0] * 4 + objects[Player_ID].GetX(),objects[Player_ID].DIRECTION[1] * 4 + objects[Player_ID].GetY() };
						}
					}
					else if (objects[Player_ID + i].GetType() == GHOST_CYAN) {
						int X1;
						int Y1;
						if (objects[Player_ID].DIRECTION[0] == 0 && objects[Player_ID].DIRECTION[1] == -1) {
							X1 = objects[Player_ID].GetX() -2;
							Y1 = objects[Player_ID].GetY() + objects[Player_ID].DIRECTION[1] * 2;

						}
						else {
							X1 = objects[Player_ID].GetX() + objects[Player_ID].DIRECTION[0] * 2;
							Y1 = objects[Player_ID].GetY() + objects[Player_ID].DIRECTION[1] * 2;
						}
						int X2 = (X1 - objects[objects.size() - 1].GetX()) * 2;
						int Y2 = (Y1 - objects[objects.size() - 1].GetY()) * 2;
						int X3 = objects[Player_ID].GetX() + X2;
						int Y3 = objects[Player_ID].GetY() + Y2;
						objects[Player_ID + i].TARGET_TILE = { X3,Y3 };
					}
					else if (objects[Player_ID + i].GetType() == GHOST_ORANGE) {
						int Distance = sqrt(pow(objects[Player_ID].GetX() - objects[Player_ID + i].GetX(), 2) + pow(objects[Player_ID].GetY() - objects[Player_ID + i].GetY(), 2));
						if (Distance < 0) { Distance = -Distance; }
						
						if (Distance > 8) {
							objects[Player_ID + i].TARGET_TILE = { objects[Player_ID].GetX(),objects[Player_ID].GetY() };
							
						}
						else if (Distance < 8) {
							objects[Player_ID + i].TARGET_TILE = { 0,-3 };
						}
					}
				}
				if (objects[Player_ID + i].GetMode() == EATEN && objects[Player_ID + i].GetX() == 11 && objects[Player_ID + i].GetY() == 11) {
					objects[Player_ID + i].TARGET_TILE = { 11,9 };
					objects[Player_ID + i].SetMode(EXITING_ROOM);
					objects[Player_ID + i].DIRECTION[0] = 0;
					objects[Player_ID + i].DIRECTION[1] = -1;
				}
				if (objects[Player_ID + i].GetMode() == EATEN) {
					objects[Player_ID + i].TARGET_TILE = { 11,11 };
				}
				
				
				std::array<int, 4> Priorities = {1,3,2,4};
				std::array<int, 4> DirectionX = { 1,-1,0,0 };
				std::array<int, 4> DirectionY = { 0,0,1,-1 };
				std::vector<int> Distance;
				std::vector<int> Priority_Push;
				std::vector<int> MoveX;
				std::vector<int> MoveY;
				for (int j = 0; j < DirectionX.size(); j++) {
					if (objects[Player_ID + i].DIRECTION[0] != -DirectionX[j] or objects[Player_ID + i].DIRECTION[1] != -DirectionY[j]) {
						
							if (objects[Player_ID + i].COLLISION[j] != "#"  ) {
									if (objects[Player_ID + i].GetMode() == EATEN or objects[Player_ID + i].GetMode() == EXITING_ROOM or objects[Player_ID + i].GetX() + DirectionX[j] != 11 or objects[Player_ID + i].GetY() + DirectionY[j] != 10) {
											MoveX.push_back(DirectionX[j]);
											MoveY.push_back(DirectionY[j]);
											Priority_Push.push_back(Priorities[j]);
											int getX = objects[Player_ID + i].GetX() + DirectionX[j];
											int getY = objects[Player_ID + i].GetY() + DirectionY[j];
											if (getX >= 23) { getX = 0; }
											else if (getX < 0) { getX = 22; }
											int TargetX = objects[Player_ID + i].TARGET_TILE[0];
											int TargetY = objects[Player_ID + i].TARGET_TILE[1];
											int Diffrence = sqrt(pow(TargetX - getX, 2) + pow(TargetY - getY, 2));
											if (Diffrence < 0) { Diffrence = -Diffrence; }
											Distance.push_back(Diffrence);
											if (DirectionY[j] == -1 && objects[Player_ID + i].GetX() == 10 && objects[Player_ID + i].GetY() == 9 && switched == false) {
												MoveX.pop_back();
												MoveY.pop_back();
												Distance.pop_back();
												Priority_Push.pop_back();
											}
											else if (DirectionY[j] == -1 && objects[Player_ID + i].GetX() == 12 && objects[Player_ID + i].GetY() == 9 && switched == false) {
												MoveX.pop_back();
												MoveY.pop_back();
												Distance.pop_back();
												Priority_Push.pop_back();
											}
											else if (DirectionY[j] == -1 && objects[Player_ID + i].GetX() == 10 && objects[Player_ID + i].GetY() == 18 && switched == false) {
												MoveX.pop_back();
												MoveY.pop_back();
												Distance.pop_back();
												Priority_Push.pop_back();
											}
											else if (DirectionY[j] == -1 && objects[Player_ID + i].GetX() == 12 && objects[Player_ID + i].GetY() == 18 && switched == false) {
												MoveX.pop_back();
												MoveY.pop_back();
												Distance.pop_back();
												Priority_Push.pop_back();
											}
										
										
										
									}
								
							}
					}
				}
				if (Distance.size() >= 1 && std::chrono::duration_cast<std::chrono::milliseconds>(clock - objects[Player_ID + i].Ghost_Movment_Diffrence).count() >= 250 ) {
				
					for (int x = 0; x < Distance.size(); x++) {
						for (int j = 0; j < Distance.size(); j++) {
							if (x != j) {
								if (Distance[x] == Distance[j]) {
									if (Priority_Push[x] > Priority_Push[j]) {
										Priority_Push.erase(Priority_Push.begin() + j);
										MoveX.erase(MoveX.begin() + j);
										MoveY.erase(MoveY.begin() + j);
										Distance.erase(Distance.begin() + j);
									}
									else {
										Priority_Push.erase(Priority_Push.begin() + x);
										MoveX.erase(MoveX.begin() + x);
										MoveY.erase(MoveY.begin() + x);
										Distance.erase(Distance.begin() + x);
									}
								}
							}
							
						}
					}
					
					std::vector<int>::iterator iter = std::min_element(Distance.begin(), Distance.end());
					
					objects[Player_ID + i].SetX(objects[Player_ID + i].GetX() + MoveX[std::distance(Distance.begin(), iter)]);
					objects[Player_ID + i].SetY(objects[Player_ID + i].GetY() + MoveY[std::distance(Distance.begin(), iter)]);
					objects[Player_ID + i].DIRECTION[0] = MoveX[std::distance(Distance.begin(), iter)];
					objects[Player_ID + i].DIRECTION[1] = MoveY[std::distance(Distance.begin(), iter)];
					if (objects[Player_ID + i].GetX() == 23 && objects[Player_ID + i].GetY() == 11) {
						objects[Player_ID + i].SetX(0);
					}
					else if (objects[Player_ID + i].GetX() == -1 && objects[Player_ID + i].GetY() == 11) {
						objects[Player_ID + i].SetX(22);
					}
					SetMap(map, objects);
					for (int i = 1; i < 5; i++) {
						Collision(objects[Player_ID + i], map);
					}

					objects[Player_ID + i].Ghost_Movment_Diffrence = std::chrono::steady_clock::now();

				}
				switched = false;
			}
			if (objects[Player_ID + i].GetMode() == FRIGHTENED) {
				std::array<int, 4> DirectionX = { 1,-1,0,0 };
				std::array<int, 4> DirectionY = { 0,0,1,-1 };
				std::vector<int> MoveX;
				std::vector<int> MoveY;
				for (int j = 0; j < DirectionX.size(); j++) {
					if (objects[Player_ID + i].DIRECTION[0] != -DirectionX[j] or objects[Player_ID + i].DIRECTION[1] != -DirectionY[j]) {

						if (objects[Player_ID + i].COLLISION[j] != "#") {
							if (objects[Player_ID + i].GetX() + DirectionX[j] != 11 or objects[Player_ID + i].GetY() + DirectionY[j] != 10 ) {
								MoveX.push_back(DirectionX[j]);
								MoveY.push_back(DirectionY[j]);
							}

						}
					}
				}
				if (std::chrono::duration_cast<std::chrono::milliseconds>(clock - objects[Player_ID + i].Ghost_Movment_Diffrence).count() >= 300 && MoveX.size()>=1) {
					
					std::random_device device;
					std::uniform_int_distribution<int> distribution(0, MoveX.size() - 1);
					int get = distribution(device);
					if (MoveX.size() == 1) { get = 0; }
					objects[Player_ID + i].SetX(objects[Player_ID + i].GetX() + MoveX[get]);
					objects[Player_ID + i].SetY(objects[Player_ID + i].GetY() + MoveY[get]);
					objects[Player_ID + i].DIRECTION[0] = MoveX[get];
					objects[Player_ID + i].DIRECTION[1] = MoveY[get];
					
					if (objects[Player_ID + i].GetX() == 23 && objects[Player_ID + i].GetY() == 11) {
						objects[Player_ID + i].SetX(0);
					}
					else if (objects[Player_ID + i].GetX() == -1 && objects[Player_ID + i].GetY() == 11) {
						objects[Player_ID + i].SetX(22);
					}
					
					SetMap(map, objects);
					for (int i = 1; i < 5; i++) {
						Collision(objects[Player_ID + i], map);
					}
					objects[Player_ID + i].Ghost_Movment_Diffrence = std::chrono::steady_clock::now();

				}


				switched = false;
			}
		}
		
		if (std::chrono::duration_cast<std::chrono::seconds>(clock - Ghost_Diffrence).count() >= Duration[Index]) {
			Ghost_Diffrence = std::chrono::steady_clock::now();
			if (Index < Duration.size() - 1) {
				Index++;
				for (int i = 1; i < 5; i++) {
					if (objects[Player_ID + i].GetMode() != FRIGHTENED && objects[Player_ID + i].GetMode() != EATEN ) {
						objects[Player_ID + i].DIRECTION[0] = -objects[Player_ID + i].DIRECTION[0];
						objects[Player_ID + i].DIRECTION[1] = -objects[Player_ID + i].DIRECTION[1];
						switched = true;
					}
				}
				
			}
			
		}
		Player_ID = objects.size() - 5;
		if (Key == 'd' && std::chrono::duration_cast<std::chrono::milliseconds>(clock- MovmentDiffrence).count() >= 250 && objects[Player_ID].COLLISION[0] != "#") {
			
			objects[Player_ID].DIRECTION = { 1,0 };
			objects[Player_ID].SetX(objects[Player_ID].GetX() + 1);
			MovmentDiffrence = std::chrono::steady_clock::now();
		}
		else if (Key == 'a' && std::chrono::duration_cast<std::chrono::milliseconds>(clock - MovmentDiffrence).count() >= 250 && objects[Player_ID].COLLISION[1] != "#") {
			objects[Player_ID].DIRECTION = { -1,0 };
			objects[Player_ID].SetX(objects[Player_ID].GetX() - 1);
			MovmentDiffrence = std::chrono::steady_clock::now();
		}
		else if (Key == 's' && std::chrono::duration_cast<std::chrono::milliseconds>(clock - MovmentDiffrence).count() >= 250 && objects[Player_ID].COLLISION[2] != "#") {
			objects[Player_ID].DIRECTION = { 0,1 };
			objects[Player_ID].SetY(objects[Player_ID].GetY() + 1);
			MovmentDiffrence = std::chrono::steady_clock::now();
		}
		else if (Key == 'w' && std::chrono::duration_cast<std::chrono::milliseconds>(clock - MovmentDiffrence).count() >= 250 && objects[Player_ID].COLLISION[3] != "#") {
			objects[Player_ID].DIRECTION = { 0,-1 };
			objects[Player_ID].SetY(objects[Player_ID].GetY() - 1);
			MovmentDiffrence = std::chrono::steady_clock::now();
		}
		
		if (objects[Player_ID].GetX() == 23 && objects[Player_ID].GetY() == 11) {
			objects[Player_ID].SetX(0);
		}
		else if (objects[Player_ID].GetX() == -1 && objects[Player_ID].GetY() == 11) {
			objects[Player_ID].SetX(22);
		}
		


			for (int k = 0; k < objects.size(); k++) {
				if (objects[k].GetType() != PLAYER && objects[k].GetX() == objects[Player_ID].GetX() && objects[k].GetY() == objects[Player_ID].GetY() ) {
					switch (objects[k].GetType()) {
					case SUPER_DOT:
						for (int g = 1; g < 5; g++) {
							if (objects[Player_ID + g].GetMode() != EATEN) {
								objects[Player_ID + g].PREVIOUS_MODE = objects[Player_ID + g].GetMode();
								objects[Player_ID + g].SetMode(FRIGHTENED);
								objects[Player_ID + g].DIRECTION[0] = -objects[Player_ID + g].DIRECTION[0];
								objects[Player_ID + g].DIRECTION[1] = -objects[Player_ID + g].DIRECTION[1];
								switched = true;
							}
						}
						Frightened_Diffrence = std::chrono::steady_clock::now();
						objects.erase(objects.begin() + k);
						Ghost_Diffrence = Ghost_Diffrence + std::chrono::seconds(6);
						Score += 50;
						break;
					case DOT:
						objects.erase(objects.begin() + k);
						pellets -= 1;
						Score += 10;
						break;
					default:
						switch (objects[k].GetMode()) {
						case FRIGHTENED:
							objects[k].SetMode(EATEN);
							Score += 200;
							break;
						case CHASE:
							Dead = true;
							break;
						case SCATTER:
							Dead = true;
							break;
						}

						break;
					}



					break;
				}

			}
			for(int i = 0; i < 26; i++){ std::cout << "\033[F"; }
			std::this_thread::sleep_for(std::chrono::milliseconds(33));
		
	}

	InputThread.detach();
}
2

Przemyśl, czy potrzebujesz tyle setterów i getterów. W szczególności SetType nigdzie nie używasz. Czy w ogóle zaistnieje kiedykolwiek potrzeba zmiany typu obiektu? (poczytaj o zasadzie YAGNI https://en.wikipedia.org/wiki/You_aren%27t_gonna_need_it ).

Pozostałe settery też na ten moment wydają się niepotrzebne, chociaż mogę sobie wyobrazić sytuację, gdzie np. SetX czy SetY ustawiałoby flagę "dirty" do obiektu. Chociaż pytanie, czy ty w swoim kodzie faktycznie o tym pomyślałeś? Bo mi to wygląda bardziej na typowe dodawanie czegoś na zapas. Efektywnie masz po prostu klasę z publicznymi polami, tylko zaciemnione jest to przez gettery i settery.

map[objects[i].GetY()][objects[i].GetX()] 

Masz duplikację tego wyrażenia

objects[objects.size() - 1]

tego też.

objects[Player_ID].DIRECTION

i to również się powtarza. Pomyśl może o wydzieleniu zmiennych na takie powtarzające się kawałki kodu.

1

Wytrwałości na planowanie krótkich i dobrze nazwanych (o jednej roli) funckji starczyło na chwilę
Reszta jest jedną funkcją na 600 linii

Pętle po magicznych stałych

Męczysz sie ze strukturami danych, wyrażnie brakuje kontenera kontenerów

for (int g = 1; g < 5; g++) {
							if (objects[Player_ID + g].GetMode() != EATEN) {

Są deklaracje enumów ale zmienne do nich są chyba int, tak samo ich arraye

... i jeszcze 50 uwag.
Ogląda sie to, jakby dwóch autorów było (pewnie jeden z netu)

Tak, wiem, to imponuje w lustrze "napisałem grę", to coś lepszego niż "ćwiczebny program który pójdzie w kosz", ale poćwicz algorytmy i struktury danych na mniejszych zagadnieniach

0

Dziękuje za uwagi. @AnyKtokolwiek @LukeJL

1

Uwaga z narzędzia SonarLint na temat main

Refactor this function to reduce its Cognitive Complexity from 361 to the 25 allowed. sonarlint(cpp:S3776)

I opis na temat sonarlint(cpp:S3776) https://rules.sonarsource.com/cpp/RSPEC-3776/
W sumie to chyba rekordowy wynik jaki widziałem

0
Marius.Maximus napisał(a):

Uwaga z narzędzia SonarLint na temat main

Refactor this function to reduce its Cognitive Complexity from 361 to the 25 allowed. sonarlint(cpp:S3776)

I opis na temat sonarlint(cpp:S3776) https://rules.sonarsource.com/cpp/RSPEC-3776/
W sumie to chyba rekordowy wynik jaki widziałem
Oj ;-; No cóż,będe miał dużo roboty

0
Marius.Maximus napisał(a):

Uwaga z narzędzia SonarLint na temat main

Refactor this function to reduce its Cognitive Complexity from 361 to the 25 allowed. sonarlint(cpp:S3776)

I opis na temat sonarlint(cpp:S3776) https://rules.sonarsource.com/cpp/RSPEC-3776/
W sumie to chyba rekordowy wynik jaki widziałem

@Marius.Maximus: Dziękuje za podesłanie strony

1 użytkowników online, w tym zalogowanych: 0, gości: 1