#include <bits/stdc++.h>
#include <vector>
#include <cstdlib>
#include <ctime>
// Constants for board size
const int BOARD_SIZE = 10;
const int NUM_TANKS = 3;
// Class representing a Tank
class Tank {
private:
int x;
int y;
bool destroyed;
public:
Tank(int x, int y) : x(x), y(y), destroyed(false) {}
int getX() const { return x; }
int getY() const { return y; }
bool isDestroyed() const { return destroyed; }
void moveUp() { y--; }
void moveDown() { y++; }
void moveLeft() { x--; }
void moveRight() { x++; }
void destroy() { destroyed = true; }
};
// Class representing the game
class TankBattle {
private:
std::vector<Tank> tanks;
int playerX;
int playerY;
public:
TankBattle() : playerX(0), playerY(0) {
// Generate random positions for the tanks
std::srand(std::time(nullptr));
for (int i = 0; i < NUM_TANKS; i++) {
int x = std::rand() % BOARD_SIZE;
int y = std::rand() % BOARD_SIZE;
tanks.push_back(Tank(x, y));
}
}
void displayBoard() const {
for (int y = 0; y < BOARD_SIZE; y++) {
for (int x = 0; x < BOARD_SIZE; x++) {
if (playerX == x && playerY == y) {
std::cout << "P ";
}
else {
bool tankFound = false;
for (const auto& tank : tanks) {
if (tank.getX() == x && tank.getY() == y && !tank.isDestroyed()) {
std::cout << "T ";
tankFound = true;
break;
}
}
if (!tankFound) {
std::cout << "- ";
}
}
}
std::cout << std::endl;
}
}
bool isPlayerAlive() const {
for (const auto& tank : tanks) {
if (tank.getX() == playerX && tank.getY() == playerY && !tank.isDestroyed()) {
return false;
}
}
return true;
}
void movePlayer(char direction) {
switch (direction) {
case 'w':
if (playerY > 0) {
playerY--;
}
break;
case 's':
if (playerY < BOARD_SIZE - 1) {
playerY++;
}
break;
case 'a':
if (playerX > 0) {
playerX--;
}
break;
case 'd':
if (playerX < BOARD_SIZE - 1) {
playerX++;
}
break;
}
}
void play() {
char move;
while (isPlayerAlive()) {
displayBoard();
std::cout << "Enter your move (w - up, s - down, a - left, d - right): ";
std::cin >> move;
movePlayer(move);
}
std::cout << "Game Over! You were destroyed by a tank." << std::endl;
}
};
int main() {
TankBattle game;
game.play();
return 0;
}