Fork Copy #include #include #include #include #include using namespace std; int calcScore(int value) { switch (value) { case 1: return 10; case 2: return 3; case 3: return -5; default: return 0; } } bool isBomb(int value) { return value == 4; } char display(int value, bool isDug) { if (isDug) return 'X'; switch (value) { case 1: return 'V'; case 2: return 'D'; case 3: return 'T'; case 4: return '*'; default: return '.'; } } int main() { srand(time(0)); int n, K; cout << "Nhap kich thuoc ban do n x n: "; cin >> n; int total = n * n; int gold = total * 10 / 100; int rock = total * 20 / 100; int trap = total * 10 / 100; int bomb = total * 5 / 100; int empty = total - (gold + rock + trap + bomb); vector flatMap; flatMap.insert(flatMap.end(), gold, 1); flatMap.insert(flatMap.end(), rock, 2); flatMap.insert(flatMap.end(), trap, 3); flatMap.insert(flatMap.end(), bomb, 4); flatMap.insert(flatMap.end(), empty, 0); random_shuffle(flatMap.begin(), flatMap.end()); vector> map(n, vector(n)); vector> dug(n, vector(n, false)); for (int i = 0; i < total; ++i) map[i / n][i % n] = flatMap[i]; cout << "Nhap so luot choi toi da (K): "; cin >> K; int score = 0; bool gameOver = false; while (K > 0 && !gameOver) { int i, j; cout << "\nLuot con lai: " << K << ". Nhap toa do i j (0-based): "; cin >> i >> j; if (i < 0 || i >= n || j < 0 || j >= n) { cout << "Toa do khong hop le. Thu lai.\n"; continue; } if (dug[i][j]) { cout << "O nay da dao roi.\n"; continue; } dug[i][j] = true; if (isBomb(map[i][j])) { cout << "Trung bom!\n"; score = 0; gameOver = true; } else { int s = calcScore(map[i][j]); score += s; cout << "Dao duoc: " << s << " diem. Tong diem: " << score << endl; } K--; } cout << "\n=== TRO CHOI KET THUC ===\n"; cout << "Tong diem: " << score << endl; cout << "\n=== BAN DO CUOI CUNG ===\n"; for (int i = 0; i < n; ++i) { for (int j = 0; j < n; ++j) { cout << display(map[i][j], dug[i][j]) << " "; } cout << endl; } return 0; }