Untitled

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

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

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

    void draw() const;
    void placeCharacter(const character& character);
    void cleanCharacter(character& character);
    void placeCharacter(character& character);
        

};



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


public:
    //Constructor
    character() : symbol('N'), name("None"), healthPoints(0), attackPoints(0), defensePoints(0), speedPoints(0), x(0), y(0) {}
    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(vector<character> team);
    //傷害
    void takeDamage(int damage);
    //攻擊
    void attack();
    // 移動角色
    void move(Map& chessBoard);
    // 確認是否在攻擊範圍內
    bool checkDistance(character& character) const;
};



// void character::attack() {
//     bool checkAttack() = false;
//     string dowhat;
//     getline(cin, dowhat);
// }

// 確認傳入之角色是否於九宮格內
bool character::checkDistance(character& character) const{
    if(abs(x - character.getX()) == 1 || abs(y - character.getY()) == 1 || (abs(x - character.getX()) == 1 && abs(y - character.getY()) == 1)){
        return true;
    }
    return false;
}

void character::displayInfo(vector<character> team){
    cout << "Name: " << name << endl;
    cout << "HP: " << healthPoints << endl;
    cout << "Attack: " << attackPoints << endl;
    cout << "Defence: " << defensePoints << endl;


    // 待修 不確定要不要在character內建一個存此角色可以攻擊的對象的index
    vector<size_t> tempAttackIndex;
    for(size_t i = 0; i < team.size(); i++){

        if(this->getSymbol() != team[i].getSymbol() && checkDistance(team[i]) == true){
            tempAttackIndex.push_back(i);
        }
    }
    // 如果此vector為空(沒人可以打)
    if(tempAttackIndex.empty() == true){
        return;
    }
    else{
        cout << "You can attack: ";
        canAttackIndex = tempAttackIndex; // 待修
        for(size_t i = 0; i < tempAttackIndex.size(); i++){
            cout << team[tempAttackIndex[i]].getName() << " ";
        }
        cout << '\n';
        return;
    }
}

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

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

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

void Map::placeCharacter(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 Map::draw() const{
    for (int i = 0; i < rows; i++) {
            for (int j = 0; j < cols; j++) {
                cout << map[i][j] << ' ';
            }
            cout << endl;
        }
}

void character::move(Map& chessBoard) {
    string input;
    int moveX;
    int moveY;
    cout << "Your Move(Enter your movement): " << endl;
    getline(cin, input);
    chessBoard.cleanCharacter(*this);  // 將原先位置設為空格

    if(input == "attack"){
        // call function "attack"
        return;
    }
    else if(input == "move"){
        cin >> moveX >> moveY;
        cin.ignore();
        x += moveX;
        y += moveY;
        chessBoard.placeCharacter(*this);  // 更新新位置

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


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

    // 玩家的隊伍
    vector<character> team;
    // 初始化玩家
    character W = character('W', "Wizard", 100, 50, 30, 10, 5, 0);
    character K = character('K', "Knight", 120, 40, 25, 15, 7, 2);
    // 加入隊伍
    team.push_back(W);
    team.push_back(K);

    // 放置隊伍進地圖
    for(size_t i = 0; i < team.size(); i++) {
        chessBoard.placeCharacter(team[i]);
    }

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

    // Game loop (回合制)
    for(size_t i = 0; i < team.size(); i++) { 
        // 顯示角色基本資料
        team[i].displayInfo(team);

        // 移動角色
        team[i].move(chessBoard);


        cout << "\n\n\n\n\n\n\n\n\n\n";
        cout << "Chess Board after moving player:" << endl;
        chessBoard.draw();
    }

    return 0;
}
Leave a Comment