Game isn't working properly

Can someone tell me why my game shows me random letters, numbers and symbols

#include
#include
#include
#include

using namespace std;

// Constants for the game dimensions
const int GRID_WIDTH = 40;
const int GRID_HEIGHT = 20;

// Struct to represent the player character
struct Player {
int x;
int y;
};

// Function to draw the game grid and objects
void draw(string grid[GRID_HEIGHT][GRID_WIDTH], Player player, int enemy_x, int enemy_y) {
// Clear the screen
cout << "\033[2J\033[1;1H";

// Draw the grid and objects
for (int y = 0; y < GRID_HEIGHT; y++) {
for (int x = 0; x < GRID_WIDTH; x++) {
if (x == player.x && y == player.y) {
cout << "P";
} else if (x == enemy_x && y == enemy_y) {
cout << "E";
} else {
cout << grid[y][x];
}
}
cout << endl;
}
}

int main() {
// Initialize the random number generator
srand(time(NULL));

// Initialize the game grid and objects
string grid[GRID_HEIGHT][GRID_WIDTH];
for (int y = 0; y < GRID_HEIGHT; y++) {
for (int x = 0; x < GRID_WIDTH; x++) {
grid[y][x] = " ";
}
}
Player player = {1, 1};
int enemy_x = GRID_WIDTH - 2;
int enemy_y = GRID_HEIGHT - 2;
int score = 0;

// Set up the random number generator
std::random_device rd;
std::mt19937 rng(rd());
std::uniform_int_distribution dist(-1, 1);

// Main game loop
while (true) {
// Draw the game grid and objects
draw(grid, player, enemy_x, enemy_y);

// Get the player's input
char input;
cin >> input;

// Update the player position based on the input
if (input == 'd') player.x++;
if (input == 'a') player.x--;
if (input == 'w') player.y--;
if (input == 's') player.y++;

// Update the enemy position
enemy_x += dist(rng);
enemy_y += dist(rng);

// Check if the player has collided with the enemy
if (player.x == enemy_x && player.y == enemy_y) {
  cout << "Przegrales! WYNIK: " << score << "." << endl;
  return 0;
}

// Increment the score
score++;

}

return 0;
}

A few questions, as well as asking you to post your code. properly.

What board, processor and tool chain are you using, obviously not the IDE.

Have you written code before, or is this just a cut & paste ?

nvm i fixed it

You're leaving but the post stays here where searches can find it. Can you please mention the solution so people who might seek a similar answer and stumble on this thread can know how you fixed it?

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.