加了setXY

mail@pastecode.io avatar
unknown
plain_text
7 months ago
4.4 kB
10
Indexable
Never
#include <iostream>
#include <vector>
#include <cmath>
#include <string> 
using namespace std;

class Map;

class character
{
protected:
    //名字、血量、攻擊、防禦、速度
    char symbol;
    string name;
    int healthPoints;
    int attackPoints;
    int defensePoints;
    int speedPoints;
    int x;  // 角色在方格地圖上的x、y座標 
    int y;


public:
    //Constructor
    character(const char charSymbol, const string& charName, int charHP, int charAttack, int charDefense, int charSpeed, int x, int y) : symbol(charSymbol), name(charName), healthPoints(charHP), attackPoints(charAttack), defensePoints(charDefense), speedPoints(charSpeed), x(x), y(y) {}

    //取得基本資料函數
    char getSymbol() const { return symbol; }
    string getName() const { return name; }
    int getHealthPoints() const { return healthPoints; }
    int getAttackPoints() const { return attackPoints; }
    int getDefensePoints() const { return defensePoints; }
    int getSpeedPoints() const { return speedPoints; }
    int getX() const { return x; }
    int getY() const { return y; }


    //顯示基本資料
    void displayInfo() const {
        cout << "Name: " << name << endl;
        cout << "HP: " << healthPoints << endl;
        cout << "Attack: " << attackPoints << endl;
        cout << "Defence: " << defensePoints << endl;
    }

    

    //傷害
    void takeDamage(int damage) {
        healthPoints -= damage;
        if (healthPoints < 0) {
            healthPoints = 0;
            cout << name << " 被擊敗了!" << endl;
        }
        else {
            cout << name << " 受到 " << damage << " 點傷害,剩餘生命值:" << healthPoints << endl;
        }
    }

    //設置角色X、Y
    void setXY(int newX, int newY) {
        x = newX;
        y = newY;
    }

    // 移動角色
    void move(Map& chessBoard);
};

class Map {
private:
    int rows;   // 地圖的行數
    int cols;   // 地圖的列數
    vector<std::vector<char>> map;  // 地圖的表示

public:
    Map(int rows, int cols)
        : rows(rows), cols(cols), map(rows, vector<char>(cols, '.')) {}

    void draw() const {
        for (const auto& row : map) {
            for (char cell : row) {
                std::cout << cell << ' ';
            }
            cout << endl;
        }
    }

     void placeCharacter(const character& character) {
        int row = character.getY();
        int col = character.getX();

        // 檢查座標是否合法
        if (row >= 0 && row < rows && col >= 0 && col < cols) {
            map[row][col] = character.getSymbol();
        }
    }

    void cleanCharacter(character& character) {
        int row = character.getY();
        int col = character.getX();

        // 檢查座標是否合法
        if (row >= 0 && row < rows && col >= 0 && col < cols) {
            map[row][col] = ' ';
        }
    }
        

};


void character::move(Map& chessBoard) {
        int moveX = 0;  
        int moveY = 0;

        chessBoard.cleanCharacter( *this);  // 將原先位置設為空格

        cout << "Your Move(Enter your movement): " << endl;
        cin >> moveX >> moveY;

        x += moveX;
        y += moveY;

        chessBoard.placeCharacter(*this);  // 更新新位置


        cout << x << " " << y << endl;
}

int main() {
    // 初始化地圖
    Map chessBoard(20, 20);

    // 初始化玩家
    // 玩家的隊伍
    vector<character> team = {
        character('W', "Wizard", 100, 50, 30, 10, 5, 0),
        character('K', "Knight", 120, 40, 25, 15, 7, 2),
        // Add more characters to the team...
    };

    // 放置隊伍進地圖
    for (const auto& character : team) {
        chessBoard.placeCharacter(character);
    }

    //遊戲剛開始時地圖
    cout << "Initial Chess Board:" << endl;
    chessBoard.draw();

    // Game loop (回合制)
    for (auto& character : team) {
        // 顯示角色基本資料
        character.displayInfo();

        // 移動角色
        character.move(chessBoard);
    }

    // 繪製移動後的地圖
    cout << "Chess Board after moving player:" << std::endl;
    chessBoard.draw();

    return 0;
}
Leave a Comment