Fork Copy #include #include #include using namespace std; char map[10][5]; int playerX = 2; void initMap() { for (int i = 0; i < 10; i++) { for (int j = 0; j < 5; j++) { map[i][j] = '.'; } } map[9][playerX] = 'Y'; } void drawMap() { for (int i = 0; i < 10; i++) { for (int j = 0; j < 5; j++) { cout << map[i][j] << ' '; } cout << endl; } cout << "--------------------" << endl; } void generateObstacle() { int col = rand() % 5; map[0][col] = 'X'; } void moveDown() { for (int i = 8; i >= 0; i--) { for (int j = 0; j < 5; j++) { if (map[i][j] == 'X') { map[i][j] = '.'; map[i + 1][j] = 'X'; } } } } bool checkCollision() { return (map[9][playerX] == 'X'); } void movePlayer(char c) { map[9][playerX] = '.'; if (c == 'A' && playerX > 0) playerX--; else if (c == 'D' && playerX < 4) playerX++; // For 'Q' or other inputs, stay in place map[9][playerX] = 'Y'; } int main() { srand(time(0)); initMap(); int k; cout << "hay nhap k: "; cin >> k; int turns = 0; while (turns < k) { drawMap(); char C; cout << "hay nhap di chuyen A: sang trai; D sang phai; Q: dung yen "; cin >> C; movePlayer(C); moveDown(); if (checkCollision()) { drawMap(); cout << "you lose!" << endl; return 0; } else { // Clear any 'X' that landed on row 9 without collision for (int j = 0; j < 5; j++) { if (map[9][j] == 'X') map[9][j] = '.'; } } generateObstacle(); turns++; } drawMap(); cout << "Thang cuoc" << endl; return 0; }