小坦克飞车

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值