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;
}